This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 632
/
DictionaryTests.cs
394 lines (331 loc) · 12.5 KB
/
DictionaryTests.cs
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
using System;
using System.Collections.Generic;
using System.Diagnostics;
using NUnit.Framework;
using ServiceStack.Text.Tests.DynamicModels.DataModel;
namespace ServiceStack.Text.Tests
{
[TestFixture]
public class DictionaryTests
: TestBase
{
[TestFixtureSetUp]
public void SetUp()
{
#if MONOTOUCH
JsConfig.RegisterTypeForAot<Dictionary<string, int>> ();
JsConfig.RegisterTypeForAot<KeyValuePair<int, string>> ();
JsConfig.RegisterTypeForAot<KeyValuePair<string, int>> ();
JsConfig.RegisterTypeForAot<Dictionary<string, int>> ();
JsConfig.RegisterTypeForAot<KeyValuePair<string, Dictionary<string, int>>> ();
JsConfig.RegisterTypeForAot<Dictionary<string, Dictionary<string, int>>> ();
JsConfig.RegisterTypeForAot<KeyValuePair<int, Dictionary<string, int>>> ();
JsConfig.RegisterTypeForAot<Dictionary<int, Dictionary<string, int>>> ();
#endif
}
[TearDown]
public void TearDown()
{
JsConfig.Reset();
}
[Test]
public void Can_serialize_one_level_dictionary()
{
var map = new Dictionary<string, int>
{
{"One", 1}, {"Two", 2}, {"Three", 3},
};
Serialize(map);
}
[Test]
public void Can_serialize_empty_map()
{
var emptyMap = new Dictionary<string, int>();
Serialize(emptyMap);
}
[Test]
public void Can_serialize_empty_string_map()
{
var emptyMap = new Dictionary<string, string>();
Serialize(emptyMap);
}
[Test]
public void Can_serialize_two_level_dictionary()
{
var map = new Dictionary<string, Dictionary<string, int>>
{
{"map1", new Dictionary<string, int>
{
{"One", 1}, {"Two", 2}, {"Three", 3},
}
},
{"map2", new Dictionary<string, int>
{
{"Four", 4}, {"Five", 5}, {"Six", 6},
}
},
};
Serialize(map);
}
[Test]
public void Can_serialize_two_level_dictionary_with_int_key()
{
var map = new Dictionary<int, Dictionary<string, int>>
{
{1, new Dictionary<string, int>
{
{"One", 1}, {"Two", 2}, {"Three", 3},
}
},
{2, new Dictionary<string, int>
{
{"Four", 4}, {"Five", 5}, {"Six", 6},
}
},
};
Serialize(map);
}
[Test]
public void Can_deserialize_two_level_dictionary_with_array()
{
JsConfig.TryToParsePrimitiveTypeValues = true;
JsConfig.ConvertObjectTypesIntoStringDictionary = true;
var original = new Dictionary<string, StrictType[]>
{
{"array",
new [] {
new StrictType { Name = "First" },
new StrictType { Name = "Second" },
new StrictType { Name = "Third" },
}
},
};
var json = JsonSerializer.SerializeToString(original);
var deserialized = JsonSerializer.DeserializeFromString<Dictionary<string, object>>(json);
Console.WriteLine(json);
Assert.That(deserialized, Is.Not.Null);
Assert.That(deserialized["array"], Is.Not.Null);
Assert.That(((List<object>)deserialized["array"]).Count, Is.EqualTo(3));
Assert.That(((List<object>)deserialized["array"])[0].ToJson(), Is.EqualTo("{\"Name\":\"First\"}"));
Assert.That(((List<object>)deserialized["array"])[1].ToJson(), Is.EqualTo("{\"Name\":\"Second\"}"));
Assert.That(((List<object>)deserialized["array"])[2].ToJson(), Is.EqualTo("{\"Name\":\"Third\"}"));
}
[Test]
public void Can_deserialize_dictionary_with_special_characters_in_strings()
{
JsConfig.TryToParsePrimitiveTypeValues = true;
JsConfig.ConvertObjectTypesIntoStringDictionary = true;
var original = new Dictionary<string, string>
{
{"embeddedtypecharacters", "{{body}}"},
{"embeddedlistcharacters", "[stuff]"},
{"ShortDateTimeFormat", "yyyy-MM-dd"},
{"DefaultDateTimeFormat", "dd/MM/yyyy HH:mm:ss"},
{"DefaultDateTimeFormatWithFraction", "dd/MM/yyyy HH:mm:ss.fff"},
{"XsdDateTimeFormat", "yyyy-MM-ddTHH:mm:ss.fffffffZ"},
{"XsdDateTimeFormat3F", "yyyy-MM-ddTHH:mm:ss.fffZ"},
{"XsdDateTimeFormatSeconds", "yyyy-MM-ddTHH:mm:ssZ"},
{"ShouldBeAZeroInAString", "0"},
{"ShouldBeAPositiveIntegerInAString", "12345"},
{"ShouldBeANegativeIntegerInAString", "-12345"},
};
var json = JsonSerializer.SerializeToString(original);
var deserialized = JsonSerializer.DeserializeFromString<Dictionary<string, object>>(json);
Console.WriteLine(json);
Assert.That(deserialized, Is.Not.Null);
Assert.That(deserialized["embeddedtypecharacters"], Is.Not.Null);
Assert.That(deserialized["embeddedtypecharacters"], Is.EqualTo("{{body}}"));
Assert.That(deserialized["embeddedlistcharacters"], Is.EqualTo("[stuff]"));
Assert.That(deserialized["ShortDateTimeFormat"], Is.EqualTo("yyyy-MM-dd"));
Assert.That(deserialized["DefaultDateTimeFormat"], Is.EqualTo("dd/MM/yyyy HH:mm:ss"));
Assert.That(deserialized["DefaultDateTimeFormatWithFraction"], Is.EqualTo("dd/MM/yyyy HH:mm:ss.fff"));
Assert.That(deserialized["XsdDateTimeFormat"], Is.EqualTo("yyyy-MM-ddTHH:mm:ss.fffffffZ"));
Assert.That(deserialized["XsdDateTimeFormat3F"], Is.EqualTo("yyyy-MM-ddTHH:mm:ss.fffZ"));
Assert.That(deserialized["XsdDateTimeFormatSeconds"], Is.EqualTo("yyyy-MM-ddTHH:mm:ssZ"));
Assert.That(deserialized["ShouldBeAZeroInAString"], Is.EqualTo("0"));
Assert.That(deserialized["ShouldBeAZeroInAString"], Is.InstanceOf<string>());
Assert.That(deserialized["ShouldBeAPositiveIntegerInAString"], Is.EqualTo("12345"));
Assert.That(deserialized["ShouldBeAPositiveIntegerInAString"], Is.InstanceOf<string>());
Assert.That(deserialized["ShouldBeANegativeIntegerInAString"], Is.EqualTo("-12345"));
}
private static Dictionary<string, object> SetupDict()
{
return new Dictionary<string, object> {
{ "a", "text" },
{ "b", 32 },
{ "c", false },
{ "d", new[] {1, 2, 3} }
};
}
public class MixType
{
public string a { get; set; }
public int b { get; set; }
public bool c { get; set; }
public int[] d { get; set; }
}
private static void AssertDict(Dictionary<string, object> dict)
{
Assert.AreEqual("text", dict["a"]);
Assert.AreEqual(32, dict["b"]);
Assert.AreEqual(false, dict["c"]);
}
//[Test]
//public void Test_JsonNet()
//{
// var dict = SetupDict();
// var json = JsonConvert.SerializeObject(dict);
// var deserializedDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
// AssertDict(deserializedDict);
//}
[Test]
public void Test_ServiceStack_Text_TypeSerializer()
{
JsConfig.TryToParsePrimitiveTypeValues = true;
JsConfig.ConvertObjectTypesIntoStringDictionary = true;
var dict = SetupDict();
var json = TypeSerializer.SerializeToString(dict);
var deserializedDict = TypeSerializer.DeserializeFromString<Dictionary<string, object>>(json);
AssertDict(deserializedDict);
}
[Test]
public void Test_ServiceStack_Text_JsonSerializer()
{
JsConfig.TryToParsePrimitiveTypeValues = true;
JsConfig.ConvertObjectTypesIntoStringDictionary = true;
var dict = SetupDict();
var json = JsonSerializer.SerializeToString(dict);
var deserializedDict = JsonSerializer.DeserializeFromString<Dictionary<string, object>>(json);
AssertDict(deserializedDict);
}
[Test]
public void Test_ServiceStack_Text_JsonSerializer_Array_Value_Deserializes_Correctly()
{
JsConfig.TryToParsePrimitiveTypeValues = true;
JsConfig.ConvertObjectTypesIntoStringDictionary = true;
var dict = SetupDict();
var json = JsonSerializer.SerializeToString(dict);
var deserializedDict = JsonSerializer.DeserializeFromString<Dictionary<string, object>>(json);
Assert.AreEqual("text", deserializedDict["a"]);
Assert.AreEqual(new List<int> {1, 2, 3}, deserializedDict["d"]);
}
[Test]
public void Can_deserialize_mixed_dictionary_into_strongtyped_map()
{
var mixedMap = SetupDict();
var json = JsonSerializer.SerializeToString(mixedMap);
Console.WriteLine("JSON:\n" + json);
var mixedType = json.FromJson<MixType>();
Assert.AreEqual("text", mixedType.a);
Assert.AreEqual(32, mixedType.b);
Assert.AreEqual(false, mixedType.c);
Assert.AreEqual(new[] {1, 2, 3}, mixedType.d);
}
[Test]
public void Can_serialise_null_values_from_dictionary_correctly()
{
JsConfig.IncludeNullValues = true;
var dictionary = new Dictionary<string, object> { { "value", null } };
Serialize(dictionary, includeXml: false);
var json = JsonSerializer.SerializeToString(dictionary);
Log(json);
Assert.That(json, Is.EqualTo("{\"value\":null}"));
JsConfig.Reset();
}
[Test]
public void Will_ignore_null_values_from_dictionary_correctly()
{
JsConfig.IncludeNullValues = false;
var dictionary = new Dictionary<string, string> { { "value", null } };
Serialize(dictionary, includeXml: false);
var json = JsonSerializer.SerializeToString(dictionary);
Log(json);
Assert.That(json, Is.EqualTo("{}"));
JsConfig.Reset();
}
public class FooSlash
{
public Dictionary<string, string> Nested { get; set; }
public string Bar { get; set; }
}
[Test]
public void Can_serialize_Dictionary_with_end_slash()
{
var foo = new FooSlash {
Nested = new Dictionary<string, string> { { "key", "value\"" } },
Bar = "BarValue"
};
Serialize(foo);
}
[Test]
public void Can_serialise_null_values_from_nested_dictionary_correctly()
{
JsConfig.IncludeNullValues = true;
var foo = new FooSlash();
var json = JsonSerializer.SerializeToString(foo);
Assert.That(json, Is.EqualTo("{\"Nested\":null,\"Bar\":null}"));
JsConfig.Reset();
}
[Test]
public void Can_serialize_Dictionary_with_quotes()
{
var dto = new Dictionary<string, string> { { "title", "\"test\"" } };
var to = Serialize(dto);
Assert.That(to["title"], Is.EqualTo(dto["title"]));
}
[Test]
public void Can_serialize_Dictionary_with_escaped_symbols_in_key()
{
var dto = new Dictionary<string, string> { { @"a\fb", "\"test\"" } };
var to = Serialize(dto);
Assert.That(to.Keys.ToArray()[0], Is.EqualTo(@"a\fb"));
}
[Test]
public void Can_serialize_Dictionary_with_escaped_symbols_in_key_and_binary_value()
{
var dto = new Dictionary<string, byte[]> { { @"a\fb", new byte[]{1} } };
var to = Serialize(dto);
Assert.That(to.Keys.ToArray()[0], Is.EqualTo(@"a\fb"));
}
[Test]
public void Can_serialize_Dictionary_with_int_key_and_string_with_quote()
{
var dto = new Dictionary<int, string> { { 1, @"a""b" } };
var to = Serialize(dto);
Assert.That(to.Keys.ToArray()[0], Is.EqualTo(1));
Assert.That(to[1], Is.EqualTo(@"a""b"));
}
[Test]
public void Can_serialize_string_byte_Dictionary_with_UTF8()
{
var dto = new Dictionary<string, byte[]> { { "aфаž\"a", new byte[] { 1 } } };
var to = Serialize(dto);
Assert.That(to.Keys.ToArray()[0], Is.EqualTo( "aфаž\"a"));
}
[Test]
public void Can_serialize_string_string_Dictionary_with_UTF8()
{
var dto = new Dictionary<string, string> { { "aфаž\"a", "abc" } };
var to = Serialize(dto);
Assert.That(to.Keys.ToArray()[0], Is.EqualTo("aфаž\"a"));
}
[Test]
public void Can_Deserialize_Object_To_Dictionary()
{
const string json = "{\"Id\":1}";
var d = json.To<Dictionary<string, string>>();
Assert.That(d.ContainsKey("Id"));
Assert.That(d["Id"], Is.EqualTo("1"));
}
#if NET40
[Test]
public void Nongeneric_implementors_of_IDictionary_K_V_Should_serialize_like_Dictionary_K_V()
{
dynamic expando = new System.Dynamic.ExpandoObject();
expando.Property = "Value";
IDictionary<string, object> dict = expando;
Assert.AreEqual(dict.Dump(), new Dictionary<string, object>(dict).Dump());
}
#endif
}
}