forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonObjectTests.cs
More file actions
313 lines (255 loc) · 13.5 KB
/
JsonObjectTests.cs
File metadata and controls
313 lines (255 loc) · 13.5 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace ServiceStack.Text.Tests
{
[TestFixture]
public class JsonObjectTests : TestBase
{
private const string JsonCentroid = @"{""place"":{ ""woeid"":12345, ""placeTypeName"":""St\\a\/te"" } }";
[Test]
public void Can_dynamically_parse_JSON_with_escape_chars()
{
var placeTypeName = JsonObject.Parse(JsonCentroid).Object("place").Get("placeTypeName");
Assert.That(placeTypeName, Is.EqualTo("St\\a/te"));
placeTypeName = JsonObject.Parse(JsonCentroid).Object("place").Get<string>("placeTypeName");
Assert.That(placeTypeName, Is.EqualTo("St\\a/te"));
}
private const string JsonEscapedByteArray = @"{""universalId"":""09o4bFTeBq3hTKhoJVCkzSLRG\/o1SktTPqxgZ3L3Xss=""}";
[Test]
public void Can_dynamically_parse_JSON_with_escape_byte_array()
{
var parsed = JsonObject.Parse(JsonEscapedByteArray).Get<byte[]>("universalId");
Assert.That(parsed, Is.EqualTo(new byte[] {
0xd3, 0xda, 0x38, 0x6c, 0x54, 0xde, 0x06, 0xad,
0xe1, 0x4c, 0xa8, 0x68, 0x25, 0x50, 0xa4, 0xcd,
0x22, 0xd1, 0x1b, 0xfa, 0x35, 0x4a, 0x4b, 0x53,
0x3e, 0xac, 0x60, 0x67, 0x72, 0xf7, 0x5e, 0xcb}));
}
[Test]
public void Does_escape_string_access()
{
string test = "\"quoted string\"";
var json = JsonSerializer.SerializeToString(new { a = test });
var jsonObject = JsonObject.Parse(json);
var actual = jsonObject["a"];
Assert.That(actual, Is.EqualTo(test));
Assert.That(jsonObject.Get("a"), Is.EqualTo(test));
Assert.That(jsonObject.Get<string>("a"), Is.EqualTo(test));
Assert.That(jsonObject.GetUnescaped("a"), Is.EqualTo(test.Replace("\"", "\\\"")));
}
[Test]
public void Does_encode_unicode()
{
string test = "<\"I get this : ";
var obj = new { test };
using (var mem = new System.IO.MemoryStream())
{
ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), mem);
var encoded = System.Text.Encoding.UTF8.GetString(mem.ToArray());
var copy1 = JsonObject.Parse(encoded);
Assert.That(test, Is.EqualTo(copy1["test"]));
System.Diagnostics.Debug.WriteLine(copy1["test"]);
}
}
[Test]
public void Does_encode_large_strings()
{
char[] testChars = new char[32769];
for (int i = 0; i < testChars.Length; i++)
testChars[i] = (char)i;
string test = new string(testChars);
var obj = new { test };
using (var mem = new System.IO.MemoryStream())
{
ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), mem);
var encoded = System.Text.Encoding.UTF8.GetString(mem.ToArray());
var copy1 = JsonObject.Parse(encoded);
Assert.That(test, Is.EqualTo(copy1["test"]));
System.Diagnostics.Debug.WriteLine(copy1["test"]);
}
}
[Test]
public void Can_parse_Twitter_response()
{
var json = @"[{""is_translator"":false,""geo_enabled"":false,""profile_background_color"":""000000"",""protected"":false,""default_profile"":false,""profile_background_tile"":false,""created_at"":""Sun Nov 23 17:42:51 +0000 2008"",""name"":""Demis Bellot TW"",""profile_background_image_url_https"":""https:\/\/si0.twimg.com\/profile_background_images\/192991651\/twitter-bg.jpg"",""profile_sidebar_fill_color"":""2A372F"",""listed_count"":36,""notifications"":null,""utc_offset"":0,""friends_count"":267,""description"":""StackExchangarista, JavaScript, C#, Web & Mobile developer. Creator of the ServiceStack.NET projects. "",""following"":null,""verified"":false,""profile_sidebar_border_color"":""D9D082"",""followers_count"":796,""profile_image_url"":""http:\/\/a2.twimg.com\/profile_images\/1598852740\/avatar_normal.png"",""contributors_enabled"":false,""profile_image_url_https"":""https:\/\/si0.twimg.com\/profile_images\/1598852740\/avatar_normal.png"",""status"":{""possibly_sensitive"":false,""place"":null,""retweet_count"":37,""in_reply_to_screen_name"":null,""created_at"":""Mon Nov 07 02:34:23 +0000 2011"",""retweeted"":false,""in_reply_to_status_id_str"":null,""in_reply_to_user_id_str"":null,""contributors"":null,""id_str"":""133371690876022785"",""retweeted_status"":{""possibly_sensitive"":false,""place"":null,""retweet_count"":37,""in_reply_to_screen_name"":null,""created_at"":""Mon Nov 07 02:32:15 +0000 2011"",""retweeted"":false,""in_reply_to_status_id_str"":null,""in_reply_to_user_id_str"":null,""contributors"":null,""id_str"":""133371151551447041"",""in_reply_to_user_id"":null,""in_reply_to_status_id"":null,""source"":""\u003Ca href=\""http:\/\/www.arstechnica.com\"" rel=\""nofollow\""\u003EArs auto-tweeter\u003C\/a\u003E"",""geo"":null,""favorited"":false,""id"":133371151551447041,""coordinates"":null,""truncated"":false,""text"":""Google: Microsoft uses patents when products \""stop succeeding\"": http:\/\/t.co\/50QFc1uJ by @binarybits""},""in_reply_to_user_id"":null,""in_reply_to_status_id"":null,""source"":""web"",""geo"":null,""favorited"":false,""id"":133371690876022785,""coordinates"":null,""truncated"":false,""text"":""RT @arstechnica: Google: Microsoft uses patents when products \""stop succeeding\"": http:\/\/t.co\/50QFc1uJ by @binarybits""},""profile_use_background_image"":true,""favourites_count"":238,""location"":""New York"",""id_str"":""17575623"",""default_profile_image"":false,""show_all_inline_media"":false,""profile_text_color"":""ABB8AF"",""screen_name"":""demisbellot"",""statuses_count"":9638,""profile_background_image_url"":""http:\/\/a0.twimg.com\/profile_background_images\/192991651\/twitter-bg.jpg"",""url"":""http:\/\/www.servicestack.net\/mythz_blog\/"",""time_zone"":""London"",""profile_link_color"":""43594A"",""id"":17575623,""follow_request_sent"":null,""lang"":""en""}]";
var objs = JsonObject.ParseArray(json);
var obj = objs[0];
Assert.That(obj.Get("name"), Is.EqualTo("Demis Bellot TW"));
}
[Test]
public void Can_parse_ArrayObjects()
{
var data = new { key = new[] { "value1", "value2" } };
var json = data.ToJson();
Assert.That(json, Is.EqualTo(@"{""key"":[""value1"",""value2""]}"));
var value = JsonObject.Parse(json);
var dataObjects = value.Get<string[]>("key");
Assert.That(dataObjects[0], Is.EqualTo("value1"));
Assert.That(dataObjects[1], Is.EqualTo("value2"));
}
[Test]
public void Can_deserialize_JsonArray()
{
var json = @"
{
""projects"":[
{
""name"": ""Project1""
},
{
""name"": ""Project2""
},
{
""name"": ""Project3""
}
]
}";
var projects = JsonObject.Parse(json).ArrayObjects("projects");
var proj3Name = projects[2].Get("name");
Assert.That(proj3Name, Is.EqualTo("Project3"));
}
[Test]
public void Can_deserialize_JSON_Object()
{
var json = "{\"http://SomeUrl.com/\":{\"http://otherUrl.org/schema#name\":[{\"value\":\"val1\",\"type\":\"val2\"}]}}";
var obj = JsonObject.Parse(json)
.Object("http://SomeUrl.com/");
var items = obj.ArrayObjects("http://otherUrl.org/schema#name")[0];
Assert.That(items["value"], Is.EqualTo("val1"));
Assert.That(items["type"], Is.EqualTo("val2"));
}
public class Customer
{
public static List<object> Setters = new List<object>();
private string name;
private int age;
private string address;
public string Name
{
get { return name; }
set { name = value; Setters.Add(value); }
}
public int Age
{
get { return age; }
set { age = value; Setters.Add(value); }
}
public string Address
{
get { return address; }
set { address = value; Setters.Add(value); }
}
}
[Test]
public void Only_sets_Setters_with_JSON()
{
var dto = "{\"Name\":\"Foo\"}".FromJson<Customer>();
Assert.That(dto.Name, Is.EqualTo("Foo"));
Assert.That(Customer.Setters.Count, Is.EqualTo(1));
Assert.That(Customer.Setters[0], Is.EqualTo(dto.Name));
}
public class TypeObject
{
public string Prop1 { get; set; }
public int Prop2 { get; set; }
public bool Prop3 { get; set; }
public double Prop4 { get; set; }
public string[] Prop5 { get; set; }
public Dictionary<string, string> Prop6 { get; set; }
}
[Test]
public void Can_parse_dynamic_json()
{
var json = @"{
""prop1"": ""text string"",
""prop2"": 33,
""prop3"": true,
""prop4"": 6.3,
""prop5"": [ ""A"", ""B"", ""C"" ],
""prop6"": { ""A"" : ""a"" }
}";
var typeObj = json.FromJson<TypeObject>();
Assert.That(typeObj.Prop1, Is.EqualTo("text string"));
Assert.That(typeObj.Prop2, Is.EqualTo(33));
Assert.That(typeObj.Prop3, Is.EqualTo(true));
Assert.That(typeObj.Prop4, Is.EqualTo(6.3d));
Assert.That(typeObj.Prop5, Is.EquivalentTo(new[] { "A", "B", "C" }));
Assert.That(typeObj.Prop6, Is.EquivalentTo(new Dictionary<string, string> { { "A", "a" } }));
var obj = JsonObject.Parse(json);
var o = new TypeObject
{
Prop1 = obj["prop1"],
Prop2 = obj.Get<int>("prop2"),
Prop3 = obj.Get<bool>("prop3"),
Prop4 = obj.Get<double>("prop4"),
Prop5 = obj.Get<string[]>("prop5"),
Prop6 = obj.Object("prop6"),
};
Assert.That(o.Prop1, Is.EqualTo("text string"));
Assert.That(o.Prop2, Is.EqualTo(33));
Assert.That(o.Prop3, Is.EqualTo(true));
Assert.That(o.Prop4, Is.EqualTo(6.3d));
Assert.That(o.Prop5, Is.EquivalentTo(new[] { "A", "B", "C" }));
Assert.That(o.Prop6, Is.EquivalentTo(new Dictionary<string, string> { { "A", "a" } }));
}
[Test]
public void Can_deserialize_array_string_in_Map()
{
var json = "{\"name\":\"foo\",\"roles\":[\"Role1\",\"Role 2\"]}";
var obj = JsonObject.Parse(json);
Assert.That(obj.GetArray<string>("roles"), Is.EqualTo(new[] { "Role1", "Role 2" }));
var map = json.FromJson<Dictionary<string, string>>();
Assert.That(map.GetArray<string>("roles"), Is.EqualTo(new[] { "Role1", "Role 2" }));
}
[Test]
public void Can_deserialize_array_numbers_in_Map()
{
var json = "{\"name\":\"foo\",\"roles\":[1,2]}";
var obj = JsonObject.Parse(json);
Assert.That(obj.GetArray<int>("roles"), Is.EqualTo(new[] { 1, 2 }));
var map = json.FromJson<Dictionary<string, string>>();
Assert.That(map.GetArray<int>("roles"), Is.EqualTo(new[] { 1, 2 }));
}
public class TestJArray
{
public int Id { get; set; }
public string Name { get; set; }
protected bool Equals(TestJArray other)
{
return Id == other.Id && string.Equals(Name, other.Name);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((TestJArray) obj);
}
public override int GetHashCode()
{
unchecked
{
return (Id*397) ^ (Name != null ? Name.GetHashCode() : 0);
}
}
}
[Test]
public void Can_deserialize_array_objects_in_Map()
{
var json = "{\"name\":\"foo\",\"roles\":[{\"Id\":1,\"Name\":\"Role1\"},{\"Id\":2,\"Name\":\"Role 2\"}]}";
var obj = JsonObject.Parse(json);
Assert.That(obj.GetArray<TestJArray>("roles"), Is.EqualTo(new[]
{
new TestJArray { Id = 1, Name = "Role1" },
new TestJArray { Id = 2, Name = "Role 2" },
}));
var map = json.FromJson<Dictionary<string, string>>();
Assert.That(map.GetArray<TestJArray>("roles"), Is.EqualTo(new[]
{
new TestJArray { Id = 1, Name = "Role1" },
new TestJArray { Id = 2, Name = "Role 2" },
}));
}
}
}