-
-
Notifications
You must be signed in to change notification settings - Fork 405
/
Copy pathJsonMapperTest.cs
1138 lines (915 loc) · 32.9 KB
/
JsonMapperTest.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region Header
/**
* JsonMapperTest.cs
* Tests for the JsonMapper class.
*
* The authors disclaim copyright to this source code. For more details, see
* the COPYING file included with this distribution.
**/
#endregion
using LitJson;
using NUnit.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
namespace LitJson.Test
{
// Sample classes to test json->object and object->json conversions
public enum Planets
{
Jupiter,
Saturn,
Uranus,
Neptune,
Pluto
}
[Flags]
public enum Instruments
{
Bass = 1,
Guitar = 2,
Drums = 4,
Harp = 8
}
public class EnumsTest
{
public Planets FavouritePlanet;
public Instruments Band;
}
public class NestedArrays
{
public int[][] numbers;
public List<List<List<string>>> strings;
}
public class PropertyReadOnly
{
private int x;
public int X {
get { return x; }
set { x = value; }
}
public int Y {
get { return x * 2; }
}
}
public class PropertyWriteOnly
{
private int x;
public int X {
set { x = value; }
}
public int GetX ()
{
return x;
}
}
public class UiImage
{
public string src;
public string name;
public int hOffset;
public int vOffset;
public string alignment;
}
public class UiSample
{
private UiWidget _widget;
public UiWidget widget {
get { return _widget; }
set { _widget = value; }
}
public UiSample ()
{
_widget = new UiWidget ();
}
}
public class UiText
{
public string data;
public int size;
public string style;
public string name;
public int hOffset;
public int vOffset;
public string alignment;
public string onMouseUp;
}
public class UiWidget
{
private UiImage _image;
private UiText _text;
private UiWindow _window;
public bool debug;
public UiWindow window {
get { return _window; }
set { _window = value; }
}
public UiImage image {
get { return _image; }
set { _image = value; }
}
public UiText text {
get { return _text; }
set { _text = value; }
}
public UiWidget ()
{
_image = new UiImage ();
_text = new UiText ();
_window = new UiWindow ();
}
}
public class UiWindow
{
public string title;
public string name;
public int width;
public int height;
}
public class ValueTypesTest
{
public byte TestByte;
public char TestChar;
public DateTime TestDateTime;
public decimal TestDecimal;
public sbyte TestSByte;
public short TestShort;
public ushort TestUShort;
public uint TestUInt;
public ulong TestULong;
public DateTimeOffset TestDateTimeOffset;
}
public class NullableTypesTest
{
public int? TestNullableInt;
}
public class CompoundNullableTypesTest
{
public CompoundNullableType<int>? TestNested;
public CompoundNullableType<int>?[] TestNullableTypeArray;
}
public struct CompoundNullableType<T>
{
public T TestValue;
}
public enum NullableEnum
{
TestVal0 = 0,
TestVal1 = 1,
TestVal2 = 2
}
public class NullableEnumTest
{
public NullableEnum? TestEnum;
}
class EmptyArrayInJsonDataTest
{
public int[] array;
public string name;
}
class EmptyArrayInJsonDataTestWrapper
{
public JsonData data = null;
}
[TestFixture]
public class JsonMapperTest
{
[Test]
public void CustomExporterTest ()
{
// Custom DateTime exporter that only uses the Year value
ExporterFunc<DateTime> exporter =
delegate (DateTime obj, JsonWriter writer) {
writer.Write (obj.Year);
};
JsonMapper.RegisterExporter<DateTime> (exporter);
OrderedDictionary sample = new OrderedDictionary ();
sample.Add ("date", new DateTime (1980, 12, 8));
string json = JsonMapper.ToJson (sample);
string expected = "{\"date\":1980}";
JsonMapper.UnregisterExporters ();
Assert.AreEqual (expected, json);
}
[Test]
public void CustomImporterTest ()
{
// Custom DateTime importer that only uses the Year value
// (assuming January 1st of that year)
ImporterFunc<int, DateTime> importer =
delegate (int obj) {
return new DateTime (obj, 1, 1);
};
JsonMapper.RegisterImporter<int, DateTime> (importer);
string json = "{ \"TestDateTime\" : 1980 }";
ValueTypesTest sample =
JsonMapper.ToObject<ValueTypesTest> (json);
JsonMapper.UnregisterImporters ();
Assert.AreEqual (new DateTime (1980, 1, 1), sample.TestDateTime);
}
[Test]
public void EmptyObjectsTest ()
{
JsonData empty_obj = JsonMapper.ToObject ("{}");
Assert.IsTrue (empty_obj.IsObject, "A1");
string empty_json = JsonMapper.ToJson (empty_obj);
Assert.AreEqual ("{}", empty_json, "A2");
JsonData empty_array = JsonMapper.ToObject ("[]");
Assert.IsTrue (empty_array.IsArray, "B1");
empty_json = JsonMapper.ToJson (empty_array);
Assert.AreEqual ("[]", empty_json, "B2");
}
[Test]
public void ExportArrayOfIntsTest ()
{
int[] numbers = new int[] { 1, 1, 2, 3, 5, 8, 13 };
string json = JsonMapper.ToJson (numbers);
Assert.AreEqual ("[1,1,2,3,5,8,13]", json);
}
[Test]
public void ExportDictionaryTest ()
{
IDictionary hash = new OrderedDictionary ();
hash.Add ("product", "ACME rocket skates");
hash.Add ("quantity", 5);
hash.Add ("price", 45.95);
string expected = "{\"product\":\"ACME rocket skates\"," +
"\"quantity\":5,\"price\":45.95}";
string json = JsonMapper.ToJson (hash);
Assert.AreEqual (expected, json);
}
[Test]
public void ExportEnumsTest ()
{
EnumsTest e_test = new EnumsTest ();
e_test.FavouritePlanet = Planets.Saturn;
e_test.Band = Instruments.Bass | Instruments.Harp;
string json = JsonMapper.ToJson (e_test);
Assert.AreEqual ("{\"FavouritePlanet\":1,\"Band\":9}", json);
}
[Test]
public void ExportEnumDictionaryTest()
{
Dictionary<Planets, int> planets = new Dictionary<Planets, int>();
planets.Add(Planets.Jupiter, 5);
planets.Add(Planets.Saturn, 6);
planets.Add(Planets.Uranus, 7);
planets.Add(Planets.Neptune, 8);
planets.Add(Planets.Pluto, 9);
string json = JsonMapper.ToJson(planets);
Assert.AreEqual("{\"Jupiter\":5,\"Saturn\":6,\"Uranus\":7,\"Neptune\":8,\"Pluto\":9}", json);
}
[Test]
public void ExportObjectTest ()
{
UiSample sample = new UiSample ();
sample.widget.window.title = "FooBar";
sample.widget.window.name = "foo_window";
sample.widget.window.width = 400;
sample.widget.window.height = 300;
sample.widget.image.src = "logo.png";
sample.widget.image.name = "Foo Logo";
sample.widget.image.hOffset = 10;
sample.widget.image.vOffset = 20;
sample.widget.image.alignment = "right";
sample.widget.text.data = "About Us";
sample.widget.text.size = 24;
sample.widget.text.style = "normal";
sample.widget.text.name = "about";
sample.widget.text.alignment = "center";
string expected = "{\"widget\":{\"window\":" +
"{\"title\":\"FooBar\",\"name\":\"foo_window\"," +
"\"width\":400,\"height\":300},\"image\":{\"src\":" +
"\"logo.png\",\"name\":\"Foo Logo\",\"hOffset\":10," +
"\"vOffset\":20,\"alignment\":\"right\"},\"text\":{" +
"\"data\":\"About Us\",\"size\":24,\"style\":\"normal\"," +
"\"name\":\"about\",\"hOffset\":0,\"vOffset\":0," +
"\"alignment\":\"center\",\"onMouseUp\":null}," +
"\"debug\":false}}";
string json = JsonMapper.ToJson (sample);
Assert.AreEqual (expected, json);
}
[Test]
public void ExportPrettyPrint ()
{
OrderedDictionary sample = new OrderedDictionary ();
sample["rolling"] = "stones";
sample["flaming"] = "pie";
sample["nine"] = 9;
string expected =string.Join(
Environment.NewLine,
new [] {
"",
"{",
" \"rolling\" : \"stones\",",
" \"flaming\" : \"pie\",",
" \"nine\" : 9",
"}"});
JsonWriter writer = new JsonWriter ();
writer.PrettyPrint = true;
JsonMapper.ToJson (sample, writer);
Assert.AreEqual (expected, writer.ToString (), "A1");
writer.Reset ();
writer.IndentValue = 8;
expected = string.Join(
Environment.NewLine,
new [] {
"",
"{",
" \"rolling\" : \"stones\",",
" \"flaming\" : \"pie\",",
" \"nine\" : 9",
"}"});
JsonMapper.ToJson (sample, writer);
Assert.AreEqual (expected, writer.ToString (), "A2");
}
[Test]
public void ExportValueTypesTest ()
{
ValueTypesTest test = new ValueTypesTest ();
test.TestByte = 200;
test.TestChar = 'P';
test.TestDateTime = new DateTime (2012, 12, 22);
test.TestDecimal = 10.333m;
test.TestSByte = -5;
test.TestShort = 1024;
test.TestUShort = 30000;
test.TestUInt = 90000000;
test.TestULong = 0xFFFFFFFFFFFFFFFF; // = =18446744073709551615
test.TestDateTimeOffset =
new DateTimeOffset(2019, 9, 18, 16, 47,
50, 123, TimeSpan.FromHours(8)).AddTicks(4567);
string json = JsonMapper.ToJson (test);
string expected =
"{\"TestByte\":200,\"TestChar\":\"P\",\"TestDateTime\":" +
"\"12/22/2012 00:00:00\",\"TestDecimal\":10.333," +
"\"TestSByte\":-5,\"TestShort\":1024,\"TestUShort\":30000" +
",\"TestUInt\":90000000,\"TestULong\":18446744073709551615" +
",\"TestDateTimeOffset\":\"2019-09-18T16:47:50.1234567+08:00\"}";
Assert.AreEqual (expected, json);
}
[Test]
public void ImportArrayOfStringsTest ()
{
string json = @"[
""Adam"",
""Danny"",
""James"",
""Justin""
]";
string[] names = JsonMapper.ToObject<string[]> (json);
Assert.IsTrue (names.Length == 4, "A1");
Assert.AreEqual (names[1], "Danny", "A2");
}
[Test]
public void ImportEnumsTest ()
{
string json = @"
{
""FavouritePlanet"" : 4,
""Band"" : 6
}";
EnumsTest e_test = JsonMapper.ToObject<EnumsTest> (json);
Assert.AreEqual (Planets.Pluto, e_test.FavouritePlanet, "A1");
Assert.AreEqual (Instruments.Guitar
| Instruments.Drums, e_test.Band, "A2");
}
[Test]
public void ImportExtendedGrammarTest ()
{
string json = @"
{
// The domain name
""domain"" : ""example.com"",
/******************
* The IP address *
******************/
'ip_address' : '127.0.0.1'
}
";
JsonData data = JsonMapper.ToObject (json);
Assert.AreEqual ("example.com", (string) data["domain"], "A1");
Assert.AreEqual ("127.0.0.1", (string) data["ip_address"], "A2");
}
[Test]
public void ImportFromFileTest ()
{
JsonData data;
Assembly asmb = typeof (JsonMapperTest).Assembly;
StreamReader stream = new StreamReader (
asmb.GetManifestResourceStream (asmb.GetName().Name + ".json-example.txt"));
using (stream) {
data = JsonMapper.ToObject (stream);
}
Assert.AreEqual (
"cofaxCDS",
(string) data["web-app"]["servlet"][0]["servlet-name"],
"A1");
Assert.AreEqual (
false,
(bool) data["web-app"]["servlet"][0]["init-param"]["useJSP"],
"A2");
Assert.AreEqual (
"cofax.tld",
(string) data["web-app"]["taglib"]["taglib-uri"],
"A1");
}
[Test]
public void ImportJsonDataArrayTest ()
{
string json = " [ 1, 10, 100, 1000 ] ";
JsonData data = JsonMapper.ToObject (json);
Assert.AreEqual (4, data.Count, "A1");
Assert.AreEqual (1000, (int) data[3], "A2");
}
[Test]
public void ImportManyJsonTextPiecesTest ()
{
string json_arrays = @"
[ true, true, false, false ]
[ 10, 0, -10 ]
[ ""war is over"", ""if you want it"" ]
";
JsonReader reader;
JsonData arrays;
reader = new JsonReader (json_arrays);
arrays = JsonMapper.ToObject (reader);
Assert.IsFalse (reader.EndOfInput, "A1");
Assert.IsTrue (arrays.IsArray, "A2");
Assert.AreEqual (4, arrays.Count, "A3");
Assert.AreEqual (true, (bool) arrays[0], "A4");
arrays = JsonMapper.ToObject (reader);
Assert.IsFalse (reader.EndOfInput, "A5");
Assert.IsTrue (arrays.IsArray, "A6");
Assert.AreEqual (3, arrays.Count, "A7");
Assert.AreEqual (10, (int) arrays[0], "A8");
arrays = JsonMapper.ToObject (reader);
Assert.IsTrue (arrays.IsArray, "A9");
Assert.AreEqual (2, arrays.Count, "A10");
Assert.AreEqual ("war is over", (string) arrays[0], "A11");
reader.Close ();
string json_objects = @"
{
""title"" : ""First"",
""name"" : ""First Window"",
""width"" : 640,
""height"" : 480
}
{
""title"" : ""Second"",
""name"" : ""Second Window"",
""width"" : 800,
""height"" : 600
}
";
reader = new JsonReader (json_objects);
UiWindow window;
window = JsonMapper.ToObject<UiWindow> (reader);
Assert.IsFalse (reader.EndOfInput, "A12");
Assert.AreEqual ("First", window.title, "A13");
Assert.AreEqual (640, window.width, "A14");
window = JsonMapper.ToObject<UiWindow> (reader);
Assert.AreEqual ("Second", window.title, "A15");
Assert.AreEqual (800, window.width, "A16");
reader.Close ();
// Read them in a loop to make sure we get the correct number of
// iterations
reader = new JsonReader (json_objects);
int i = 0;
while (! reader.EndOfInput) {
window = JsonMapper.ToObject<UiWindow> (reader);
i++;
}
Assert.AreEqual (2, i, "A17");
}
[Test]
public void ImportNestedArrays ()
{
string json = "[ [ [ 42 ] ] ]";
JsonData data = JsonMapper.ToObject (json);
Assert.IsTrue (data.IsArray, "A1");
Assert.AreEqual (1, data.Count, "A2");
Assert.IsTrue (data[0].IsArray, "A3");
Assert.AreEqual (1, data[0].Count, "A4");
Assert.IsTrue (data[0][0].IsArray, "A5");
Assert.AreEqual (1, data[0][0].Count, "A6");
Assert.AreEqual (42, (int) data[0][0][0], "A7");
Assert.AreEqual ("[[[42]]]", data.ToJson(), "A8");
json = " [ [ 10, 20, 30 ], \"hi\", [ null, null ] ] ";
data = JsonMapper.ToObject (json);
Assert.IsTrue (data.IsArray, "B1");
Assert.AreEqual (3, data.Count, "B2");
Assert.AreEqual (20, (int) data[0][1], "B3");
Assert.AreEqual ("hi", (string) data[1], "B4");
Assert.IsTrue (data[2].IsArray, "B5");
Assert.AreEqual (2, data[2].Count, "B6");
Assert.IsNull (data[2][0], "B7");
Assert.IsNull (data[2][1], "B8");
json = @"{
""numbers"" : [ [ 0, 1, 2 ], [], [ 2, 3, 5, 7, 11 ] ],
""strings"" : [
[ [ ""abc"", ""def"" ], [ ""hi there"" ], null ],
[ [ ""Bob Marley is in the house"" ] ]
]
}";
var obj = JsonMapper.ToObject<NestedArrays> (json);
Assert.IsNotNull (obj, "C1");
Assert.AreEqual (2, obj.numbers[0][2], "C2");
Assert.AreEqual (0, obj.numbers[1].Length, "C3");
Assert.AreEqual (5, obj.numbers[2].Length, "C4");
Assert.AreEqual (11, obj.numbers[2][4], "C5");
Assert.AreEqual ("abc", obj.strings[0][0][0], "C6");
Assert.AreEqual ("hi there", obj.strings[0][1][0], "C7");
Assert.IsNull (obj.strings[0][2], "C8");
Assert.AreEqual (1, obj.strings[1][0].Count, "C9");
}
[Test]
public void ImportNumbersTest ()
{
double[] d_array;
float[] f_array;
decimal[] m_array;
string json = " [ 0, 5, 10 ] ";
d_array = JsonMapper.ToObject<double[]> (json);
Assert.AreEqual (3, d_array.Length, "A1");
Assert.AreEqual (10.0, d_array[2], "A2");
f_array = JsonMapper.ToObject<float[]> (json);
Assert.AreEqual (3, f_array.Length, "A3");
Assert.AreEqual (10.0, f_array[2], "A4");
m_array = JsonMapper.ToObject<decimal[]> (json);
Assert.AreEqual (3, m_array.Length, "A5");
Assert.AreEqual (10m, m_array[2], "A6");
}
[Test]
public void ImportObjectTest ()
{
string json = @"
{
""widget"": {
""debug"": true,
""window"": {
""title"": ""Sample Widget"",
""name"": ""main_window"",
""width"": 500,
""height"": 500
},
""image"": {
""src"": ""Images/Sun.png"",
""name"": ""sun1"",
""hOffset"": 250,
""vOffset"": 250,
""alignment"": ""center""
},
""text"": {
""data"": ""Click Here"",
""size"": 36,
""style"": ""bold"",
""name"": ""text1"",
""hOffset"": 250,
""vOffset"": 100,
""alignment"": ""center"",
""onMouseUp"": ""sun1.opacity = (sun1.opacity / 100) * 90;""
}
}
}";
UiSample sample = JsonMapper.ToObject<UiSample> (json);
Assert.IsNotNull (sample, "A1");
Assert.AreEqual ("Sample Widget", sample.widget.window.title,
"A2");
Assert.AreEqual (500, sample.widget.window.width, "A3");
Assert.AreEqual ("sun1", sample.widget.image.name, "A4");
Assert.AreEqual ("Click Here", sample.widget.text.data, "A5");
}
[Test]
public void ImportObjectSkipNonMembersTest()
{
string json = @"
{
""title"" : ""First"",
""extra_bool"": false,
""extra_object"": {
""title"" : ""Sample Widget"",
""name"" : ""main_window"",
""width"" : 500,
""height"" : 500
},
""name"" : ""First Window"",
""extra_array"" :[1, 2, 3],
""width"" : 640,
""extra_array_object"" : [
{
""obj1"": { ""checked"": false },
""obj2"": [ 7, 6, 5 ]
},
{
""member1"": false,
""member2"": true,
""member3"": -1,
""member4"": ""vars2"",
""member5"": [9, 8, 7],
""member6"": { ""checked"": true }
}
],
""height"" : 480
}";
UiWindow window = JsonMapper.ToObject<UiWindow>(json);
Assert.IsNotNull(window, "A1");
Assert.AreEqual("First", window.title, "A2");
Assert.AreEqual("First Window", window.name, "A3");
Assert.AreEqual(640, window.width, "A4");
Assert.AreEqual(480, window.height, "A5");
}
[Test]
public void ImportObjectNonMembersTest()
{
string json = @"
{
""title"" : ""First"",
""extra_string"": ""Hello world"",
""name"" : ""First Window"",
""width"" : 640,
""height"" : 480
}";
JsonReader reader = new JsonReader(json);
reader.SkipNonMembers = false;
Assert.Throws<JsonException>(() => {
UiWindow window = JsonMapper.ToObject<UiWindow>(reader);
window.title = "Unreachable";
});
}
[Test]
public void ImportStrictCommentsTest ()
{
string json = @"
[
/* This is a comment */
1,
2,
3
]";
JsonReader reader = new JsonReader (json);
reader.AllowComments = false;
Assert.Throws<JsonException>(() => {
JsonData data = JsonMapper.ToObject (reader);
if (data.Count != 3)
data = JsonMapper.ToObject (reader);
});
}
[Test]
public void ImportStrictStringsTest ()
{
string json = "[ 'Look! Single quotes' ]";
JsonReader reader = new JsonReader (json);
reader.AllowSingleQuotedStrings = false;
Assert.Throws<JsonException>(() => {
JsonData data = JsonMapper.ToObject (reader);
if (data[0] == null)
data = JsonMapper.ToObject (reader);
});
}
[Test]
public void ImportValueTypesTest ()
{
string json = @"
{
""TestByte"": 200,
""TestChar"": 'P',
""TestDateTime"": ""12/22/2012 00:00:00"",
""TestDecimal"": 10.333,
""TestSByte"": -5,
""TestShort"": 1024,
""TestUShort"": 30000,
""TestUInt"": 90000000,
""TestULong"": 18446744073709551615,
""TestDateTimeOffset"": ""2019-09-18T16:47:50.1234567+08:00""
}";
ValueTypesTest test = JsonMapper.ToObject<ValueTypesTest> (json);
Assert.AreEqual (200, test.TestByte, "A1");
Assert.AreEqual ('P', test.TestChar, "A2");
Assert.AreEqual (new DateTime (2012, 12, 22),
test.TestDateTime, "A3");
Assert.AreEqual (10.333m, test.TestDecimal, "A4");
Assert.AreEqual (-5, test.TestSByte, "A5");
Assert.AreEqual (1024, test.TestShort, "A6");
Assert.AreEqual (30000, test.TestUShort, "A7");
Assert.AreEqual (90000000, test.TestUInt, "A8");
Assert.AreEqual (18446744073709551615L, test.TestULong, "A9");
Assert.AreEqual(
new DateTimeOffset(2019, 9, 18, 16, 47,
50, 123, TimeSpan.FromHours(8)).AddTicks(4567),
test.TestDateTimeOffset, "A10");
}
[Test]
public void NullConversionsTest ()
{
object[] MyObjects = new object[] {"Hi!", 123, true, null};
string json = JsonMapper.ToJson (MyObjects);
Assert.AreEqual ("[\"Hi!\",123,true,null]", json, "A1");
JsonData data = JsonMapper.ToObject (json);
Assert.AreEqual ("Hi!", (string) data[0], "A2");
Assert.AreEqual (123, (int) data[1], "A3");
Assert.IsTrue ((bool) data[2], "A4");
Assert.IsNull (data[3], "A5");
}
[Test]
public void PropertiesReadOnlyTest ()
{
PropertyReadOnly p_obj = new PropertyReadOnly ();
p_obj.X = 10;
string json = JsonMapper.ToJson (p_obj);
Assert.AreEqual ("{\"X\":10,\"Y\":20}", json, "A1");
PropertyReadOnly p_obj2 =
JsonMapper.ToObject<PropertyReadOnly> (json);
Assert.AreEqual (10, p_obj2.X, "A2");
Assert.AreEqual (20, p_obj2.Y, "A3");
}
[Test]
public void PropertiesWriteOnlyTest ()
{
string json = " { \"X\" : 3 } ";
PropertyWriteOnly p_obj =
JsonMapper.ToObject<PropertyWriteOnly> (json);
Assert.AreEqual (3, p_obj.GetX (), "A1");
json = JsonMapper.ToJson (p_obj);
Assert.AreEqual ("{}", json, "A2");
}
[Test]
public void NullableTypesImportTest()
{
string json = @" {
""TestNullableInt"": 42
}";
var value = JsonMapper.ToObject<NullableTypesTest>(json);
Assert.AreEqual(value.TestNullableInt, 42);
json = @" {
""TestNullableInt"": null
}";
value = JsonMapper.ToObject<NullableTypesTest>(json);
Assert.AreEqual(value.TestNullableInt, null);
}
[Test]
public void NullableTypesExportTest()
{
string expectedJson = "{\"TestNullableInt\":42}";
var value = new NullableTypesTest() { TestNullableInt = 42 };
Assert.AreEqual(expectedJson, JsonMapper.ToJson(value));
expectedJson = "{\"TestNullableInt\":null}";
value = new NullableTypesTest() { TestNullableInt = null };
Assert.AreEqual(expectedJson, JsonMapper.ToJson(value));
}
[Test]
public void CompoundNullableTypesImportTest()
{
string json = @" {
""TestNested"": {
""TestValue"": 42
}
}";
JsonReader reader = new JsonReader(json);
reader.SkipNonMembers = false;
var value = JsonMapper.ToObject<CompoundNullableTypesTest>(reader);
Assert.AreNotEqual(value.TestNested, null);
var innerValue = (CompoundNullableType<int>)value.TestNested;
Assert.AreEqual(innerValue.TestValue, 42);
json = @" {
""TestNested"": null
}";
value = JsonMapper.ToObject<CompoundNullableTypesTest>(json);
Assert.AreEqual(value.TestNested, null);
json = @" {
""TestNullableTypeArray"": [
{ ""TestValue"": 42 },
{ ""TestValue"": 43 },
{ ""TestValue"": 44 }
]
}";
value = JsonMapper.ToObject<CompoundNullableTypesTest>(json);
Assert.AreNotEqual(value.TestNullableTypeArray, null);
Assert.AreEqual(value.TestNullableTypeArray.Length, 3);
Assert.AreNotEqual(value.TestNullableTypeArray[0], null);