Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit c7ec154

Browse files
committed
Escape JSON strings when written to TextWriter or Stream
1 parent a60ceb4 commit c7ec154

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

src/ServiceStack.Text/Json/JsonWriter.Generic.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ public static TypeInfo GetTypeInfo()
174174

175175
static JsonWriter()
176176
{
177-
var isNumeric = typeof(T).IsNumericType();
177+
if (JsonWriter.Instance == null)
178+
return;
179+
180+
var isNumeric = typeof(T).IsNumericType();
178181
TypeInfo = new TypeInfo {
179182
EncodeMapKey = typeof(T) == typeof(bool) || isNumeric,
180183
IsNumeric = isNumeric

src/ServiceStack.Text/JsonSerializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public static void SerializeToWriter<T>(T value, TextWriter writer)
152152
if (value == null) return;
153153
if (typeof(T) == typeof(string))
154154
{
155-
writer.Write(value);
155+
JsonUtils.WriteString(writer, value as string);
156156
}
157157
else if (typeof(T) == typeof(object))
158158
{
@@ -175,7 +175,7 @@ public static void SerializeToWriter(object value, Type type, TextWriter writer)
175175
if (value == null) return;
176176
if (type == typeof(string))
177177
{
178-
writer.Write(value);
178+
JsonUtils.WriteString(writer, value as string);
179179
return;
180180
}
181181

src/ServiceStack.Text/TypeSerializer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ public static string SerializeToString<T>(T value)
151151
public static string SerializeToString(object value, Type type)
152152
{
153153
if (value == null) return null;
154-
if (type == typeof(string)) return value as string;
154+
if (type == typeof(string))
155+
return value as string;
155156

156157
using (var sb = StringBuilderWriter.Create())
157158
{

tests/ServiceStack.Text.Tests/ServiceStack.Text.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@
268268
<Compile Include="Shared\Shipper.cs" />
269269
<Compile Include="Shared\ShipperFactory.cs" />
270270
<Compile Include="Shared\TaskQueue.cs" />
271+
<Compile Include="StreamTests.cs" />
271272
<Compile Include="StringConverterUtilsTests.cs" />
272273
<Compile Include="StringTests.cs" />
273274
<Compile Include="StructTests.cs" />
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.IO;
2+
using NUnit.Framework;
3+
4+
namespace ServiceStack.Text.Tests
5+
{
6+
[TestFixture]
7+
public class StreamTests
8+
{
9+
[Test]
10+
public void Does_escape_string_when_serializing_to_TextWriter()
11+
{
12+
var expected = @"String with backslashes '\', 'single' and ""double quotes"", (along with other special symbols like tabs) wich may broke incorrect serializing/deserializing implementation ;)";
13+
14+
var json = "\"String with backslashes '\\\\', 'single' and \\\"double quotes\\\", (along\\t\\twith\\tother\\tspecial\\tsymbols\\tlike\\ttabs) wich may broke incorrect serializing/deserializing implementation ;)\"";
15+
16+
using (var ms = new MemoryStream())
17+
{
18+
var sw = new StreamWriter(ms);
19+
JsonSerializer.SerializeToWriter(expected, sw);
20+
sw.Flush();
21+
22+
using (var sr = new StreamReader(ms))
23+
{
24+
ms.Position = 0;
25+
var ssJson = sr.ReadToEnd();
26+
Assert.That(ssJson, Is.EqualTo(json));
27+
28+
ms.Position = 0;
29+
var ssString = JsonSerializer.DeserializeFromReader(sr, typeof(string));
30+
Assert.That(ssString, Is.EqualTo(expected));
31+
}
32+
}
33+
}
34+
35+
[Test]
36+
public void Does_escape_string_when_serializing_to_Stream()
37+
{
38+
var expected = @"String with backslashes '\', 'single' and ""double quotes"", (along with other special symbols like tabs) wich may broke incorrect serializing/deserializing implementation ;)";
39+
40+
var json = "\"String with backslashes '\\\\', 'single' and \\\"double quotes\\\", (along\\t\\twith\\tother\\tspecial\\tsymbols\\tlike\\ttabs) wich may broke incorrect serializing/deserializing implementation ;)\"";
41+
42+
using (var ms = new MemoryStream())
43+
{
44+
JsonSerializer.SerializeToStream(expected, ms);
45+
var ssJson = ms.ToArray().FromUtf8Bytes();
46+
47+
Assert.That(ssJson, Is.EqualTo(json));
48+
49+
ms.Position = 0;
50+
var ssString = JsonSerializer.DeserializeFromStream(typeof(string), ms);
51+
52+
Assert.That(ssString, Is.EqualTo(expected));
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)