This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathDataContractSerializer.cs
3204 lines (2751 loc) · 217 KB
/
DataContractSerializer.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using SerializationTestTypes;
using SerializationTypes;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using Xunit;
public static partial class DataContractSerializerTests
{
#if ReflectionOnly
private static readonly string SerializationOptionSetterName = "set_Option";
static DataContractSerializerTests()
{
if (!PlatformDetection.IsFullFramework)
{
MethodInfo method = typeof(DataContractSerializer).GetMethod(SerializationOptionSetterName, BindingFlags.NonPublic | BindingFlags.Static);
Assert.True(method != null, $"No method named {SerializationOptionSetterName}");
method.Invoke(null, new object[] { 1 });
}
}
#endif
[Fact]
public static void DCS_DateTimeOffsetAsRoot()
{
// Assume that UTC offset doesn't change more often than once in the day 2013-01-02
// DO NOT USE TimeZoneInfo.Local.BaseUtcOffset !
var offsetMinutes = (int)TimeZoneInfo.Local.GetUtcOffset(new DateTime(2013, 1, 2)).TotalMinutes;
var objs = new DateTimeOffset[]
{
// Adding offsetMinutes so the DateTime component in serialized strings are time-zone independent
new DateTimeOffset(new DateTime(2013, 1, 2, 3, 4, 5, 6).AddMinutes(offsetMinutes)),
new DateTimeOffset(new DateTime(2013, 1, 2, 3, 4, 5, 6, DateTimeKind.Local).AddMinutes(offsetMinutes)),
new DateTimeOffset(new DateTime(2013, 1, 2, 3, 4, 5, 6, DateTimeKind.Unspecified).AddMinutes(offsetMinutes)),
new DateTimeOffset(new DateTime(2013, 1, 2, 3, 4, 5, 6, DateTimeKind.Utc)),
new DateTimeOffset(DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc)),
new DateTimeOffset(DateTime.SpecifyKind(DateTime.MaxValue, DateTimeKind.Utc))
};
var serializedStrings = new string[]
{
string.Format(@"<DateTimeOffset xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/System""><DateTime>2013-01-02T03:04:05.006Z</DateTime><OffsetMinutes>{0}</OffsetMinutes></DateTimeOffset>", offsetMinutes),
string.Format(@"<DateTimeOffset xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/System""><DateTime>2013-01-02T03:04:05.006Z</DateTime><OffsetMinutes>{0}</OffsetMinutes></DateTimeOffset>", offsetMinutes),
string.Format(@"<DateTimeOffset xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/System""><DateTime>2013-01-02T03:04:05.006Z</DateTime><OffsetMinutes>{0}</OffsetMinutes></DateTimeOffset>", offsetMinutes),
@"<DateTimeOffset xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/System""><DateTime>2013-01-02T03:04:05.006Z</DateTime><OffsetMinutes>0</OffsetMinutes></DateTimeOffset>",
@"<DateTimeOffset xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/System""><DateTime>0001-01-01T00:00:00Z</DateTime><OffsetMinutes>0</OffsetMinutes></DateTimeOffset>",
@"<DateTimeOffset xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/System""><DateTime>9999-12-31T23:59:59.9999999Z</DateTime><OffsetMinutes>0</OffsetMinutes></DateTimeOffset>"
};
for (int i = 0; i < objs.Length; ++i)
{
Assert.StrictEqual(SerializeAndDeserialize<DateTimeOffset>(objs[i], serializedStrings[i]), objs[i]);
}
}
[Fact]
public static void DCS_BoolAsRoot()
{
Assert.StrictEqual(SerializeAndDeserialize<bool>(true, @"<boolean xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">true</boolean>"), true);
Assert.StrictEqual(SerializeAndDeserialize<bool>(false, @"<boolean xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">false</boolean>"), false);
}
[Fact]
public static void DCS_ByteArrayAsRoot()
{
Assert.Null(SerializeAndDeserialize<byte[]>(null, @"<base64Binary i:nil=""true"" xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""/>"));
byte[] x = new byte[] { 1, 2 };
byte[] y = SerializeAndDeserialize<byte[]>(x, @"<base64Binary xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">AQI=</base64Binary>");
Assert.Equal<byte>(x, y);
}
[Fact]
public static void DCS_CharAsRoot()
{
Assert.StrictEqual(SerializeAndDeserialize<char>(char.MinValue, @"<char xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">0</char>"), char.MinValue);
Assert.StrictEqual(SerializeAndDeserialize<char>(char.MaxValue, @"<char xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">65535</char>"), char.MaxValue);
Assert.StrictEqual(SerializeAndDeserialize<char>('a', @"<char xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">97</char>"), 'a');
Assert.StrictEqual(SerializeAndDeserialize<char>('ñ', @"<char xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">241</char>"), 'ñ');
Assert.StrictEqual(SerializeAndDeserialize<char>('漢', @"<char xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">28450</char>"), '漢');
}
[Fact]
public static void DCS_ByteAsRoot()
{
Assert.StrictEqual(SerializeAndDeserialize<byte>(10, @"<unsignedByte xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">10</unsignedByte>"), 10);
Assert.StrictEqual(SerializeAndDeserialize<byte>(byte.MinValue, @"<unsignedByte xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">0</unsignedByte>"), byte.MinValue);
Assert.StrictEqual(SerializeAndDeserialize<byte>(byte.MaxValue, @"<unsignedByte xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">255</unsignedByte>"), byte.MaxValue);
}
[Fact]
public static void DCS_DateTimeAsRoot()
{
var offsetMinutes = (int)TimeZoneInfo.Local.GetUtcOffset(new DateTime(2013, 1, 2)).TotalMinutes;
Assert.StrictEqual(SerializeAndDeserialize<DateTime>(new DateTime(2013, 1, 2), @"<dateTime xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">2013-01-02T00:00:00</dateTime>"), new DateTime(2013, 1, 2));
Assert.StrictEqual(SerializeAndDeserialize<DateTime>(new DateTime(2013, 1, 2, 3, 4, 5, 6, DateTimeKind.Local), string.Format(@"<dateTime xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">2013-01-02T03:04:05.006{0:+;-}{1}</dateTime>", offsetMinutes, new TimeSpan(0, offsetMinutes, 0).ToString(@"hh\:mm"))), new DateTime(2013, 1, 2, 3, 4, 5, 6, DateTimeKind.Local));
Assert.StrictEqual(SerializeAndDeserialize<DateTime>(new DateTime(2013, 1, 2, 3, 4, 5, 6, DateTimeKind.Unspecified), @"<dateTime xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">2013-01-02T03:04:05.006</dateTime>"), new DateTime(2013, 1, 2, 3, 4, 5, 6, DateTimeKind.Unspecified));
Assert.StrictEqual(SerializeAndDeserialize<DateTime>(new DateTime(2013, 1, 2, 3, 4, 5, 6, DateTimeKind.Utc), @"<dateTime xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">2013-01-02T03:04:05.006Z</dateTime>"), new DateTime(2013, 1, 2, 3, 4, 5, 6, DateTimeKind.Utc));
Assert.StrictEqual(SerializeAndDeserialize<DateTime>(DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc), @"<dateTime xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">0001-01-01T00:00:00Z</dateTime>"), DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc));
Assert.StrictEqual(SerializeAndDeserialize<DateTime>(DateTime.SpecifyKind(DateTime.MaxValue, DateTimeKind.Utc), @"<dateTime xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">9999-12-31T23:59:59.9999999Z</dateTime>"), DateTime.SpecifyKind(DateTime.MaxValue, DateTimeKind.Utc));
}
[Fact]
public static void DCS_DecimalAsRoot()
{
foreach (decimal value in new decimal[] { (decimal)-1.2, (decimal)0, (decimal)2.3, decimal.MinValue, decimal.MaxValue })
{
Assert.StrictEqual(SerializeAndDeserialize<decimal>(value, string.Format(@"<decimal xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">{0}</decimal>", value.ToString(CultureInfo.InvariantCulture))), value);
}
}
[Fact]
public static void DCS_DoubleAsRoot()
{
Assert.StrictEqual(SerializeAndDeserialize<double>(-1.2, @"<double xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">-1.2</double>"), -1.2);
Assert.StrictEqual(SerializeAndDeserialize<double>(0, @"<double xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">0</double>"), 0);
Assert.StrictEqual(SerializeAndDeserialize<double>(2.3, @"<double xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">2.3</double>"), 2.3);
Assert.StrictEqual(SerializeAndDeserialize<double>(double.MinValue, @"<double xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">-1.7976931348623157E+308</double>"), double.MinValue);
Assert.StrictEqual(SerializeAndDeserialize<double>(double.MaxValue, @"<double xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">1.7976931348623157E+308</double>"), double.MaxValue);
}
[Fact]
public static void DCS_FloatAsRoot()
{
Assert.StrictEqual(SerializeAndDeserialize<float>((float)-1.2, @"<float xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">-1.2</float>"), (float)-1.2);
Assert.StrictEqual(SerializeAndDeserialize<float>((float)0, @"<float xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">0</float>"), (float)0);
Assert.StrictEqual(SerializeAndDeserialize<float>((float)2.3, @"<float xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">2.3</float>"), (float)2.3);
Assert.StrictEqual(SerializeAndDeserialize<float>(float.MinValue, @"<float xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">-3.40282347E+38</float>"), float.MinValue);
Assert.StrictEqual(SerializeAndDeserialize<float>(float.MaxValue, @"<float xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">3.40282347E+38</float>"), float.MaxValue);
}
[Fact]
public static void DCS_GuidAsRoot()
{
foreach (Guid value in new Guid[] { Guid.NewGuid(), Guid.Empty })
{
Assert.StrictEqual(SerializeAndDeserialize<Guid>(value, string.Format(@"<guid xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">{0}</guid>", value.ToString())), value);
}
}
[Fact]
public static void DCS_IntAsRoot()
{
foreach (int value in new int[] { -1, 0, 2, int.MinValue, int.MaxValue })
{
Assert.StrictEqual(SerializeAndDeserialize<int>(value, string.Format(@"<int xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">{0}</int>", value)), value);
}
}
[Fact]
public static void DCS_LongAsRoot()
{
foreach (long value in new long[] { (long)-1, (long)0, (long)2, long.MinValue, long.MaxValue })
{
Assert.StrictEqual(SerializeAndDeserialize<long>(value, string.Format(@"<long xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">{0}</long>", value)), value);
}
}
[Fact]
public static void DCS_ObjectAsRoot()
{
Assert.StrictEqual(SerializeAndDeserialize<object>(1, @"<z:anyType i:type=""a:int"" xmlns:z=""http://schemas.microsoft.com/2003/10/Serialization/"" xmlns:a=""http://www.w3.org/2001/XMLSchema"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">1</z:anyType>"), 1);
Assert.StrictEqual(SerializeAndDeserialize<object>(true, @"<z:anyType i:type=""a:boolean"" xmlns:z=""http://schemas.microsoft.com/2003/10/Serialization/"" xmlns:a=""http://www.w3.org/2001/XMLSchema"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">true</z:anyType>"), true);
Assert.StrictEqual(SerializeAndDeserialize<object>("abc", @"<z:anyType i:type=""a:string"" xmlns:z=""http://schemas.microsoft.com/2003/10/Serialization/"" xmlns:a=""http://www.w3.org/2001/XMLSchema"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">abc</z:anyType>"), "abc");
Assert.StrictEqual(SerializeAndDeserialize<object>(null, @"<z:anyType i:nil=""true"" xmlns:z=""http://schemas.microsoft.com/2003/10/Serialization/"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""/>"), null);
}
[Fact]
public static void DCS_XmlQualifiedNameAsRoot()
{
Assert.StrictEqual(SerializeAndDeserialize<XmlQualifiedName>(new XmlQualifiedName("abc", "def"), @"<z:QName xmlns:z=""http://schemas.microsoft.com/2003/10/Serialization/"" xmlns:a=""def"">a:abc</z:QName>"), new XmlQualifiedName("abc", "def"));
Assert.StrictEqual(SerializeAndDeserialize<XmlQualifiedName>(XmlQualifiedName.Empty, @"<z:QName xmlns:z=""http://schemas.microsoft.com/2003/10/Serialization/""/>"), XmlQualifiedName.Empty);
}
[Fact]
public static void DCS_ShortAsRoot()
{
foreach (short value in new short[] { (short)-1.2, (short)0, (short)2.3, short.MinValue, short.MaxValue })
{
Assert.StrictEqual(SerializeAndDeserialize<short>(value, string.Format(@"<short xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">{0}</short>", value)), value);
}
}
[Fact]
public static void DCS_SbyteAsRoot()
{
foreach (sbyte value in new sbyte[] { (sbyte)3, (sbyte)0, sbyte.MinValue, sbyte.MaxValue })
{
Assert.StrictEqual(SerializeAndDeserialize<sbyte>(value, string.Format(@"<byte xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">{0}</byte>", value)), value);
}
}
[Fact]
public static void DCS_StringAsRoot()
{
Assert.StrictEqual(SerializeAndDeserialize<string>("abc", @"<string xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">abc</string>"), "abc");
Assert.StrictEqual(SerializeAndDeserialize<string>(" a b ", @"<string xmlns=""http://schemas.microsoft.com/2003/10/Serialization/""> a b </string>"), " a b ");
Assert.StrictEqual(SerializeAndDeserialize<string>(null, @"<string i:nil=""true"" xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""/>"), null);
Assert.StrictEqual(SerializeAndDeserialize<string>("", @"<string xmlns=""http://schemas.microsoft.com/2003/10/Serialization/""/>"), "");
Assert.StrictEqual(SerializeAndDeserialize<string>(" ", @"<string xmlns=""http://schemas.microsoft.com/2003/10/Serialization/""> </string>"), " ");
Assert.StrictEqual(SerializeAndDeserialize<string>("Hello World! 漢 ñ", @"<string xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">Hello World! 漢 ñ</string>"), "Hello World! 漢 ñ");
}
[Fact]
public static void DCS_TimeSpanAsRoot()
{
Assert.StrictEqual(SerializeAndDeserialize<TimeSpan>(new TimeSpan(1, 2, 3), @"<duration xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">PT1H2M3S</duration>"), new TimeSpan(1, 2, 3));
Assert.StrictEqual(SerializeAndDeserialize<TimeSpan>(TimeSpan.Zero, @"<duration xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">PT0S</duration>"), TimeSpan.Zero);
Assert.StrictEqual(SerializeAndDeserialize<TimeSpan>(TimeSpan.MinValue, @"<duration xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">-P10675199DT2H48M5.4775808S</duration>"), TimeSpan.MinValue);
Assert.StrictEqual(SerializeAndDeserialize<TimeSpan>(TimeSpan.MaxValue, @"<duration xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">P10675199DT2H48M5.4775807S</duration>"), TimeSpan.MaxValue);
}
[Fact]
public static void DCS_UintAsRoot()
{
foreach (uint value in new uint[] { (uint)3, (uint)0, uint.MinValue, uint.MaxValue })
{
Assert.StrictEqual<uint>(SerializeAndDeserialize<uint>(value, string.Format(@"<unsignedInt xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">{0}</unsignedInt>", value)), value);
}
}
[Fact]
public static void DCS_UlongAsRoot()
{
foreach (ulong value in new ulong[] { (ulong)3, (ulong)0, ulong.MinValue, ulong.MaxValue })
{
Assert.StrictEqual(SerializeAndDeserialize<ulong>(value, string.Format(@"<unsignedLong xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">{0}</unsignedLong>", value)), value);
}
}
[Fact]
public static void DCS_UshortAsRoot()
{
foreach (ushort value in new ushort[] { (ushort)3, (ushort)0, ushort.MinValue, ushort.MaxValue })
{
Assert.StrictEqual(SerializeAndDeserialize<ushort>(value, string.Format(@"<unsignedShort xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">{0}</unsignedShort>", value)), value);
}
}
[Fact]
public static void DCS_UriAsRoot()
{
Assert.StrictEqual(SerializeAndDeserialize<Uri>(new Uri("http://abc/"), @"<anyURI xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">http://abc/</anyURI>"), new Uri("http://abc/"));
Assert.StrictEqual(SerializeAndDeserialize<Uri>(new Uri("http://abc/def/x.aspx?p1=12&p2=34"), @"<anyURI xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">http://abc/def/x.aspx?p1=12&p2=34</anyURI>"), new Uri("http://abc/def/x.aspx?p1=12&p2=34"));
}
[Fact]
public static void DCS_ArrayAsRoot()
{
SimpleType[] x = new SimpleType[] { new SimpleType { P1 = "abc", P2 = 11 }, new SimpleType { P1 = "def", P2 = 12 } };
SimpleType[] y = SerializeAndDeserialize<SimpleType[]>(x, @"<ArrayOfSimpleType xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><SimpleType><P1>abc</P1><P2>11</P2></SimpleType><SimpleType><P1>def</P1><P2>12</P2></SimpleType></ArrayOfSimpleType>");
Utils.Equal<SimpleType>(x, y, (a, b) => { return SimpleType.AreEqual(a, b); });
}
[Fact]
public static void DCS_ArrayAsGetSet()
{
TypeWithGetSetArrayMembers x = new TypeWithGetSetArrayMembers
{
F1 = new SimpleType[] { new SimpleType { P1 = "ab", P2 = 1 }, new SimpleType { P1 = "cd", P2 = 2 } },
F2 = new int[] { -1, 3 },
P1 = new SimpleType[] { new SimpleType { P1 = "ef", P2 = 5 }, new SimpleType { P1 = "gh", P2 = 7 } },
P2 = new int[] { 11, 12 }
};
TypeWithGetSetArrayMembers y = SerializeAndDeserialize<TypeWithGetSetArrayMembers>(x, @"<TypeWithGetSetArrayMembers xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><F1><SimpleType><P1>ab</P1><P2>1</P2></SimpleType><SimpleType><P1>cd</P1><P2>2</P2></SimpleType></F1><F2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:int>-1</a:int><a:int>3</a:int></F2><P1><SimpleType><P1>ef</P1><P2>5</P2></SimpleType><SimpleType><P1>gh</P1><P2>7</P2></SimpleType></P1><P2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:int>11</a:int><a:int>12</a:int></P2></TypeWithGetSetArrayMembers>");
Assert.NotNull(y);
Utils.Equal<SimpleType>(x.F1, y.F1, (a, b) => { return SimpleType.AreEqual(a, b); });
Assert.Equal<int>(x.F2, y.F2);
Utils.Equal<SimpleType>(x.P1, y.P1, (a, b) => { return SimpleType.AreEqual(a, b); });
Assert.Equal<int>(x.P2, y.P2);
}
[Fact]
public static void DCS_ArrayAsGetOnly()
{
TypeWithGetOnlyArrayProperties x = new TypeWithGetOnlyArrayProperties();
x.P1[0] = new SimpleType { P1 = "ab", P2 = 1 };
x.P1[1] = new SimpleType { P1 = "cd", P2 = 2 };
x.P2[0] = -1;
x.P2[1] = 3;
TypeWithGetOnlyArrayProperties y = SerializeAndDeserialize<TypeWithGetOnlyArrayProperties>(x, @"<TypeWithGetOnlyArrayProperties xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><P1><SimpleType><P1>ab</P1><P2>1</P2></SimpleType><SimpleType><P1>cd</P1><P2>2</P2></SimpleType></P1><P2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:int>-1</a:int><a:int>3</a:int></P2></TypeWithGetOnlyArrayProperties>");
Assert.NotNull(y);
Utils.Equal<SimpleType>(x.P1, y.P1, (a, b) => { return SimpleType.AreEqual(a, b); });
Assert.Equal<int>(x.P2, y.P2);
}
[Fact]
public static void DCS_DictionaryGenericRoot()
{
Dictionary<string, int> x = new Dictionary<string, int>();
x.Add("one", 1);
x.Add("two", 2);
Dictionary<string, int> y = SerializeAndDeserialize<Dictionary<string, int>>(x, @"<ArrayOfKeyValueOfstringint xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><KeyValueOfstringint><Key>one</Key><Value>1</Value></KeyValueOfstringint><KeyValueOfstringint><Key>two</Key><Value>2</Value></KeyValueOfstringint></ArrayOfKeyValueOfstringint>");
Assert.NotNull(y);
Assert.True(y.Count == 2);
Assert.True(y["one"] == 1);
Assert.True(y["two"] == 2);
}
[Fact]
public static void DCS_DictionaryGenericMembers()
{
TypeWithDictionaryGenericMembers x = new TypeWithDictionaryGenericMembers
{
F1 = new Dictionary<string, int>(),
F2 = new Dictionary<string, int>(),
P1 = new Dictionary<string, int>(),
P2 = new Dictionary<string, int>()
};
x.F1.Add("ab", 12);
x.F1.Add("cd", 15);
x.F2.Add("ef", 17);
x.F2.Add("gh", 19);
x.P1.Add("12", 120);
x.P1.Add("13", 130);
x.P2.Add("14", 140);
x.P2.Add("15", 150);
x.RO1.Add(true, 't');
x.RO1.Add(false, 'f');
x.RO2.Add(true, 'a');
x.RO2.Add(false, 'b');
TypeWithDictionaryGenericMembers y = SerializeAndDeserialize<TypeWithDictionaryGenericMembers>(x, @"<TypeWithDictionaryGenericMembers xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><F1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfstringint><a:Key>ab</a:Key><a:Value>12</a:Value></a:KeyValueOfstringint><a:KeyValueOfstringint><a:Key>cd</a:Key><a:Value>15</a:Value></a:KeyValueOfstringint></F1><F2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfstringint><a:Key>ef</a:Key><a:Value>17</a:Value></a:KeyValueOfstringint><a:KeyValueOfstringint><a:Key>gh</a:Key><a:Value>19</a:Value></a:KeyValueOfstringint></F2><P1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfstringint><a:Key>12</a:Key><a:Value>120</a:Value></a:KeyValueOfstringint><a:KeyValueOfstringint><a:Key>13</a:Key><a:Value>130</a:Value></a:KeyValueOfstringint></P1><P2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfstringint><a:Key>14</a:Key><a:Value>140</a:Value></a:KeyValueOfstringint><a:KeyValueOfstringint><a:Key>15</a:Key><a:Value>150</a:Value></a:KeyValueOfstringint></P2><RO1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfbooleanchar><a:Key>true</a:Key><a:Value>116</a:Value></a:KeyValueOfbooleanchar><a:KeyValueOfbooleanchar><a:Key>false</a:Key><a:Value>102</a:Value></a:KeyValueOfbooleanchar></RO1><RO2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfbooleanchar><a:Key>true</a:Key><a:Value>97</a:Value></a:KeyValueOfbooleanchar><a:KeyValueOfbooleanchar><a:Key>false</a:Key><a:Value>98</a:Value></a:KeyValueOfbooleanchar></RO2></TypeWithDictionaryGenericMembers>");
Assert.NotNull(y);
Assert.NotNull(y.F1);
Assert.True(y.F1.Count == 2);
Assert.True(y.F1["ab"] == 12);
Assert.True(y.F1["cd"] == 15);
Assert.NotNull(y.F2);
Assert.True(y.F2.Count == 2);
Assert.True(y.F2["ef"] == 17);
Assert.True(y.F2["gh"] == 19);
Assert.NotNull(y.P1);
Assert.True(y.P1.Count == 2);
Assert.True(y.P1["12"] == 120);
Assert.True(y.P1["13"] == 130);
Assert.NotNull(y.P2);
Assert.True(y.P2.Count == 2);
Assert.True(y.P2["14"] == 140);
Assert.True(y.P2["15"] == 150);
Assert.NotNull(y.RO1);
Assert.True(y.RO1.Count == 2);
Assert.True(y.RO1[true] == 't');
Assert.True(y.RO1[false] == 'f');
Assert.NotNull(y.RO2);
Assert.True(y.RO2.Count == 2);
Assert.True(y.RO2[true] == 'a');
Assert.True(y.RO2[false] == 'b');
}
[Fact]
public static void DCS_DictionaryRoot()
{
MyDictionary x = new MyDictionary();
x.Add(1, "one");
x.Add(2, "two");
MyDictionary y = SerializeAndDeserialize<MyDictionary>(x, @"<ArrayOfKeyValueOfanyTypeanyType xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><KeyValueOfanyTypeanyType><Key i:type=""a:int"" xmlns:a=""http://www.w3.org/2001/XMLSchema"">1</Key><Value i:type=""a:string"" xmlns:a=""http://www.w3.org/2001/XMLSchema"">one</Value></KeyValueOfanyTypeanyType><KeyValueOfanyTypeanyType><Key i:type=""a:int"" xmlns:a=""http://www.w3.org/2001/XMLSchema"">2</Key><Value i:type=""a:string"" xmlns:a=""http://www.w3.org/2001/XMLSchema"">two</Value></KeyValueOfanyTypeanyType></ArrayOfKeyValueOfanyTypeanyType>");
Assert.NotNull(y);
Assert.True(y.Count == 2);
Assert.True((string)y[1] == "one");
Assert.True((string)y[2] == "two");
}
[Fact]
public static void DCS_DictionaryMembers()
{
TypeWithDictionaryMembers x = new TypeWithDictionaryMembers();
x.F1 = new MyDictionary();
x.F1.Add("ab", 12);
x.F1.Add("cd", 15);
x.F2 = new MyDictionary();
x.F2.Add("ef", 17);
x.F2.Add("gh", 19);
x.P1 = new MyDictionary();
x.P1.Add("12", 120);
x.P1.Add("13", 130);
x.P2 = new MyDictionary();
x.P2.Add("14", 140);
x.P2.Add("15", 150);
x.RO1.Add(true, 't');
x.RO1.Add(false, 'f');
x.RO2.Add(true, 'a');
x.RO2.Add(false, 'b');
TypeWithDictionaryMembers y = SerializeAndDeserialize<TypeWithDictionaryMembers>(x, @"<TypeWithDictionaryMembers xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><F1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfanyTypeanyType><a:Key i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">ab</a:Key><a:Value i:type=""b:int"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">12</a:Value></a:KeyValueOfanyTypeanyType><a:KeyValueOfanyTypeanyType><a:Key i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">cd</a:Key><a:Value i:type=""b:int"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">15</a:Value></a:KeyValueOfanyTypeanyType></F1><F2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfanyTypeanyType><a:Key i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">ef</a:Key><a:Value i:type=""b:int"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">17</a:Value></a:KeyValueOfanyTypeanyType><a:KeyValueOfanyTypeanyType><a:Key i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">gh</a:Key><a:Value i:type=""b:int"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">19</a:Value></a:KeyValueOfanyTypeanyType></F2><P1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfanyTypeanyType><a:Key i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">12</a:Key><a:Value i:type=""b:int"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">120</a:Value></a:KeyValueOfanyTypeanyType><a:KeyValueOfanyTypeanyType><a:Key i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">13</a:Key><a:Value i:type=""b:int"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">130</a:Value></a:KeyValueOfanyTypeanyType></P1><P2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfanyTypeanyType><a:Key i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">14</a:Key><a:Value i:type=""b:int"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">140</a:Value></a:KeyValueOfanyTypeanyType><a:KeyValueOfanyTypeanyType><a:Key i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">15</a:Key><a:Value i:type=""b:int"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">150</a:Value></a:KeyValueOfanyTypeanyType></P2><RO1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfanyTypeanyType><a:Key i:type=""b:boolean"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">true</a:Key><a:Value i:type=""b:char"" xmlns:b=""http://schemas.microsoft.com/2003/10/Serialization/"">116</a:Value></a:KeyValueOfanyTypeanyType><a:KeyValueOfanyTypeanyType><a:Key i:type=""b:boolean"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">false</a:Key><a:Value i:type=""b:char"" xmlns:b=""http://schemas.microsoft.com/2003/10/Serialization/"">102</a:Value></a:KeyValueOfanyTypeanyType></RO1><RO2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfanyTypeanyType><a:Key i:type=""b:boolean"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">true</a:Key><a:Value i:type=""b:char"" xmlns:b=""http://schemas.microsoft.com/2003/10/Serialization/"">97</a:Value></a:KeyValueOfanyTypeanyType><a:KeyValueOfanyTypeanyType><a:Key i:type=""b:boolean"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">false</a:Key><a:Value i:type=""b:char"" xmlns:b=""http://schemas.microsoft.com/2003/10/Serialization/"">98</a:Value></a:KeyValueOfanyTypeanyType></RO2></TypeWithDictionaryMembers>");
Assert.NotNull(y);
Assert.NotNull(y.F1);
Assert.True(y.F1.Count == 2);
Assert.True((int)y.F1["ab"] == 12);
Assert.True((int)y.F1["cd"] == 15);
Assert.NotNull(y.F2);
Assert.True(y.F2.Count == 2);
Assert.True((int)y.F2["ef"] == 17);
Assert.True((int)y.F2["gh"] == 19);
Assert.NotNull(y.P1);
Assert.True(y.P1.Count == 2);
Assert.True((int)y.P1["12"] == 120);
Assert.True((int)y.P1["13"] == 130);
Assert.NotNull(y.P2);
Assert.True(y.P2.Count == 2);
Assert.True((int)y.P2["14"] == 140);
Assert.True((int)y.P2["15"] == 150);
Assert.NotNull(y.RO1);
Assert.True(y.RO1.Count == 2);
Assert.True((char)y.RO1[true] == 't');
Assert.True((char)y.RO1[false] == 'f');
Assert.NotNull(y.RO2);
Assert.True(y.RO2.Count == 2);
Assert.True((char)y.RO2[true] == 'a');
Assert.True((char)y.RO2[false] == 'b');
}
[Fact]
public static void DCS_TypeWithIDictionaryPropertyInitWithConcreteType()
{
// Test for Bug 876869 : [Serialization] Concrete type not inferred for DCS
var dict = new TypeWithIDictionaryPropertyInitWithConcreteType();
dict.DictionaryProperty.Add("key1", "value1");
dict.DictionaryProperty.Add("key2", "value2");
var dict2 = SerializeAndDeserialize<TypeWithIDictionaryPropertyInitWithConcreteType>(dict, @"<TypeWithIDictionaryPropertyInitWithConcreteType xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><DictionaryProperty xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfstringstring><a:Key>key1</a:Key><a:Value>value1</a:Value></a:KeyValueOfstringstring><a:KeyValueOfstringstring><a:Key>key2</a:Key><a:Value>value2</a:Value></a:KeyValueOfstringstring></DictionaryProperty></TypeWithIDictionaryPropertyInitWithConcreteType>");
Assert.True(dict2 != null && dict2.DictionaryProperty != null);
Assert.True(dict.DictionaryProperty.Count == dict2.DictionaryProperty.Count);
foreach (var entry in dict.DictionaryProperty)
{
Assert.True(dict2.DictionaryProperty.ContainsKey(entry.Key) && dict2.DictionaryProperty[entry.Key].Equals(dict.DictionaryProperty[entry.Key]));
}
}
[Fact]
public static void DCS_ListGenericRoot()
{
List<string> x = new List<string>();
x.Add("zero");
x.Add("one");
List<string> y = SerializeAndDeserialize<List<string>>(x, @"<ArrayOfstring xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><string>zero</string><string>one</string></ArrayOfstring>");
Assert.NotNull(y);
Assert.True(y.Count == 2);
Assert.True(y[0] == "zero");
Assert.True(y[1] == "one");
}
[Fact]
public static void DCS_ListGenericMembers()
{
TypeWithListGenericMembers x = new TypeWithListGenericMembers();
x.F1 = new List<string>();
x.F1.Add("zero");
x.F1.Add("one");
x.F2 = new List<string>();
x.F2.Add("abc");
x.F2.Add("def");
x.P1 = new List<int>();
x.P1.Add(10);
x.P1.Add(20);
x.P2 = new List<int>();
x.P2.Add(12);
x.P2.Add(34);
x.RO1.Add('a');
x.RO1.Add('b');
x.RO2.Add('c');
x.RO2.Add('d');
TypeWithListGenericMembers y = SerializeAndDeserialize<TypeWithListGenericMembers>(x, @"<TypeWithListGenericMembers xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><F1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string>zero</a:string><a:string>one</a:string></F1><F2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string>abc</a:string><a:string>def</a:string></F2><P1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:int>10</a:int><a:int>20</a:int></P1><P2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:int>12</a:int><a:int>34</a:int></P2><RO1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:char>97</a:char><a:char>98</a:char></RO1><RO2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:char>99</a:char><a:char>100</a:char></RO2></TypeWithListGenericMembers>");
Assert.NotNull(y);
Assert.NotNull(y.F1);
Assert.True(y.F1.Count == 2);
Assert.True(y.F1[0] == "zero");
Assert.True(y.F1[1] == "one");
Assert.NotNull(y.F2);
Assert.True(y.F2.Count == 2);
Assert.True(y.F2[0] == "abc");
Assert.True(y.F2[1] == "def");
Assert.NotNull(y.P1);
Assert.True(y.P1.Count == 2);
Assert.True(y.P1[0] == 10);
Assert.True(y.P1[1] == 20);
Assert.NotNull(y.P2);
Assert.True(y.P2.Count == 2);
Assert.True(y.P2[0] == 12);
Assert.True(y.P2[1] == 34);
Assert.NotNull(y.RO1);
Assert.True(y.RO1.Count == 2);
Assert.True(y.RO1[0] == 'a');
Assert.True(y.RO1[1] == 'b');
Assert.NotNull(y.RO2);
Assert.True(y.RO2.Count == 2);
Assert.True(y.RO2[0] == 'c');
Assert.True(y.RO2[1] == 'd');
}
[Fact]
public static void DCS_CollectionGenericRoot()
{
MyCollection<string> x = new MyCollection<string>("a1", "a2");
MyCollection<string> y = SerializeAndDeserialize<MyCollection<string>>(x, @"<ArrayOfstring xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><string>a1</string><string>a2</string></ArrayOfstring>");
Assert.NotNull(y);
Assert.True(y.Count == 2);
foreach (var item in x)
{
Assert.True(y.Contains(item));
}
}
[Fact]
public static void DCS_CollectionGenericMembers()
{
TypeWithCollectionGenericMembers x = new TypeWithCollectionGenericMembers
{
F1 = new MyCollection<string>("a1", "a2"),
F2 = new MyCollection<string>("b1", "b2"),
P1 = new MyCollection<string>("c1", "c2"),
P2 = new MyCollection<string>("d1", "d2"),
};
x.RO1.Add("abc");
x.RO2.Add("xyz");
TypeWithCollectionGenericMembers y = SerializeAndDeserialize<TypeWithCollectionGenericMembers>(x, @"<TypeWithCollectionGenericMembers xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><F1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string>a1</a:string><a:string>a2</a:string></F1><F2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string>b1</a:string><a:string>b2</a:string></F2><P1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string>c1</a:string><a:string>c2</a:string></P1><P2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string>d1</a:string><a:string>d2</a:string></P2><RO1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string>abc</a:string></RO1><RO2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string>xyz</a:string></RO2></TypeWithCollectionGenericMembers>");
Assert.NotNull(y);
Assert.True(y.F1.Count == 2, getCheckFailureMsg("F1"));
Assert.True(y.F2.Count == 2, getCheckFailureMsg("F2"));
Assert.True(y.P1.Count == 2, getCheckFailureMsg("P1"));
Assert.True(y.P2.Count == 2, getCheckFailureMsg("P2"));
Assert.True(y.RO1.Count == 1, getCheckFailureMsg("RO1"));
Assert.True(y.RO2.Count == 1, getCheckFailureMsg("RO2"));
foreach (var item in x.F1)
{
Assert.True(y.F1.Contains(item), getCheckFailureMsg("F1"));
}
foreach (var item in x.F2)
{
Assert.True(y.F2.Contains(item), getCheckFailureMsg("F2"));
}
foreach (var item in x.P1)
{
Assert.True(y.P1.Contains(item), getCheckFailureMsg("P1"));
}
foreach (var item in x.P2)
{
Assert.True(y.P2.Contains(item), getCheckFailureMsg("P2"));
}
foreach (var item in x.RO1)
{
Assert.True(y.RO1.Contains(item), getCheckFailureMsg("RO1"));
}
foreach (var item in x.RO2)
{
Assert.True(y.RO2.Contains(item), getCheckFailureMsg("RO2"));
}
}
[Fact]
public static void DCS_ListRoot()
{
MyList x = new MyList("a1", "a2");
MyList y = SerializeAndDeserialize<MyList>(x, @"<ArrayOfanyType xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><anyType i:type=""a:string"" xmlns:a=""http://www.w3.org/2001/XMLSchema"">a1</anyType><anyType i:type=""a:string"" xmlns:a=""http://www.w3.org/2001/XMLSchema"">a2</anyType></ArrayOfanyType>");
Assert.NotNull(y);
Assert.True(y.Count == 2);
foreach (var item in x)
{
Assert.True(y.Contains(item));
}
}
[Fact]
public static void DCS_ListMembers()
{
TypeWithListMembers x = new TypeWithListMembers
{
F1 = new MyList("a1", "a2"),
F2 = new MyList("b1", "b2"),
P1 = new MyList("c1", "c2"),
P2 = new MyList("d1", "d2"),
};
x.RO1.Add("abc");
x.RO2.Add("xyz");
TypeWithListMembers y = SerializeAndDeserialize<TypeWithListMembers>(x, @"<TypeWithListMembers xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><F1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">a1</a:anyType><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">a2</a:anyType></F1><F2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">b1</a:anyType><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">b2</a:anyType></F2><P1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">c1</a:anyType><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">c2</a:anyType></P1><P2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">d1</a:anyType><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">d2</a:anyType></P2><RO1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">abc</a:anyType></RO1><RO2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">xyz</a:anyType></RO2></TypeWithListMembers>");
Assert.NotNull(y);
Assert.True(y.F1.Count == 2, getCheckFailureMsg("F1"));
Assert.True(y.F2.Count == 2, getCheckFailureMsg("F2"));
Assert.True(y.P1.Count == 2, getCheckFailureMsg("P1"));
Assert.True(y.P2.Count == 2, getCheckFailureMsg("P2"));
Assert.True(y.RO1.Count == 1, getCheckFailureMsg("RO1"));
Assert.True(y.RO2.Count == 1, getCheckFailureMsg("RO2"));
Assert.True((string)x.F1[0] == (string)y.F1[0], getCheckFailureMsg("F1"));
Assert.True((string)x.F1[1] == (string)y.F1[1], getCheckFailureMsg("F1"));
Assert.True((string)x.F2[0] == (string)y.F2[0], getCheckFailureMsg("F2"));
Assert.True((string)x.F2[1] == (string)y.F2[1], getCheckFailureMsg("F2"));
Assert.True((string)x.P1[0] == (string)y.P1[0], getCheckFailureMsg("P1"));
Assert.True((string)x.P1[1] == (string)y.P1[1], getCheckFailureMsg("P1"));
Assert.True((string)x.P2[0] == (string)y.P2[0], getCheckFailureMsg("P2"));
Assert.True((string)x.P2[1] == (string)y.P2[1], getCheckFailureMsg("P2"));
Assert.True((string)x.RO1[0] == (string)y.RO1[0], getCheckFailureMsg("RO1"));
Assert.True((string)x.RO2[0] == (string)y.RO2[0], getCheckFailureMsg("RO2"));
}
[Fact]
public static void DCS_EnumerableGenericRoot()
{
MyEnumerable<string> x = new MyEnumerable<string>("a1", "a2");
MyEnumerable<string> y = SerializeAndDeserialize<MyEnumerable<string>>(x, @"<ArrayOfstring xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><string>a1</string><string>a2</string></ArrayOfstring>");
Assert.NotNull(y);
Assert.True(y.Count == 2);
string actual = string.Join("", y);
Assert.StrictEqual(actual, "a1a2");
}
[Fact]
public static void DCS_EnumerableGenericMembers()
{
TypeWithEnumerableGenericMembers x = new TypeWithEnumerableGenericMembers
{
F1 = new MyEnumerable<string>("a1", "a2"),
F2 = new MyEnumerable<string>("b1", "b2"),
P1 = new MyEnumerable<string>("c1", "c2"),
P2 = new MyEnumerable<string>("d1", "d2")
};
x.RO1.Add("abc");
TypeWithEnumerableGenericMembers y = SerializeAndDeserialize<TypeWithEnumerableGenericMembers>(x, @"<TypeWithEnumerableGenericMembers xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><F1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string>a1</a:string><a:string>a2</a:string></F1><F2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string>b1</a:string><a:string>b2</a:string></F2><P1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string>c1</a:string><a:string>c2</a:string></P1><P2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string>d1</a:string><a:string>d2</a:string></P2><RO1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string>abc</a:string></RO1></TypeWithEnumerableGenericMembers>");
Assert.NotNull(y);
Assert.True(y.F1.Count == 2);
Assert.True(((string[])y.F2).Length == 2);
Assert.True(y.P1.Count == 2);
Assert.True(((string[])y.P2).Length == 2);
Assert.True(y.RO1.Count == 1);
}
[Fact]
public static void DCS_CollectionRoot()
{
MyCollection x = new MyCollection('a', 45);
MyCollection y = SerializeAndDeserialize<MyCollection>(x, @"<ArrayOfanyType xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><anyType i:type=""a:char"" xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/"">97</anyType><anyType i:type=""a:int"" xmlns:a=""http://www.w3.org/2001/XMLSchema"">45</anyType></ArrayOfanyType>");
Assert.NotNull(y);
Assert.True(y.Count == 2);
Assert.True((char)y[0] == 'a');
Assert.True((int)y[1] == 45);
}
[Fact]
public static void DCS_CollectionMembers()
{
TypeWithCollectionMembers x = new TypeWithCollectionMembers
{
F1 = new MyCollection('a', 45),
F2 = new MyCollection("ab", true),
P1 = new MyCollection("x", "y"),
P2 = new MyCollection(false, true)
};
x.RO1.Add("abc");
TypeWithCollectionMembers y = SerializeAndDeserialize<TypeWithCollectionMembers>(x, @"<TypeWithCollectionMembers xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><F1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:char"" xmlns:b=""http://schemas.microsoft.com/2003/10/Serialization/"">97</a:anyType><a:anyType i:type=""b:int"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">45</a:anyType></F1><F2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">ab</a:anyType><a:anyType i:type=""b:boolean"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">true</a:anyType></F2><P1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">x</a:anyType><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">y</a:anyType></P1><P2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:boolean"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">false</a:anyType><a:anyType i:type=""b:boolean"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">true</a:anyType></P2><RO1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">abc</a:anyType></RO1></TypeWithCollectionMembers>");
Assert.NotNull(y);
Assert.NotNull(y.F1);
Assert.True(y.F1.Count == 2);
Assert.True((char)y.F1[0] == 'a');
Assert.True((int)y.F1[1] == 45);
Assert.NotNull(y.F2);
Assert.True(((object[])y.F2).Length == 2);
Assert.True((string)((object[])y.F2)[0] == "ab");
Assert.True((bool)((object[])y.F2)[1] == true);
Assert.True(y.P1.Count == 2);
Assert.True((string)y.P1[0] == "x");
Assert.True((string)y.P1[1] == "y");
Assert.True(((object[])y.P2).Length == 2);
Assert.True((bool)((object[])y.P2)[0] == false);
Assert.True((bool)((object[])y.P2)[1] == true);
Assert.True(y.RO1.Count == 1);
Assert.True((string)y.RO1[0] == "abc");
}
[Fact]
public static void DCS_EnumerableRoot()
{
MyEnumerable x = new MyEnumerable("abc", 3);
MyEnumerable y = SerializeAndDeserialize<MyEnumerable>(x, @"<ArrayOfanyType xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><anyType i:type=""a:string"" xmlns:a=""http://www.w3.org/2001/XMLSchema"">abc</anyType><anyType i:type=""a:int"" xmlns:a=""http://www.w3.org/2001/XMLSchema"">3</anyType></ArrayOfanyType>");
Assert.NotNull(y);
Assert.True(y.Count == 2);
Assert.True((string)y[0] == "abc");
Assert.True((int)y[1] == 3);
}
[Fact]
public static void DCS_EnumerableMembers()
{
TypeWithEnumerableMembers x = new TypeWithEnumerableMembers
{
F1 = new MyEnumerable('a', 45),
F2 = new MyEnumerable("ab", true),
P1 = new MyEnumerable("x", "y"),
P2 = new MyEnumerable(false, true)
};
x.RO1.Add('x');
TypeWithEnumerableMembers y = SerializeAndDeserialize<TypeWithEnumerableMembers>(x, @"<TypeWithEnumerableMembers xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><F1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:char"" xmlns:b=""http://schemas.microsoft.com/2003/10/Serialization/"">97</a:anyType><a:anyType i:type=""b:int"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">45</a:anyType></F1><F2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">ab</a:anyType><a:anyType i:type=""b:boolean"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">true</a:anyType></F2><P1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">x</a:anyType><a:anyType i:type=""b:string"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">y</a:anyType></P1><P2 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:boolean"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">false</a:anyType><a:anyType i:type=""b:boolean"" xmlns:b=""http://www.w3.org/2001/XMLSchema"">true</a:anyType></P2><RO1 xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:anyType i:type=""b:char"" xmlns:b=""http://schemas.microsoft.com/2003/10/Serialization/"">120</a:anyType></RO1></TypeWithEnumerableMembers>");
Assert.NotNull(y);
Assert.True(y.F1.Count == 2);
Assert.True((char)y.F1[0] == 'a');
Assert.True((int)y.F1[1] == 45);
Assert.True(((object[])y.F2).Length == 2);
Assert.True((string)((object[])y.F2)[0] == "ab");
Assert.True((bool)((object[])y.F2)[1] == true);
Assert.True(y.P1.Count == 2);
Assert.True((string)y.P1[0] == "x");
Assert.True((string)y.P1[1] == "y");
Assert.True(((object[])y.P2).Length == 2);
Assert.True((bool)((object[])y.P2)[0] == false);
Assert.True((bool)((object[])y.P2)[1] == true);
Assert.True(y.RO1.Count == 1);
Assert.True((char)y.RO1[0] == 'x');
}
[Fact]
public static void DCS_CustomType()
{
MyTypeA x = new MyTypeA
{
PropX = new MyTypeC { PropC = 'a', PropB = true },
PropY = 45,
};
MyTypeA y = SerializeAndDeserialize<MyTypeA>(x, @"<MyTypeA xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><P_Col_Array i:nil=""true""/><PropX i:type=""MyTypeC""><PropA i:nil=""true""/><PropC>97</PropC><PropB>true</PropB></PropX><PropY>45</PropY></MyTypeA>");
Assert.NotNull(y);
Assert.NotNull(y.PropX);
Assert.StrictEqual(x.PropX.PropC, y.PropX.PropC);
Assert.StrictEqual(((MyTypeC)x.PropX).PropB, ((MyTypeC)y.PropX).PropB);
Assert.StrictEqual(x.PropY, y.PropY);
}
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "dotnet/corefx #19516")]
public static void DCS_TypeWithPrivateFieldAndPrivateGetPublicSetProperty()
{
TypeWithPrivateFieldAndPrivateGetPublicSetProperty x = new TypeWithPrivateFieldAndPrivateGetPublicSetProperty
{
Name = "foo",
};
TypeWithPrivateFieldAndPrivateGetPublicSetProperty y = SerializeAndDeserialize<TypeWithPrivateFieldAndPrivateGetPublicSetProperty>(x, @"<TypeWithPrivateFieldAndPrivateGetPublicSetProperty xmlns=""http://schemas.datacontract.org/2004/07/"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><Name>foo</Name></TypeWithPrivateFieldAndPrivateGetPublicSetProperty>");
Assert.Equal(x.GetName(), y.GetName());
}
[Fact]
public static void DCS_DataContractAttribute()
{
SerializeAndDeserialize<DCA_1>(new DCA_1 { P1 = "xyz" }, @"<DCA_1 xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""/>");
SerializeAndDeserialize<DCA_2>(new DCA_2 { P1 = "xyz" }, @"<abc xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""/>");
SerializeAndDeserialize<DCA_3>(new DCA_3 { P1 = "xyz" }, @"<DCA_3 xmlns=""def"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""/>");
SerializeAndDeserialize<DCA_4>(new DCA_4 { P1 = "xyz" }, @"<DCA_4 z:Id=""i1"" xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:z=""http://schemas.microsoft.com/2003/10/Serialization/""/>");
SerializeAndDeserialize<DCA_5>(new DCA_5 { P1 = "xyz" }, @"<abc xmlns=""def"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""/>");
}
[Fact]
public static void DCS_DataMemberAttribute()
{
SerializeAndDeserialize<DMA_1>(new DMA_1 { P1 = "abc", P2 = 12, P3 = true, P4 = 'a', P5 = 10, MyDataMemberInAnotherNamespace = new MyDataContractClass04_1() { MyDataMember = "Test" }, Order100 = true, OrderMaxValue = false }, @"<DMA_1 xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><MyDataMemberInAnotherNamespace xmlns:a=""http://MyDataContractClass04_1.com/""><a:MyDataMember>Test</a:MyDataMember></MyDataMemberInAnotherNamespace><P1>abc</P1><P4>97</P4><P5>10</P5><xyz>12</xyz><P3>true</P3><Order100>true</Order100><OrderMaxValue>false</OrderMaxValue></DMA_1>");
}
[Fact]
public static void DCS_IgnoreDataMemberAttribute()
{
IDMA_1 x = new IDMA_1 { MyDataMember = "MyDataMember", MyIgnoreDataMember = "MyIgnoreDataMember", MyUnsetDataMember = "MyUnsetDataMember" };
IDMA_1 y = SerializeAndDeserialize<IDMA_1>(x, @"<IDMA_1 xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><MyDataMember>MyDataMember</MyDataMember></IDMA_1>");
Assert.NotNull(y);
Assert.StrictEqual(x.MyDataMember, y.MyDataMember);
Assert.Null(y.MyIgnoreDataMember);
Assert.Null(y.MyUnsetDataMember);
}
[Fact]
public static void DCS_EnumAsRoot()
{
//The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.
Assert.StrictEqual(SerializeAndDeserialize<MyEnum>(MyEnum.Two, @"<MyEnum xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"">Two</MyEnum>"), MyEnum.Two);
Assert.StrictEqual(SerializeAndDeserialize<ByteEnum>(ByteEnum.Option1, @"<ByteEnum xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"">Option1</ByteEnum>"), ByteEnum.Option1);
Assert.StrictEqual(SerializeAndDeserialize<SByteEnum>(SByteEnum.Option1, @"<SByteEnum xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"">Option1</SByteEnum>"), SByteEnum.Option1);
Assert.StrictEqual(SerializeAndDeserialize<ShortEnum>(ShortEnum.Option1, @"<ShortEnum xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"">Option1</ShortEnum>"), ShortEnum.Option1);
Assert.StrictEqual(SerializeAndDeserialize<IntEnum>(IntEnum.Option1, @"<IntEnum xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"">Option1</IntEnum>"), IntEnum.Option1);
Assert.StrictEqual(SerializeAndDeserialize<UIntEnum>(UIntEnum.Option1, @"<UIntEnum xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"">Option1</UIntEnum>"), UIntEnum.Option1);
Assert.StrictEqual(SerializeAndDeserialize<LongEnum>(LongEnum.Option1, @"<LongEnum xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"">Option1</LongEnum>"), LongEnum.Option1);
Assert.StrictEqual(SerializeAndDeserialize<ULongEnum>(ULongEnum.Option1, @"<ULongEnum xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"">Option1</ULongEnum>"), ULongEnum.Option1);
}
[Fact]
public static void DCS_EnumAsMember()
{
TypeWithEnumMembers x = new TypeWithEnumMembers { F1 = MyEnum.Three, P1 = MyEnum.Two };
TypeWithEnumMembers y = SerializeAndDeserialize<TypeWithEnumMembers>(x, @"<TypeWithEnumMembers xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><F1>Three</F1><P1>Two</P1></TypeWithEnumMembers>");
Assert.NotNull(y);
Assert.StrictEqual(x.F1, y.F1);
Assert.StrictEqual(x.P1, y.P1);
}
[Fact]
public static void DCS_DCClassWithEnumAndStruct()
{
var x = new DCClassWithEnumAndStruct(true);
var y = SerializeAndDeserialize<DCClassWithEnumAndStruct>(x, @"<DCClassWithEnumAndStruct xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><MyEnum1>One</MyEnum1><MyStruct><Data>Data</Data></MyStruct></DCClassWithEnumAndStruct>");
Assert.StrictEqual(x.MyStruct, y.MyStruct);
Assert.StrictEqual(x.MyEnum1, y.MyEnum1);
}
[Fact]
public static void DCS_SuspensionManager()
{
var x = new Dictionary<string, object>();
var subDictionary = new Dictionary<string, object>();
subDictionary.Add("subkey1", "subkey1value");
x.Add("Key1", subDictionary);
Dictionary<string, object> y = SerializeAndDeserialize<Dictionary<string, object>>(x, @"<ArrayOfKeyValueOfstringanyType xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><KeyValueOfstringanyType><Key>Key1</Key><Value i:type=""ArrayOfKeyValueOfstringanyType""><KeyValueOfstringanyType><Key>subkey1</Key><Value i:type=""a:string"" xmlns:a=""http://www.w3.org/2001/XMLSchema"">subkey1value</Value></KeyValueOfstringanyType></Value></KeyValueOfstringanyType></ArrayOfKeyValueOfstringanyType>");
Assert.NotNull(y);
Assert.StrictEqual(y.Count, 1);
Assert.True(y["Key1"] is Dictionary<string, object>);
Assert.StrictEqual(((y["Key1"] as Dictionary<string, object>)["subkey1"]) as string, "subkey1value");
}
[Fact]
public static void DCS_BuiltInTypes()
{
BuiltInTypes x = new BuiltInTypes
{
ByteArray = new byte[] { 1, 2 }
};
BuiltInTypes y = SerializeAndDeserialize<BuiltInTypes>(x, @"<BuiltInTypes xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><ByteArray>AQI=</ByteArray></BuiltInTypes>");
Assert.NotNull(y);
Assert.Equal<byte>(x.ByteArray, y.ByteArray);
}
[Fact]
public static void DCS_CircularLink()
{
CircularLinkDerived circularLinkDerived = new CircularLinkDerived(true);
SerializeAndDeserialize<CircularLinkDerived>(circularLinkDerived, @"<CircularLinkDerived z:Id=""i1"" xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:z=""http://schemas.microsoft.com/2003/10/Serialization/""><Link z:Id=""i2""><Link z:Id=""i3""><Link z:Ref=""i1""/><RandomHangingLink i:nil=""true""/></Link><RandomHangingLink i:nil=""true""/></Link><RandomHangingLink z:Id=""i4""><Link z:Id=""i5""><Link z:Id=""i6"" i:type=""CircularLinkDerived""><Link z:Ref=""i4""/><RandomHangingLink i:nil=""true""/></Link><RandomHangingLink i:nil=""true""/></Link><RandomHangingLink i:nil=""true""/></RandomHangingLink></CircularLinkDerived>");
}
[Fact]
public static void DCS_DataMemberNames()
{
var obj = new AppEnvironment()
{
ScreenDpi = 440,
ScreenOrientation = "horizontal"
};
var actual = SerializeAndDeserialize(obj, "<AppEnvironment xmlns=\"http://schemas.datacontract.org/2004/07/\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><screen_dpi_x0028_x_x003A_y_x0029_>440</screen_dpi_x0028_x_x003A_y_x0029_><screen_x003A_orientation>horizontal</screen_x003A_orientation></AppEnvironment>");
Assert.StrictEqual(obj.ScreenDpi, actual.ScreenDpi);
Assert.StrictEqual(obj.ScreenOrientation, actual.ScreenOrientation);
}
[Fact]
public static void DCS_GenericBase()
{
var actual = SerializeAndDeserialize<GenericBase2<SimpleBaseDerived, SimpleBaseDerived2>>(new GenericBase2<SimpleBaseDerived, SimpleBaseDerived2>(true), @"<GenericBase2OfSimpleBaseDerivedSimpleBaseDerived2zbP0weY4 z:Id=""i1"" xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:z=""http://schemas.microsoft.com/2003/10/Serialization/""><genericData1 z:Id=""i2""><BaseData/><DerivedData/></genericData1><genericData2 z:Id=""i3""><BaseData/><DerivedData/></genericData2></GenericBase2OfSimpleBaseDerivedSimpleBaseDerived2zbP0weY4>");
Assert.True(actual.genericData1 is SimpleBaseDerived);
Assert.True(actual.genericData2 is SimpleBaseDerived2);
}
[Fact]
public static void DCS_GenericContainer()
{
SerializeAndDeserialize<GenericContainer>(new GenericContainer(true), @"<GenericContainer z:Id=""i1"" xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:z=""http://schemas.microsoft.com/2003/10/Serialization/""><GenericData z:Id=""i2"" i:type=""GenericBaseOfSimpleBaseContainervjX03eZJ""><genericData z:Id=""i3"" i:type=""SimpleBaseContainer""><Base1 i:nil=""true""/><Base2 i:nil=""true""/></genericData></GenericData></GenericContainer>");
}
[Fact]
public static void DCS_DictionaryWithVariousKeyValueTypes()
{
var x = new DictionaryWithVariousKeyValueTypes(true);
var y = SerializeAndDeserialize<DictionaryWithVariousKeyValueTypes>(x, @"<DictionaryWithVariousKeyValueTypes xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><WithEnums xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfMyEnumMyEnumzbP0weY4><a:Key>Two</a:Key><a:Value>Three</a:Value></a:KeyValueOfMyEnumMyEnumzbP0weY4><a:KeyValueOfMyEnumMyEnumzbP0weY4><a:Key>One</a:Key><a:Value>One</a:Value></a:KeyValueOfMyEnumMyEnumzbP0weY4></WithEnums><WithNullables xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfNullableOfshortNullableOfboolean_ShTDFhl_P><a:Key>-32768</a:Key><a:Value>true</a:Value></a:KeyValueOfNullableOfshortNullableOfboolean_ShTDFhl_P><a:KeyValueOfNullableOfshortNullableOfboolean_ShTDFhl_P><a:Key>0</a:Key><a:Value>false</a:Value></a:KeyValueOfNullableOfshortNullableOfboolean_ShTDFhl_P><a:KeyValueOfNullableOfshortNullableOfboolean_ShTDFhl_P><a:Key>32767</a:Key><a:Value i:nil=""true""/></a:KeyValueOfNullableOfshortNullableOfboolean_ShTDFhl_P></WithNullables><WithStructs xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfStructNotSerializableStructNotSerializablezbP0weY4><a:Key><value>10</value></a:Key><a:Value><value>12</value></a:Value></a:KeyValueOfStructNotSerializableStructNotSerializablezbP0weY4><a:KeyValueOfStructNotSerializableStructNotSerializablezbP0weY4><a:Key><value>2147483647</value></a:Key><a:Value><value>-2147483648</value></a:Value></a:KeyValueOfStructNotSerializableStructNotSerializablezbP0weY4></WithStructs></DictionaryWithVariousKeyValueTypes>");
Assert.StrictEqual(y.WithEnums[MyEnum.Two], MyEnum.Three);
Assert.StrictEqual(y.WithEnums[MyEnum.One], MyEnum.One);
Assert.StrictEqual(y.WithStructs[new StructNotSerializable() { value = 10 }], new StructNotSerializable() { value = 12 });
Assert.StrictEqual(y.WithStructs[new StructNotSerializable() { value = int.MaxValue }], new StructNotSerializable() { value = int.MinValue });
Assert.StrictEqual(y.WithNullables[Int16.MinValue], true);
Assert.StrictEqual(y.WithNullables[0], false);
Assert.StrictEqual(y.WithNullables[Int16.MaxValue], null);
}
[Fact]
public static void DCS_TypesWithArrayOfOtherTypes()
{
var x = new TypeHasArrayOfASerializedAsB(true);
var y = SerializeAndDeserialize<TypeHasArrayOfASerializedAsB>(x, @"<TypeHasArrayOfASerializedAsB xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><Items><TypeA><Name>typeAValue</Name></TypeA><TypeA><Name>typeBValue</Name></TypeA></Items></TypeHasArrayOfASerializedAsB>");
Assert.StrictEqual(x.Items[0].Name, y.Items[0].Name);
Assert.StrictEqual(x.Items[1].Name, y.Items[1].Name);
}
[Fact]
public static void DCS_WithDuplicateNames()
{
var x = new WithDuplicateNames(true);
var y = SerializeAndDeserialize<WithDuplicateNames>(x, @"<WithDuplicateNames xmlns=""http://schemas.datacontract.org/2004/07/SerializationTypes"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><ClassA1 xmlns:a=""http://schemas.datacontract.org/2004/07/DuplicateTypeNamesTest.ns1""><a:Name>Hello World! 漢 ñ</a:Name></ClassA1><ClassA2 xmlns:a=""http://schemas.datacontract.org/2004/07/DuplicateTypeNamesTest.ns2""><a:Nombre/></ClassA2><EnumA1>two</EnumA1><EnumA2>dos</EnumA2><StructA1 xmlns:a=""http://schemas.datacontract.org/2004/07/DuplicateTypeNamesTest.ns1""><a:Text/></StructA1><StructA2 xmlns:a=""http://schemas.datacontract.org/2004/07/DuplicateTypeNamesTest.ns2""><a:Texto/></StructA2></WithDuplicateNames>");
Assert.StrictEqual(x.ClassA1.Name, y.ClassA1.Name);
Assert.StrictEqual(x.StructA1, y.StructA1);
Assert.StrictEqual(x.EnumA1, y.EnumA1);
Assert.StrictEqual(x.EnumA2, y.EnumA2);
Assert.StrictEqual(x.StructA2, y.StructA2);
}
[Fact]
public static void DCS_XElementAsRoot()
{
var original = new XElement("ElementName1");
original.SetAttributeValue(XName.Get("Attribute1"), "AttributeValue1");
original.SetValue("Value1");
var actual = SerializeAndDeserialize<XElement>(original, @"<ElementName1 Attribute1=""AttributeValue1"">Value1</ElementName1>");
VerifyXElementObject(original, actual);
}