forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamTests.cs
More file actions
56 lines (45 loc) · 2.26 KB
/
StreamTests.cs
File metadata and controls
56 lines (45 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System.IO;
using NUnit.Framework;
namespace ServiceStack.Text.Tests
{
[TestFixture]
public class StreamTests
{
[Test]
public void Does_escape_string_when_serializing_to_TextWriter()
{
var expected = @"String with backslashes '\', 'single' and ""double quotes"", (along with other special symbols like tabs) wich may broke incorrect serializing/deserializing implementation ;)";
var json = "\"String with backslashes '\\\\', 'single' and \\\"double quotes\\\", (along\\t\\twith\\tother\\tspecial\\tsymbols\\tlike\\ttabs) wich may broke incorrect serializing/deserializing implementation ;)\"";
using (var ms = new MemoryStream())
{
var sw = new StreamWriter(ms);
JsonSerializer.SerializeToWriter(expected, sw);
sw.Flush();
using (var sr = new StreamReader(ms))
{
ms.Position = 0;
var ssJson = sr.ReadToEnd();
Assert.That(ssJson, Is.EqualTo(json));
ms.Position = 0;
var ssString = JsonSerializer.DeserializeFromReader(sr, typeof(string));
Assert.That(ssString, Is.EqualTo(expected));
}
}
}
[Test]
public void Does_escape_string_when_serializing_to_Stream()
{
var expected = @"String with backslashes '\', 'single' and ""double quotes"", (along with other special symbols like tabs) wich may broke incorrect serializing/deserializing implementation ;)";
var json = "\"String with backslashes '\\\\', 'single' and \\\"double quotes\\\", (along\\t\\twith\\tother\\tspecial\\tsymbols\\tlike\\ttabs) wich may broke incorrect serializing/deserializing implementation ;)\"";
using (var ms = new MemoryStream())
{
JsonSerializer.SerializeToStream(expected, ms);
var ssJson = ms.ToArray().FromUtf8Bytes();
Assert.That(ssJson, Is.EqualTo(json));
ms.Position = 0;
var ssString = JsonSerializer.DeserializeFromStream(typeof(string), ms);
Assert.That(ssString, Is.EqualTo(expected));
}
}
}
}