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
378 lines (295 loc) · 12.9 KB
/
JsonObjectTests.cs
File metadata and controls
378 lines (295 loc) · 12.9 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
using System.Collections.Generic;
using NUnit.Framework;
namespace ServiceStack.Text.Tests.JsonTests
{
[TestFixture]
public class JsonObjectTests
{
[Test]
public void Can_parse_empty_object()
{
Assert.That(JsonObject.Parse("{}"), Is.Empty);
}
[Test]
public void Can_parse_empty_object_with_whitespaces()
{
Assert.That(JsonObject.Parse("{ }"), Is.Empty);
Assert.That(JsonObject.Parse("{\n\n}"), Is.Empty);
Assert.That(JsonObject.Parse("{\t\t}"), Is.Empty);
}
[Test]
public void Can_parse_empty_object_with_mixed_whitespaces()
{
Assert.That(JsonObject.Parse("{ \n\t \n\r}"), Is.Empty);
}
[Test]
public void Can_Serialize_numbers()
{
string notNumber = "{\"field\":\"00001\"}";
Assert.AreEqual(notNumber, JsonObject.Parse(notNumber).ToJson<JsonObject>());
string num1 = "{\"field\":0}";
Assert.AreEqual(num1, JsonObject.Parse(num1).ToJson<JsonObject>());
string num2 = "{\"field\":0.5}";
Assert.AreEqual(num2, JsonObject.Parse(num2).ToJson<JsonObject>());
string num3 = "{\"field\":.5}";
Assert.AreEqual(num3, JsonObject.Parse(num3).ToJson<JsonObject>());
string num4 = "{\"field\":12312}";
Assert.AreEqual(num4, JsonObject.Parse(num4).ToJson<JsonObject>());
string num5 = "{\"field\":12312.1231}";
Assert.AreEqual(num5, JsonObject.Parse(num5).ToJson<JsonObject>());
}
public class Jackalope
{
public string Name { get; set; }
public Jackalope BabyJackalope { get; set; }
}
[Test]
public void Can_serialise_json_object_deserialise_typed_object()
{
var jacks = new {
Jack = new Jackalope { BabyJackalope = new Jackalope { Name = "in utero" } }
};
var jackString = JsonSerializer.SerializeToString(jacks.Jack);
var jackJson = JsonObject.Parse(jackString);
var jack = jackJson.Get<Jackalope>("BabyJackalope");
Assert.That(jacks.Jack.BabyJackalope.Name, Is.EqualTo(jack.Name));
var jackJsonString = jackJson.SerializeToString();
Assert.That(jackString, Is.EqualTo(jackJsonString));
var jackalope = JsonSerializer.DeserializeFromString<Jackalope>(jackJsonString);
Assert.That(jackalope.BabyJackalope.Name, Is.EqualTo("in utero"));
}
readonly TextElementDto text = new TextElementDto {
ElementId = "text_1",
ElementType = "text",
// Raw nesting - won't be escaped
Content = new ElementContentDto { ElementId = "text_1", Content = "text goes here" },
Action = new ElementActionDto { ElementId = "text_1", Action = "action goes here" }
};
readonly ImageElementDto image = new ImageElementDto {
ElementId = "image_1",
ElementType = "image",
// String nesting - will be escaped
Content = new ElementContentDto { ElementId = "image_1", Content = "image url goes here" }.ToJson(),
Action = new ElementActionDto { ElementId = "image_1", Action = "action goes here" }.ToJson()
};
readonly SimpleObj simple = new SimpleObj
{
value1 = "Foo",
value2 = "Bar"
};
[Test]
public void Can_Deserialize_JsonValue()
{
var json = simple.ToJson();
var jsonValue = new JsonValue(json);
var fromJson = jsonValue.As<SimpleObj>();
Assert.That(fromJson.value1, Is.EqualTo(simple.value1));
Assert.That(fromJson.value2, Is.EqualTo(simple.value2));
}
[Test]
public void Can_Serialize_JsonValue_Multiple_Times()
{
var json = simple.ToJson();
var jsonValue = new JsonValue(json);
var jsonAfter = jsonValue.ToJson();
Assert.That(jsonAfter, Is.EqualTo(json));
}
[Test]
public void Can_Deserialize_JsonValue_After_Multiple_Serialize()
{
var json = simple.ToJson();
var jsonValue = new JsonValue(json);
jsonValue = new JsonValue(jsonValue.ToJson());
var fromJson = jsonValue.As<SimpleObj>();
Assert.That(fromJson.value1, Is.EqualTo(simple.value1));
Assert.That(fromJson.value2, Is.EqualTo(simple.value2));
}
[Test]
public void Can_Deserialize_JsonValue_After_Multiple_Serialize_2()
{
var json = simple.ToJson();
var jsonValue = new JsonValue(json);
json = jsonValue.ToJson();
var fromJson = json.FromJson<SimpleObj>();
Assert.That(fromJson.value1, Is.EqualTo(simple.value1));
Assert.That(fromJson.value2, Is.EqualTo(simple.value2));
}
[Test]
public void Can_Serialize_TypedContainerDto()
{
var container = new TypedContainerDto {
Source = text,
Destination = image
};
var json = container.ToJson();
var fromJson = json.FromJson<TypedContainerDto>();
Assert.That(container.Source.Action.ElementId, Is.EqualTo(fromJson.Source.Action.ElementId));
var imgContent = container.Destination.Content.FromJson<ElementContentDto>();
var fromContent = fromJson.Destination.Content.FromJson<ElementContentDto>();
Assert.That(imgContent.ElementId, Is.EqualTo(fromContent.ElementId));
}
[Test]
public void Can_DeSerialize_TypedContainerDto_with_JsonObject()
{
var container = new TypedContainerDto {
Source = text,
Destination = image
};
var json = container.ToJson();
var fromText = JsonObject.Parse(json).Get<TextElementDto>("Source");
Assert.That(container.Source.Action.ElementId, Is.EqualTo(fromText.Action.ElementId));
}
[Test]
public void Can_DeSerialize_TypedContainerDto_into_JsonValueContainerDto()
{
var container = new TypedContainerDto {
Source = text,
Destination = image
};
var json = container.ToJson();
var fromJson = json.FromJson<JsonValueContainerDto>();
var fromText = fromJson.Source.As<TextElementDto>();
var fromImage = fromJson.Destination.As<ImageElementDto>();
Assert.That(container.Source.Action.ElementId, Is.EqualTo(fromText.Action.ElementId));
Assert.That(container.Destination.ElementId, Is.EqualTo(fromImage.ElementId));
Assert.That(container.Destination.Action, Is.EqualTo(fromImage.Action));
Assert.That(container.Destination.Content, Is.EqualTo(fromImage.Content));
}
[Test]
public void Can_Serialize_StringContainerDto()
{
var container = new StringContainerDto {
Source = text.ToJson(),
Destination = image.ToJson()
};
var json = container.ToJson();
var fromJson = json.FromJson<StringContainerDto>();
var src = container.Source.FromJson<TextElementDto>();
var dst = container.Destination.FromJson<ImageElementDto>();
var fromSrc = fromJson.Source.FromJson<TextElementDto>();
var fromDst = fromJson.Destination.FromJson<ImageElementDto>();
Assert.That(src.Action.ElementId, Is.EqualTo(fromSrc.Action.ElementId));
Assert.That(dst.Action, Is.EqualTo(fromDst.Action));
}
[Test]
public void Can_Serialize_NestedJsonValueDto()
{
var scaffold = new List<JsonValue> { new JsonValue(text.ToJson()), new JsonValue(text.ToJson()) };
var container = new NestedJsonValueDto
{
ElementId = "container_1",
ElementType = "container",
// Raw nesting - won't be escaped
Content = new ElementContentDto { ElementId = "container_1", Content = "container goes here" },
Action = new ElementActionDto { ElementId = "container_1", Action = "action goes here" },
Scaffolding = scaffold
};
var json = container.ToJson();
var fromJson = json.FromJson<NestedJsonValueDto>();
foreach (var jsonValue in fromJson.Scaffolding)
{
var fromJsonValue = jsonValue.As<TextElementDto>();
Assert.That(fromJsonValue.ElementId, Is.EqualTo(text.ElementId));
Assert.That(fromJsonValue.Action.ElementId, Is.EqualTo(text.Action.ElementId));
Assert.That(fromJsonValue.Content.ElementId, Is.EqualTo(text.Content.ElementId));
}
}
[Test]
public void NullItemInCollectionShouldBeHandledGracefullyEvenIfShouldSerializeMethodExists()
{
var store = new ParentDto();
store.ChildDtosWithShouldSerialize = new List<ChildDtoWithShouldSerialize>() { new ChildDtoWithShouldSerialize() { Data = "xx" }, null };
var jj = JsonSerializer.SerializeToString(store);
Assert.AreEqual(jj, "{\"ChildDtosWithShouldSerialize\":[{\"Data\":\"xx\"},{}]}");
}
[Test]
public void NullItemInCollectionShouldBeHandledGracefullyEvenIfShouldSerializeForPropertyNameMethodExists()
{
var store = new ParentDto();
store.ChildDtosWithShouldSerializeProperty = new List<ChildDtoWithShouldSerializeForProperty>() { new ChildDtoWithShouldSerializeForProperty() { Data = "xx" }, null };
var jj = JsonSerializer.SerializeToString(store);
Assert.AreEqual(jj, "{\"ChildDtosWithShouldSerializeProperty\":[{\"Data\":\"xx\"},{}]}");
}
public class ParentDto
{
public IList<ChildDtoWithShouldSerialize> ChildDtosWithShouldSerialize { get; set; }
public IList<ChildDtoWithShouldSerializeForProperty> ChildDtosWithShouldSerializeProperty { get; set; }
}
public class ChildDtoWithShouldSerialize
{
protected virtual bool? ShouldSerialize(string fieldName)
{
return true;
}
public string Data { get; set; }
}
public class ChildDtoWithShouldSerializeForProperty
{
public virtual bool ShouldSerializeData()
{
return true;
}
public string Data { get; set; }
}
public class SimpleObj
{
public string value1 { get; set; }
public string value2 { get; set; }
}
public class NestedSimpleJsonValue
{
public JsonValue Simple { get; set; }
}
public class NestedJsonValueDto
{
public string ElementType { get; set; }
public string ElementId { get; set; }
public ElementContentDto Content { get; set; }
public ElementActionDto Action { get; set; }
public List<JsonValue> Scaffolding { get; set; }
}
public class TypedContainerDto
{
public TextElementDto Source { get; set; }
public ImageElementDto Destination { get; set; }
}
// DTOs
public class StringContainerDto // This is the request dto
{
public string Source { get; set; } // This will be some ElementDto
public string Destination { get; set; } // This will be some ElementDto
}
// DTOs
public class JsonValueContainerDto // This is the request dto
{
public JsonValue Source { get; set; } // This will be some ElementDto
public JsonValue Destination { get; set; } // This will be some ElementDto
}
public class TextElementDto
{
public string ElementType { get; set; }
public string ElementId { get; set; }
public ElementContentDto Content { get; set; }
public ElementActionDto Action { get; set; }
}
public class ImageElementDto
{
public string ElementType { get; set; }
public string ElementId { get; set; }
public string Content { get; set; }
public string Action { get; set; }
}
public class ElementContentDto
{
public string ElementId { get; set; }
public string Content { get; set; }
// There can be more nested objects in here
}
public class ElementActionDto
{
public string ElementId { get; set; }
public string Action { get; set; }
// There can be more nested objects in here
}
}
}