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

Commit ecafc5b

Browse files
committed
Add escaped new line test
1 parent 029fe1c commit ecafc5b

File tree

1 file changed

+141
-130
lines changed

1 file changed

+141
-130
lines changed

tests/ServiceStack.Text.Tests/JsonTests/EscapedCharsTests.cs

Lines changed: 141 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -5,150 +5,151 @@
55

66
namespace ServiceStack.Text.Tests.JsonTests
77
{
8-
[TestFixture]
9-
public class EscapedCharsTests
10-
: TestBase
11-
{
12-
public class NestedModel
13-
{
14-
public string Id { get; set; }
8+
[TestFixture]
9+
public class EscapedCharsTests
10+
: TestBase
11+
{
12+
public class NestedModel
13+
{
14+
public string Id { get; set; }
1515

16-
public ModelWithIdAndName Model { get; set; }
17-
}
16+
public ModelWithIdAndName Model { get; set; }
17+
}
1818

1919
[TearDown]
2020
public void TearDown()
2121
{
2222
JsConfig.Reset();
2323
}
2424

25-
[Test]
26-
public void Can_deserialize_text_with_escaped_chars()
27-
{
28-
var model = new ModelWithIdAndName
29-
{
30-
Id = 1,
31-
Name = @"1 \ 2 \r 3 \n 4 \b 5 \f 6 """
32-
};
33-
34-
SerializeAndCompare(model);
35-
}
36-
37-
[Test]
38-
public void Can_short_circuit_string_with_no_escape_chars()
39-
{
40-
var model = new ModelWithIdAndName
41-
{
42-
Id = 1,
43-
Name = @"Simple string"
44-
};
45-
46-
SerializeAndCompare(model);
47-
}
48-
49-
[Test]
50-
public void Can_deserialize_json_with_whitespace()
51-
{
52-
var model = new ModelWithIdAndName
53-
{
54-
Id = 1,
55-
Name = @"Simple string"
56-
};
57-
58-
const string json = "\t { \t \"Id\" \t : 1 , \t \"Name\" \t : \t \"Simple string\" \t } \t ";
59-
60-
var fromJson = JsonSerializer.DeserializeFromString<ModelWithIdAndName>(json);
61-
62-
Assert.That(fromJson, Is.EqualTo(model));
63-
}
25+
[Test]
26+
public void Can_deserialize_text_with_escaped_chars()
27+
{
28+
var model = new ModelWithIdAndName
29+
{
30+
Id = 1,
31+
Name = @"1 \ 2 \r 3 \n 4 \b 5 \f 6 """
32+
};
33+
34+
SerializeAndCompare(model);
35+
}
36+
37+
[Test]
38+
public void Can_short_circuit_string_with_no_escape_chars()
39+
{
40+
var model = new ModelWithIdAndName
41+
{
42+
Id = 1,
43+
Name = @"Simple string"
44+
};
45+
46+
SerializeAndCompare(model);
47+
}
48+
49+
[Test]
50+
public void Can_deserialize_json_with_whitespace()
51+
{
52+
var model = new ModelWithIdAndName
53+
{
54+
Id = 1,
55+
Name = @"Simple string"
56+
};
57+
58+
const string json = "\t { \t \"Id\" \t : 1 , \t \"Name\" \t : \t \"Simple string\" \t } \t ";
59+
60+
var fromJson = JsonSerializer.DeserializeFromString<ModelWithIdAndName>(json);
61+
62+
Assert.That(fromJson, Is.EqualTo(model));
63+
}
6464

6565
public class Inner
6666
{
6767
public int Int { get; set; }
6868
}
69-
69+
7070
public class Program
7171
{
7272
public Inner[] Inner { get; set; }
7373
}
7474

75-
[Test]
76-
public void Can_deserialize_inner_whitespace()
77-
{
75+
[Test]
76+
public void Can_deserialize_inner_whitespace()
77+
{
7878
var fromJson = JsonSerializer.DeserializeFromString<Program>("{\"Inner\":[{\"Int\":0} , {\"Int\":1}\r\n]}");
7979
Assert.That(fromJson.Inner.Length, Is.EqualTo(2));
8080
Assert.That(fromJson.Inner[0].Int, Is.EqualTo(0));
8181
Assert.That(fromJson.Inner[1].Int, Is.EqualTo(1));
82-
82+
8383
var dto = new Program { Inner = new[] { new Inner { Int = 0 } } };
84-
Serialize(dto);
84+
Serialize(dto);
8585
var json = JsonSerializer.SerializeToString(dto);
8686
Assert.That(json, Is.EqualTo(@"{""Inner"":[{""Int"":0}]}"));
8787
}
8888

89-
[Test]
90-
public void Can_deserialize_nested_json_with_whitespace()
91-
{
92-
var model = new NestedModel
93-
{
94-
Id = "Nested with space",
95-
Model = new ModelWithIdAndName
96-
{
97-
Id = 1,
98-
Name = @"Simple string"
99-
}
100-
};
89+
[Test]
90+
public void Can_deserialize_nested_json_with_whitespace()
91+
{
92+
var model = new NestedModel
93+
{
94+
Id = "Nested with space",
95+
Model = new ModelWithIdAndName
96+
{
97+
Id = 1,
98+
Name = @"Simple string"
99+
}
100+
};
101101

102-
const string json = "\t { \"Id\" : \"Nested with space\" \n , \r \t \"Model\" \t : \n { \t \"Id\" \t : 1 , \t \"Name\" \t : \t \"Simple string\" \t } \t } \n ";
102+
const string json = "\t { \"Id\" : \"Nested with space\" \n , \r \t \"Model\" \t : \n { \t \"Id\" \t : 1 , \t \"Name\" \t : \t \"Simple string\" \t } \t } \n ";
103103

104-
var fromJson = JsonSerializer.DeserializeFromString<NestedModel>(json);
104+
var fromJson = JsonSerializer.DeserializeFromString<NestedModel>(json);
105105

106-
Assert.That(fromJson.Id, Is.EqualTo(model.Id));
107-
Assert.That(fromJson.Model, Is.EqualTo(model.Model));
108-
}
106+
Assert.That(fromJson.Id, Is.EqualTo(model.Id));
107+
Assert.That(fromJson.Model, Is.EqualTo(model.Model));
108+
}
109109

110110

111-
public class ModelWithList
112-
{
113-
public ModelWithList()
114-
{
115-
this.StringList = new List<string>();
116-
}
111+
public class ModelWithList
112+
{
113+
public ModelWithList()
114+
{
115+
this.StringList = new List<string>();
116+
}
117117

118-
public int Id { get; set; }
118+
public int Id { get; set; }
119119
public List<string> StringList { get; set; }
120120
public string[] StringArray { get; set; }
121121

122-
public bool Equals(ModelWithList other)
123-
{
124-
if (ReferenceEquals(null, other)) return false;
125-
if (ReferenceEquals(this, other)) return true;
126-
return other.Id == Id && StringList.EquivalentTo(other.StringList);
127-
}
128-
129-
public override bool Equals(object obj)
130-
{
131-
if (ReferenceEquals(null, obj)) return false;
132-
if (ReferenceEquals(this, obj)) return true;
133-
if (obj.GetType() != typeof(ModelWithList)) return false;
134-
return Equals((ModelWithList)obj);
135-
}
136-
137-
public override int GetHashCode()
138-
{
139-
unchecked
140-
{
141-
return (Id * 397) ^ (StringList != null ? StringList.GetHashCode() : 0);
142-
}
143-
}
144-
}
122+
public bool Equals(ModelWithList other)
123+
{
124+
if (ReferenceEquals(null, other)) return false;
125+
if (ReferenceEquals(this, other)) return true;
126+
return other.Id == Id && StringList.EquivalentTo(other.StringList);
127+
}
128+
129+
public override bool Equals(object obj)
130+
{
131+
if (ReferenceEquals(null, obj)) return false;
132+
if (ReferenceEquals(this, obj)) return true;
133+
if (obj.GetType() != typeof(ModelWithList)) return false;
134+
return Equals((ModelWithList)obj);
135+
}
136+
137+
public override int GetHashCode()
138+
{
139+
unchecked
140+
{
141+
return (Id * 397) ^ (StringList != null ? StringList.GetHashCode() : 0);
142+
}
143+
}
144+
}
145145

146146
[Test]
147147
public void Can_serialize_Model_with_array()
148148
{
149-
var model = new ModelWithList {
149+
var model = new ModelWithList
150+
{
150151
Id = 1,
151-
StringArray = new[]{ "One", "Two", "Three" }
152+
StringArray = new[] { "One", "Two", "Three" }
152153
};
153154

154155
SerializeAndCompare(model);
@@ -157,7 +158,8 @@ public void Can_serialize_Model_with_array()
157158
[Test]
158159
public void Can_serialize_Model_with_list()
159160
{
160-
var model = new ModelWithList {
161+
var model = new ModelWithList
162+
{
161163
Id = 1,
162164
StringList = { "One", "Two", "Three" }
163165
};
@@ -168,9 +170,10 @@ public void Can_serialize_Model_with_list()
168170
[Test]
169171
public void Can_serialize_Model_with_array_of_escape_chars()
170172
{
171-
var model = new ModelWithList {
173+
var model = new ModelWithList
174+
{
172175
Id = 1,
173-
StringArray = new[]{ @"1 \ 2 \r 3 \n 4 \b 5 \f 6 """, @"1 \ 2 \r 3 \n 4 \b 5 \f 6 """ }
176+
StringArray = new[] { @"1 \ 2 \r 3 \n 4 \b 5 \f 6 """, @"1 \ 2 \r 3 \n 4 \b 5 \f 6 """ }
174177
};
175178

176179
SerializeAndCompare(model);
@@ -179,43 +182,44 @@ public void Can_serialize_Model_with_array_of_escape_chars()
179182
[Test]
180183
public void Can_serialize_Model_with_list_of_escape_chars()
181184
{
182-
var model = new ModelWithList {
185+
var model = new ModelWithList
186+
{
183187
Id = 1,
184188
StringList = { @"1 \ 2 \r 3 \n 4 \b 5 \f 6 """, @"1 \ 2 \r 3 \n 4 \b 5 \f 6 """ }
185189
};
186190

187191
SerializeAndCompare(model);
188192
}
189193

190-
[Test]
191-
public void Can_deserialize_json_list_with_whitespace()
192-
{
193-
var model = new ModelWithList
194-
{
195-
Id = 1,
196-
StringList = { " One ", " Two " }
197-
};
194+
[Test]
195+
public void Can_deserialize_json_list_with_whitespace()
196+
{
197+
var model = new ModelWithList
198+
{
199+
Id = 1,
200+
StringList = { " One ", " Two " }
201+
};
198202

199-
Log(JsonSerializer.SerializeToString(model));
203+
Log(JsonSerializer.SerializeToString(model));
200204

201-
const string json = "\t { \"Id\" : 1 , \n \"StringList\" \t : \n [ \t \" One \" \t , \t \" Two \" \t ] \n } \t ";
205+
const string json = "\t { \"Id\" : 1 , \n \"StringList\" \t : \n [ \t \" One \" \t , \t \" Two \" \t ] \n } \t ";
202206

203-
var fromJson = JsonSerializer.DeserializeFromString<ModelWithList>(json);
207+
var fromJson = JsonSerializer.DeserializeFromString<ModelWithList>(json);
204208

205-
Assert.That(fromJson, Is.EqualTo(model));
206-
}
209+
Assert.That(fromJson, Is.EqualTo(model));
210+
}
207211

208-
[Test]
209-
public void Can_deserialize_basic_latin_unicode()
210-
{
211-
const string json = "{\"Id\":1,\"Name\":\"\\u0041 \\u0042 \\u0043 | \\u0031 \\u0032 \\u0033\"}";
212+
[Test]
213+
public void Can_deserialize_basic_latin_unicode()
214+
{
215+
const string json = "{\"Id\":1,\"Name\":\"\\u0041 \\u0042 \\u0043 | \\u0031 \\u0032 \\u0033\"}";
212216

213-
var model = new ModelWithIdAndName { Id = 1, Name = "A B C | 1 2 3" };
217+
var model = new ModelWithIdAndName { Id = 1, Name = "A B C | 1 2 3" };
214218

215-
var fromJson = JsonSerializer.DeserializeFromString<ModelWithIdAndName>(json);
219+
var fromJson = JsonSerializer.DeserializeFromString<ModelWithIdAndName>(json);
216220

217-
Assert.That(fromJson, Is.EqualTo(model));
218-
}
221+
Assert.That(fromJson, Is.EqualTo(model));
222+
}
219223

220224
[Test]
221225
public void Can_serialize_unicode_without_escape()
@@ -303,10 +307,17 @@ public void Can_deserialize_with_new_line()
303307
Assert.That(fromJson, Is.EqualTo(model));
304308
}
305309

306-
307310
public class MyModel
308311
{
309312
public string Name { get; set; }
310313
}
311-
}
314+
315+
[Test]
316+
public void Can_serialize_string_with_new_line()
317+
{
318+
Assert.That("Line1\nLine2".ToJson(), Is.EqualTo("\"Line1\\nLine2\""));
319+
Assert.That(new MyModel { Name = "Line1\nLine2" }.ToJson(),
320+
Is.EqualTo("{\"Name\":\"Line1\\nLine2\"}"));
321+
}
322+
}
312323
}

0 commit comments

Comments
 (0)