[System] Fixes UdpClient.Receive with IPv6 endpoint
[mono.git] / mcs / class / System.ServiceModel.Web / Test / System.Runtime.Serialization.Json / DataContractJsonSerializerTest.cs
1 //
2 // DataContractJsonSerializerTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //      Ankit Jain <JAnkit@novell.com>
7 //      Antoine Cailliau <antoinecailliau@gmail.com>
8 //
9 // Copyright (C) 2005-2007 Novell, Inc.  http://www.novell.com
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 //
33 // This test code contains tests for DataContractJsonSerializer, which is
34 // imported from DataContractSerializerTest.cs.
35 //
36
37 using System;
38 using System.Collections;
39 using System.Collections.Generic;
40 using System.Collections.ObjectModel;
41 using System.IO;
42 using System.Net;
43 using System.Runtime.Serialization;
44 using System.Runtime.Serialization.Json;
45 using System.Text;
46 using System.Xml;
47 using NUnit.Framework;
48
49 namespace MonoTests.System.Runtime.Serialization.Json
50 {
51         [TestFixture]
52         public class DataContractJsonSerializerTest
53         {
54                 static readonly XmlWriterSettings settings;
55
56                 static DataContractJsonSerializerTest ()
57                 {
58                         settings = new XmlWriterSettings ();
59                         settings.OmitXmlDeclaration = true;
60                 }
61
62                 [DataContract]
63                 class Sample1
64                 {
65                         [DataMember]
66                         public string Member1;
67                 }
68
69                 [Test]
70                 [ExpectedException (typeof (ArgumentNullException))]
71                 public void ConstructorTypeNull ()
72                 {
73                         new DataContractJsonSerializer (null);
74                 }
75
76                 [Test]
77                 public void ConstructorKnownTypesNull ()
78                 {
79                         // null knownTypes is allowed.
80                         new DataContractJsonSerializer (typeof (Sample1), (IEnumerable<Type>) null);
81                         new DataContractJsonSerializer (typeof (Sample1), "Foo", null);
82                 }
83
84                 [Test]
85                 [ExpectedException (typeof (ArgumentNullException))]
86                 public void ConstructorNameNull ()
87                 {
88                         new DataContractJsonSerializer (typeof (Sample1), (string) null);
89                 }
90
91                 [Test]
92                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
93                 public void ConstructorNegativeMaxObjects ()
94                 {
95                         new DataContractJsonSerializer (typeof (Sample1), "Sample1",
96                                 null, -1, false, null, false);
97                 }
98
99                 [Test]
100                 public void ConstructorMisc ()
101                 {
102                         new DataContractJsonSerializer (typeof (GlobalSample1)).WriteObject (new MemoryStream (), new GlobalSample1 ());
103                 }
104
105                 [Test]
106                 public void WriteObjectContent ()
107                 {
108                         StringWriter sw = new StringWriter ();
109                         using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
110                                 DataContractJsonSerializer ser =
111                                         new DataContractJsonSerializer (typeof (string));
112                                 xw.WriteStartElement ("my-element");
113                                 ser.WriteObjectContent (xw, "TEST STRING");
114                                 xw.WriteEndElement ();
115                         }
116                         Assert.AreEqual ("<my-element>TEST STRING</my-element>",
117                                 sw.ToString ());
118                 }
119
120                 // int
121
122                 [Test]
123                 public void SerializeIntXml ()
124                 {
125                         StringWriter sw = new StringWriter ();
126                         SerializeInt (XmlWriter.Create (sw, settings));
127                         Assert.AreEqual (
128                                 @"<root type=""number"">1</root>",
129                                 sw.ToString ());
130                 }
131
132                 [Test]
133                 public void SerializeIntJson ()
134                 {
135                         MemoryStream ms = new MemoryStream ();
136                         SerializeInt (JsonReaderWriterFactory.CreateJsonWriter (ms));
137                         Assert.AreEqual (
138                                 "1",
139                                 Encoding.UTF8.GetString (ms.ToArray ()));
140                 }
141
142                 void SerializeInt (XmlWriter writer)
143                 {
144                         DataContractJsonSerializer ser =
145                                 new DataContractJsonSerializer (typeof (int));
146                         using (XmlWriter w = writer) {
147                                 ser.WriteObject (w, 1);
148                         }
149                 }
150
151                 // int, with rootName
152
153                 [Test]
154                 public void SerializeIntXmlWithRootName ()
155                 {
156                         StringWriter sw = new StringWriter ();
157                         SerializeIntWithRootName (XmlWriter.Create (sw, settings));
158                         Assert.AreEqual (
159                                 @"<myroot type=""number"">1</myroot>",
160                                 sw.ToString ());
161                 }
162
163                 [Test]
164                 // since JsonWriter supports only "root" as the root name, using
165                 // XmlWriter from JsonReaderWriterFactory will always fail with
166                 // an explicit rootName.
167                 [ExpectedException (typeof (SerializationException))]
168                 public void SerializeIntJsonWithRootName ()
169                 {
170                         MemoryStream ms = new MemoryStream ();
171                         SerializeIntWithRootName (JsonReaderWriterFactory.CreateJsonWriter (ms));
172                         Assert.AreEqual (
173                                 "1",
174                                 Encoding.UTF8.GetString (ms.ToArray ()));
175                 }
176
177                 void SerializeIntWithRootName (XmlWriter writer)
178                 {
179                         DataContractJsonSerializer ser =
180                                 new DataContractJsonSerializer (typeof (int), "myroot");
181                         using (XmlWriter w = writer) {
182                                 ser.WriteObject (w, 1);
183                         }
184                 }
185
186                 // pass typeof(DCEmpty), serialize int
187
188                 [Test]
189                 public void SerializeIntForDCEmptyXml ()
190                 {
191                         StringWriter sw = new StringWriter ();
192                         SerializeIntForDCEmpty (XmlWriter.Create (sw, settings));
193                         Assert.AreEqual (
194                                 @"<root type=""number"">1</root>",
195                                 sw.ToString ());
196                 }
197
198                 [Test]
199                 public void SerializeIntForDCEmptyJson ()
200                 {
201                         MemoryStream ms = new MemoryStream ();
202                         SerializeIntForDCEmpty (JsonReaderWriterFactory.CreateJsonWriter (ms));
203                         Assert.AreEqual (
204                                 "1",
205                                 Encoding.UTF8.GetString (ms.ToArray ()));
206                 }
207
208                 void SerializeIntForDCEmpty (XmlWriter writer)
209                 {
210                         DataContractJsonSerializer ser =
211                                 new DataContractJsonSerializer (typeof (DCEmpty));
212                         using (XmlWriter w = writer) {
213                                 ser.WriteObject (w, 1);
214                         }
215                 }
216
217                 // DCEmpty
218
219                 [Test]
220                 public void SerializeEmptyClassXml ()
221                 {
222                         StringWriter sw = new StringWriter ();
223                         SerializeEmptyClass (XmlWriter.Create (sw, settings));
224                         Assert.AreEqual (
225                                 @"<root type=""object"" />",
226                                 sw.ToString ());
227                 }
228
229                 [Test]
230                 public void SerializeEmptyClassJson ()
231                 {
232                         MemoryStream ms = new MemoryStream ();
233                         SerializeEmptyClass (JsonReaderWriterFactory.CreateJsonWriter (ms));
234                         Assert.AreEqual (
235                                 "{}",
236                                 Encoding.UTF8.GetString (ms.ToArray ()));
237                 }
238
239                 void SerializeEmptyClass (XmlWriter writer)
240                 {
241                         DataContractJsonSerializer ser =
242                                 new DataContractJsonSerializer (typeof (DCEmpty));
243                         using (XmlWriter w = writer) {
244                                 ser.WriteObject (w, new DCEmpty ());
245                         }
246                 }
247
248                 // string (primitive)
249
250                 [Test]
251                 public void SerializePrimitiveStringXml ()
252                 {
253                         StringWriter sw = new StringWriter ();
254                         SerializePrimitiveString (XmlWriter.Create (sw, settings));
255                         Assert.AreEqual (
256                                 "<root>TEST</root>",
257                                 sw.ToString ());
258                 }
259
260                 [Test]
261                 public void SerializePrimitiveStringJson ()
262                 {
263                         MemoryStream ms = new MemoryStream ();
264                         SerializePrimitiveString (JsonReaderWriterFactory.CreateJsonWriter (ms));
265                         Assert.AreEqual (
266                                 @"""TEST""",
267                                 Encoding.UTF8.GetString (ms.ToArray ()));
268                 }
269
270                 void SerializePrimitiveString (XmlWriter writer)
271                 {
272                         XmlObjectSerializer ser =
273                                 new DataContractJsonSerializer (typeof (string));
274                         using (XmlWriter w = writer) {
275                                 ser.WriteObject (w, "TEST");
276                         }
277                 }
278
279                 // QName (primitive but ...)
280
281                 [Test]
282                 public void SerializePrimitiveQNameXml ()
283                 {
284                         StringWriter sw = new StringWriter ();
285                         SerializePrimitiveQName (XmlWriter.Create (sw, settings));
286                         Assert.AreEqual (
287                                 "<root>foo:urn:foo</root>",
288                                 sw.ToString ());
289                 }
290
291                 [Test]
292                 public void SerializePrimitiveQNameJson ()
293                 {
294                         MemoryStream ms = new MemoryStream ();
295                         SerializePrimitiveQName (JsonReaderWriterFactory.CreateJsonWriter (ms));
296                         Assert.AreEqual (
297                                 @"""foo:urn:foo""",
298                                 Encoding.UTF8.GetString (ms.ToArray ()));
299                 }
300
301                 void SerializePrimitiveQName (XmlWriter writer)
302                 {
303                         XmlObjectSerializer ser =
304                                 new DataContractJsonSerializer (typeof (XmlQualifiedName));
305                         using (XmlWriter w = writer) {
306                                 ser.WriteObject (w, new XmlQualifiedName ("foo", "urn:foo"));
307                         }
308                 }
309
310                 // DBNull (primitive)
311
312                 [Test]
313                 public void SerializeDBNullXml ()
314                 {
315                         StringWriter sw = new StringWriter ();
316                         SerializeDBNull (XmlWriter.Create (sw, settings));
317                         Assert.AreEqual (
318                                 @"<root type=""object"" />",
319                                 sw.ToString ());
320                 }
321
322                 [Test]
323                 public void SerializeDBNullJson ()
324                 {
325                         MemoryStream ms = new MemoryStream ();
326                         SerializeDBNull (JsonReaderWriterFactory.CreateJsonWriter (ms));
327                         Assert.AreEqual (
328                                 "{}",
329                                 Encoding.UTF8.GetString (ms.ToArray ()));
330                 }
331
332                 void SerializeDBNull (XmlWriter writer)
333                 {
334                         DataContractJsonSerializer ser =
335                                 new DataContractJsonSerializer (typeof (DBNull));
336                         using (XmlWriter w = writer) {
337                                 ser.WriteObject (w, DBNull.Value);
338                         }
339                 }
340
341                 // DCSimple1
342
343                 [Test]
344                 public void SerializeSimpleClass1Xml ()
345                 {
346                         StringWriter sw = new StringWriter ();
347                         SerializeSimpleClass1 (XmlWriter.Create (sw, settings));
348                         Assert.AreEqual (
349                                 @"<root type=""object""><Foo>TEST</Foo></root>",
350                                 sw.ToString ());
351                 }
352
353                 [Test]
354                 public void SerializeSimpleClass1Json ()
355                 {
356                         MemoryStream ms = new MemoryStream ();
357                         SerializeSimpleClass1 (JsonReaderWriterFactory.CreateJsonWriter (ms));
358                         Assert.AreEqual (
359                                 @"{""Foo"":""TEST""}",
360                                 Encoding.UTF8.GetString (ms.ToArray ()));
361                 }
362
363                 void SerializeSimpleClass1 (XmlWriter writer)
364                 {
365                         DataContractJsonSerializer ser =
366                                 new DataContractJsonSerializer (typeof (DCSimple1));
367                         using (XmlWriter w = writer) {
368                                 ser.WriteObject (w, new DCSimple1 ());
369                         }
370                 }
371
372                 // NonDC
373
374                 [Test]
375                 // NonDC is not a DataContract type.
376                 public void SerializeNonDCOnlyCtor ()
377                 {
378                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (NonDC));
379                 }
380
381                 [Test]
382                 //[ExpectedException (typeof (InvalidDataContractException))]
383                 // NonDC is not a DataContract type.
384                 // UPDATE: non-DataContract types are became valid in RTM.
385                 public void SerializeNonDC ()
386                 {
387                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (NonDC));
388                         using (XmlWriter w = XmlWriter.Create (TextWriter.Null, settings)) {
389                                 ser.WriteObject (w, new NonDC ());
390                         }
391                 }
392
393                 // DCHasNonDC
394
395                 [Test]
396                 //[ExpectedException (typeof (InvalidDataContractException))]
397                 // DCHasNonDC itself is a DataContract type whose field is
398                 // marked as DataMember but its type is not DataContract.
399                 // UPDATE: non-DataContract types are became valid in RTM.
400                 public void SerializeDCHasNonDC ()
401                 {
402                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCHasNonDC));
403                         using (XmlWriter w = XmlWriter.Create (TextWriter.Null, settings)) {
404                                 ser.WriteObject (w, new DCHasNonDC ());
405                         }
406                 }
407
408                 // DCHasSerializable
409
410                 [Test]
411                 public void SerializeSimpleSerializable1Xml ()
412                 {
413                         StringWriter sw = new StringWriter ();
414                         SerializeSimpleSerializable1 (XmlWriter.Create (sw, settings));
415                         Assert.AreEqual (
416                                 @"<root type=""object""><Ser type=""object""><Doh>doh!</Doh></Ser></root>",
417                                 sw.ToString ());
418                 }
419
420                 [Test]
421                 public void SerializeSimpleSerializable1Json ()
422                 {
423                         MemoryStream ms = new MemoryStream ();
424                         SerializeSimpleSerializable1 (JsonReaderWriterFactory.CreateJsonWriter (ms));
425                         Assert.AreEqual (
426                                 @"{""Ser"":{""Doh"":""doh!""}}",
427                                 Encoding.UTF8.GetString (ms.ToArray ()));
428                 }
429
430                 // DCHasSerializable itself is DataContract and has a field
431                 // whose type is not contract but serializable.
432                 void SerializeSimpleSerializable1 (XmlWriter writer)
433                 {
434                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCHasSerializable));
435                         using (XmlWriter w = writer) {
436                                 ser.WriteObject (w, new DCHasSerializable ());
437                         }
438                 }
439
440                 [Test]
441                 public void SerializeDCWithNameXml ()
442                 {
443                         StringWriter sw = new StringWriter ();
444                         SerializeDCWithName (XmlWriter.Create (sw, settings));
445                         Assert.AreEqual (
446                                 @"<root type=""object""><FooMember>value</FooMember></root>",
447                                 sw.ToString ());
448                 }
449
450                 [Test]
451                 public void SerializeDCWithNameJson ()
452                 {
453                         MemoryStream ms = new MemoryStream ();
454                         SerializeDCWithName (JsonReaderWriterFactory.CreateJsonWriter (ms));
455                         Assert.AreEqual (
456                                 @"{""FooMember"":""value""}",
457                                 Encoding.UTF8.GetString (ms.ToArray ()));
458                 }
459
460                 void SerializeDCWithName (XmlWriter writer)
461                 {
462                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithName));
463                         using (XmlWriter w = writer) {
464                                 ser.WriteObject (w, new DCWithName ());
465                         }
466                 }
467
468                 [Test]
469                 public void SerializeDCWithEmptyName1 ()
470                 {
471                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEmptyName));
472                         StringWriter sw = new StringWriter ();
473                         DCWithEmptyName dc = new DCWithEmptyName ();
474                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
475                                 try {
476                                         ser.WriteObject (w, dc);
477                                 } catch (InvalidDataContractException) {
478                                         return;
479                                 }
480                         }
481                         Assert.Fail ("Expected InvalidDataContractException");
482                 }
483
484                 [Test]
485                 public void SerializeDCWithEmptyName2 ()
486                 {
487                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithName));
488                         StringWriter sw = new StringWriter ();
489
490                         /* DataContractAttribute.Name == "", not valid */
491                         DCWithEmptyName dc = new DCWithEmptyName ();
492                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
493                                 try {
494                                         ser.WriteObject (w, dc);
495                                 } catch (InvalidDataContractException) {
496                                         return;
497                                 }
498                         }
499                         Assert.Fail ("Expected InvalidDataContractException");
500                 }
501
502                 [Test]
503                 [Category("NotWorking")]
504                 public void SerializeDCWithNullName ()
505                 {
506                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithNullName));
507                         StringWriter sw = new StringWriter ();
508                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
509                                 try {
510                                         /* DataContractAttribute.Name == "", not valid */
511                                         ser.WriteObject (w, new DCWithNullName ());
512                                 } catch (InvalidDataContractException) {
513                                         return;
514                                 }
515                         }
516                         Assert.Fail ("Expected InvalidDataContractException");
517                 }
518
519                 [Test]
520                 public void SerializeDCWithEmptyNamespace1 ()
521                 {
522                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEmptyNamespace));
523                         StringWriter sw = new StringWriter ();
524                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
525                                 ser.WriteObject (w, new DCWithEmptyNamespace ());
526                         }
527                 }
528
529                 [Test]
530                 public void SerializeWrappedClassXml ()
531                 {
532                         StringWriter sw = new StringWriter ();
533                         SerializeWrappedClass (XmlWriter.Create (sw, settings));
534                         Assert.AreEqual (
535                                 @"<root type=""object"" />",
536                                 sw.ToString ());
537                 }
538
539                 [Test]
540                 public void SerializeWrappedClassJson ()
541                 {
542                         MemoryStream ms = new MemoryStream ();
543                         SerializeWrappedClass (JsonReaderWriterFactory.CreateJsonWriter (ms));
544                         Assert.AreEqual (
545                                 "{}",
546                                 Encoding.UTF8.GetString (ms.ToArray ()));
547                 }
548
549                 void SerializeWrappedClass (XmlWriter writer)
550                 {
551                         DataContractJsonSerializer ser =
552                                 new DataContractJsonSerializer (typeof (Wrapper.DCWrapped));
553                         using (XmlWriter w = writer) {
554                                 ser.WriteObject (w, new Wrapper.DCWrapped ());
555                         }
556                 }
557
558                 // CollectionContainer : Items must have a setter. (but became valid in RTM).
559                 [Test]
560                 public void SerializeReadOnlyCollectionMember ()
561                 {
562                         DataContractJsonSerializer ser =
563                                 new DataContractJsonSerializer (typeof (CollectionContainer));
564                         StringWriter sw = new StringWriter ();
565                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
566                                 ser.WriteObject (w, null);
567                         }
568                 }
569
570                 // DataCollectionContainer : Items must have a setter. (but became valid in RTM).
571                 [Test]
572                 public void SerializeReadOnlyDataCollectionMember ()
573                 {
574                         DataContractJsonSerializer ser =
575                                 new DataContractJsonSerializer (typeof (DataCollectionContainer));
576                         StringWriter sw = new StringWriter ();
577                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
578                                 ser.WriteObject (w, null);
579                         }
580                 }
581
582                 [Test]
583                 [Ignore ("https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=409970")]
584                 [ExpectedException (typeof (SerializationException))]
585                 public void DeserializeReadOnlyDataCollection_NullCollection ()
586                 {
587                         DataContractJsonSerializer ser =
588                                 new DataContractJsonSerializer (typeof (CollectionContainer));
589                         StringWriter sw = new StringWriter ();
590                         var c = new CollectionContainer ();
591                         c.Items.Add ("foo");
592                         c.Items.Add ("bar");
593                         using (XmlWriter w = XmlWriter.Create (sw, settings))
594                                 ser.WriteObject (w, c);
595                         // CollectionContainer.Items is null, so it cannot deserialize non-null collection.
596                         using (XmlReader r = XmlReader.Create (new StringReader (sw.ToString ())))
597                                 c = (CollectionContainer) ser.ReadObject (r);
598                 }
599
600                 [Test]
601                 public void SerializeGuidXml ()
602                 {
603                         StringWriter sw = new StringWriter ();
604                         SerializeGuid (XmlWriter.Create (sw, settings));
605                         Assert.AreEqual (
606                                 @"<root>00000000-0000-0000-0000-000000000000</root>",
607                                 sw.ToString ());
608                 }
609
610                 [Test]
611                 public void SerializeGuidJson ()
612                 {
613                         MemoryStream ms = new MemoryStream ();
614                         SerializeGuid (JsonReaderWriterFactory.CreateJsonWriter (ms));
615                         Assert.AreEqual (
616                                 @"""00000000-0000-0000-0000-000000000000""",
617                                 Encoding.UTF8.GetString (ms.ToArray ()));
618                 }
619
620                 void SerializeGuid (XmlWriter writer)
621                 {
622                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Guid));
623                         using (XmlWriter w = writer) {
624                                 ser.WriteObject (w, Guid.Empty);
625                         }
626                 }
627
628                 [Test]
629                 public void SerializeEnumXml ()
630                 {
631                         StringWriter sw = new StringWriter ();
632                         SerializeEnum (XmlWriter.Create (sw, settings));
633                         Assert.AreEqual (
634                                 @"<root type=""number"">0</root>",
635                                 sw.ToString ());
636                 }
637
638                 [Test]
639                 public void SerializeEnumJson ()
640                 {
641                         MemoryStream ms = new MemoryStream ();
642                         SerializeEnum (JsonReaderWriterFactory.CreateJsonWriter (ms));
643                         Assert.AreEqual (
644                                 "0",
645                                 Encoding.UTF8.GetString (ms.ToArray ()));
646                 }
647
648                 void SerializeEnum (XmlWriter writer)
649                 {
650                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Colors));
651                         using (XmlWriter w = writer) {
652                                 ser.WriteObject (w, new Colors ());
653                         }
654                 }
655
656                 [Test]
657                 public void SerializeEnum2Xml ()
658                 {
659                         StringWriter sw = new StringWriter ();
660                         SerializeEnum2 (XmlWriter.Create (sw, settings));
661                         Assert.AreEqual (
662                                 @"<root type=""number"">0</root>",
663                                 sw.ToString ());
664                 }
665
666                 [Test]
667                 public void SerializeEnum2Json ()
668                 {
669                         MemoryStream ms = new MemoryStream ();
670                         SerializeEnum2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
671                         Assert.AreEqual (
672                                 "0",
673                                 Encoding.UTF8.GetString (ms.ToArray ()));
674                 }
675
676                 void SerializeEnum2 (XmlWriter writer)
677                 {
678                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Colors));
679                         using (XmlWriter w = writer) {
680                                 ser.WriteObject (w, 0);
681                         }
682                 }
683
684                 [Test] // so, DataContract does not affect here.
685                 public void SerializeEnumWithDCXml ()
686                 {
687                         StringWriter sw = new StringWriter ();
688                         SerializeEnumWithDC (XmlWriter.Create (sw, settings));
689                         Assert.AreEqual (
690                                 @"<root type=""number"">0</root>",
691                                 sw.ToString ());
692                 }
693
694                 [Test]
695                 public void SerializeEnumWithDCJson ()
696                 {
697                         MemoryStream ms = new MemoryStream ();
698                         SerializeEnumWithDC (JsonReaderWriterFactory.CreateJsonWriter (ms));
699                         Assert.AreEqual (
700                                 "0",
701                                 Encoding.UTF8.GetString (ms.ToArray ()));
702                 }
703
704                 void SerializeEnumWithDC (XmlWriter writer)
705                 {
706                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
707                         using (XmlWriter w = writer) {
708                                 ser.WriteObject (w, new ColorsWithDC ());
709                         }
710                 }
711
712                 [Test]
713                 public void SerializeEnumWithNoDCXml ()
714                 {
715                         StringWriter sw = new StringWriter ();
716                         SerializeEnumWithNoDC (XmlWriter.Create (sw, settings));
717                         Assert.AreEqual (
718                                 @"<root type=""number"">0</root>",
719                                 sw.ToString ());
720                 }
721
722                 [Test]
723                 public void SerializeEnumWithNoDCJson ()
724                 {
725                         MemoryStream ms = new MemoryStream ();
726                         SerializeEnumWithNoDC (JsonReaderWriterFactory.CreateJsonWriter (ms));
727                         Assert.AreEqual (
728                                 "0",
729                                 Encoding.UTF8.GetString (ms.ToArray ()));
730                 }
731
732                 void SerializeEnumWithNoDC (XmlWriter writer)
733                 {
734                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsEnumMemberNoDC));
735                         using (XmlWriter w = writer) {
736                                 ser.WriteObject (w, new ColorsEnumMemberNoDC ());
737                         }
738                 }
739
740                 [Test]
741                 public void SerializeEnumWithDC2Xml ()
742                 {
743                         StringWriter sw = new StringWriter ();
744                         SerializeEnumWithDC2 (XmlWriter.Create (sw, settings));
745                         Assert.AreEqual (
746                                 @"<root type=""number"">3</root>",
747                                 sw.ToString ());
748                 }
749
750                 [Test]
751                 public void SerializeEnumWithDC2Json ()
752                 {
753                         MemoryStream ms = new MemoryStream ();
754                         SerializeEnumWithDC2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
755                         Assert.AreEqual (
756                                 "3",
757                                 Encoding.UTF8.GetString (ms.ToArray ()));
758                 }
759
760                 void SerializeEnumWithDC2 (XmlWriter writer)
761                 {
762                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
763                         using (XmlWriter w = writer) {
764                                 ser.WriteObject (w, 3);
765                         }
766                 }
767
768 /*
769                 [Test]
770                 [ExpectedException (typeof (SerializationException))]
771                 public void SerializeEnumWithDCInvalid ()
772                 {
773                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
774                         StringWriter sw = new StringWriter ();
775                         ColorsWithDC cdc = ColorsWithDC.Blue;
776                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
777                                 ser.WriteObject (w, cdc);
778                         }
779                 }
780 */
781
782                 [Test]
783                 public void SerializeDCWithEnumXml ()
784                 {
785                         StringWriter sw = new StringWriter ();
786                         SerializeDCWithEnum (XmlWriter.Create (sw, settings));
787                         Assert.AreEqual (
788                                 @"<root type=""object""><_colors type=""number"">0</_colors></root>",
789                                 sw.ToString ());
790                 }
791
792                 [Test]
793                 public void SerializeDCWithEnumJson ()
794                 {
795                         MemoryStream ms = new MemoryStream ();
796                         SerializeDCWithEnum (JsonReaderWriterFactory.CreateJsonWriter (ms));
797                         Assert.AreEqual (
798                                 @"{""_colors"":0}",
799                                 Encoding.UTF8.GetString (ms.ToArray ()));
800                 }
801
802                 void SerializeDCWithEnum (XmlWriter writer)
803                 {
804                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEnum));
805                         using (XmlWriter w = writer) {
806                                 ser.WriteObject (w, new DCWithEnum ());
807                         }
808                 }
809
810                 [Test]
811                 public void SerializerDCArrayXml ()
812                 {
813                         StringWriter sw = new StringWriter ();
814                         SerializerDCArray (XmlWriter.Create (sw, settings));
815                         Assert.AreEqual (
816                                 @"<root type=""array""><item type=""object""><_colors type=""number"">0</_colors></item><item type=""object""><_colors type=""number"">1</_colors></item></root>",
817                                 sw.ToString ());
818                 }
819
820                 [Test]
821                 public void SerializerDCArrayJson ()
822                 {
823                         MemoryStream ms = new MemoryStream ();
824                         SerializerDCArray (JsonReaderWriterFactory.CreateJsonWriter (ms));
825                         Assert.AreEqual (
826                                 @"[{""_colors"":0},{""_colors"":1}]",
827                                 Encoding.UTF8.GetString (ms.ToArray ()));
828                 }
829
830                 void SerializerDCArray (XmlWriter writer)
831                 {
832                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEnum []));
833                         DCWithEnum [] arr = new DCWithEnum [2];
834                         arr [0] = new DCWithEnum (); arr [0].colors = Colors.Red;
835                         arr [1] = new DCWithEnum (); arr [1].colors = Colors.Green;
836                         using (XmlWriter w = writer) {
837                                 ser.WriteObject (w, arr);
838                         }
839                 }
840
841                 [Test]
842                 public void SerializerDCArray2Xml ()
843                 {
844                         StringWriter sw = new StringWriter ();
845                         SerializerDCArray2 (XmlWriter.Create (sw, settings));
846                         Assert.AreEqual (
847                                 @"<root type=""array""><item __type=""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"" type=""object""><_colors type=""number"">0</_colors></item><item __type=""DCSimple1:#MonoTests.System.Runtime.Serialization.Json"" type=""object""><Foo>hello</Foo></item></root>",
848                                 sw.ToString ());
849                 }
850
851                 [Test]
852                 public void SerializerDCArray2Json ()
853                 {
854                         MemoryStream ms = new MemoryStream ();
855                         SerializerDCArray2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
856                         Assert.AreEqual (
857                                 @"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0},{""__type"":""DCSimple1:#MonoTests.System.Runtime.Serialization.Json"",""Foo"":""hello""}]",
858                                 Encoding.UTF8.GetString (ms.ToArray ()));
859                 }
860
861                 void SerializerDCArray2 (XmlWriter writer)
862                 {
863                         List<Type> known = new List<Type> ();
864                         known.Add (typeof (DCWithEnum));
865                         known.Add (typeof (DCSimple1));
866                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (object []), known);
867                         object [] arr = new object [2];
868                         arr [0] = new DCWithEnum (); ((DCWithEnum)arr [0]).colors = Colors.Red;
869                         arr [1] = new DCSimple1 (); ((DCSimple1) arr [1]).Foo = "hello";
870
871                         using (XmlWriter w = writer) {
872                                 ser.WriteObject (w, arr);
873                         }
874                 }
875
876                 [Test]
877                 public void SerializerDCArray3Xml ()
878                 {
879                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (int []));
880                         StringWriter sw = new StringWriter ();
881                         int [] arr = new int [2];
882                         arr [0] = 1; arr [1] = 2;
883
884                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
885                                 ser.WriteObject (w, arr);
886                         }
887
888                         Assert.AreEqual (
889                                 @"<root type=""array""><item type=""number"">1</item><item type=""number"">2</item></root>",
890                                 sw.ToString ());
891                 }
892
893                 [Test]
894                 public void SerializerDCArray3Json ()
895                 {
896                         MemoryStream ms = new MemoryStream ();
897                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (int []));
898                         int [] arr = new int [2];
899                         arr [0] = 1; arr [1] = 2;
900
901                         using (XmlWriter w = JsonReaderWriterFactory.CreateJsonWriter (ms)) {
902                                 ser.WriteObject (w, arr);
903                         }
904
905                         Assert.AreEqual (
906                                 @"[1,2]",
907                                 Encoding.UTF8.GetString (ms.ToArray ()));
908                 }
909
910                 [Test]
911                 // ... so, non-JSON XmlWriter is still accepted.
912                 public void SerializeNonDCArrayXml ()
913                 {
914                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (SerializeNonDCArrayType));
915                         StringWriter sw = new StringWriter ();
916                         using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
917                                 ser.WriteObject (xw, new SerializeNonDCArrayType ());
918                         }
919                         Assert.AreEqual (@"<root type=""object""><IPAddresses type=""array"" /></root>",
920                                 sw.ToString ());
921                 }
922
923                 [Test]
924                 public void SerializeNonDCArrayJson ()
925                 {
926                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (SerializeNonDCArrayType));
927                         MemoryStream ms = new MemoryStream ();
928                         using (XmlWriter xw = JsonReaderWriterFactory.CreateJsonWriter (ms)) {
929                                 ser.WriteObject (xw, new SerializeNonDCArrayType ());
930                         }
931                         Assert.AreEqual (@"{""IPAddresses"":[]}",
932                                 Encoding.UTF8.GetString (ms.ToArray ()));
933                 }
934
935                 [Test]
936                 public void SerializeNonDCArrayItems ()
937                 {
938                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (SerializeNonDCArrayType));
939                         StringWriter sw = new StringWriter ();
940                         using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
941                                 SerializeNonDCArrayType obj = new SerializeNonDCArrayType ();
942                                 obj.IPAddresses = new NonDCItem [] {new NonDCItem () { Data = new byte [] {1, 2, 3, 4} } };
943                                 ser.WriteObject (xw, obj);
944                         }
945
946                         XmlDocument doc = new XmlDocument ();
947                         doc.LoadXml (sw.ToString ());
948                         XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
949                         nsmgr.AddNamespace ("s", "http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization");
950                         nsmgr.AddNamespace ("n", "http://schemas.datacontract.org/2004/07/System.Net");
951                         nsmgr.AddNamespace ("a", "http://schemas.microsoft.com/2003/10/Serialization/Arrays");
952
953                         Assert.AreEqual (1, doc.SelectNodes ("/root/IPAddresses/item", nsmgr).Count, "#1");
954                         XmlElement el = doc.SelectSingleNode ("/root/IPAddresses/item/Data", nsmgr) as XmlElement;
955                         Assert.IsNotNull (el, "#3");
956                         Assert.AreEqual (4, el.SelectNodes ("item", nsmgr).Count, "#4");
957                 }
958
959                 [Test]
960                 public void MaxItemsInObjectGraph1 ()
961                 {
962                         // object count == maximum
963                         DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCEmpty), null, 1, false, null, false);
964                         s.WriteObject (XmlWriter.Create (TextWriter.Null), new DCEmpty ());
965                 }
966
967                 [Test]
968                 [ExpectedException (typeof (SerializationException))]
969                 public void MaxItemsInObjectGraph2 ()
970                 {
971                         // object count > maximum
972                         DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCSimple1), null, 1, false, null, false);
973                         s.WriteObject (XmlWriter.Create (TextWriter.Null), new DCSimple1 ());
974                 }
975
976                 [Test]
977                 public void DeserializeString ()
978                 {
979                         Assert.AreEqual ("ABC", Deserialize ("\"ABC\"", typeof (string)));
980                 }
981
982                 [Test]
983                 public void DeserializeInt ()
984                 {
985                         Assert.AreEqual (5, Deserialize ("5", typeof (int)));
986                 }
987
988                 [Test]
989                 public void DeserializeArray ()
990                 {
991                         int [] ret = (int []) Deserialize ("[5,6,7]", typeof (int []));
992                         Assert.AreEqual (5, ret [0], "#1");
993                         Assert.AreEqual (6, ret [1], "#2");
994                         Assert.AreEqual (7, ret [2], "#3");
995                 }
996
997                 [Test]
998                 public void DeserializeArrayUntyped ()
999                 {
1000                         object [] ret = (object []) Deserialize ("[5,6,7]", typeof (object []));
1001                         Assert.AreEqual (5, ret [0], "#1");
1002                         Assert.AreEqual (6, ret [1], "#2");
1003                         Assert.AreEqual (7, ret [2], "#3");
1004                 }
1005
1006                 [Test]
1007                 public void DeserializeMixedArray ()
1008                 {
1009                         object [] ret = (object []) Deserialize ("[5,\"6\",false]", typeof (object []));
1010                         Assert.AreEqual (5, ret [0], "#1");
1011                         Assert.AreEqual ("6", ret [1], "#2");
1012                         Assert.AreEqual (false, ret [2], "#3");
1013                 }
1014
1015                 [Test]
1016                 [ExpectedException (typeof (SerializationException))]
1017                 public void DeserializeEmptyAsString ()
1018                 {
1019                         // it somehow expects "root" which should have been already consumed.
1020                         Deserialize ("", typeof (string));
1021                 }
1022
1023                 [Test]
1024                 [ExpectedException (typeof (SerializationException))]
1025                 public void DeserializeEmptyAsInt ()
1026                 {
1027                         // it somehow expects "root" which should have been already consumed.
1028                         Deserialize ("", typeof (int));
1029                 }
1030
1031                 [Test]
1032                 [ExpectedException (typeof (SerializationException))]
1033                 public void DeserializeEmptyAsDBNull ()
1034                 {
1035                         // it somehow expects "root" which should have been already consumed.
1036                         Deserialize ("", typeof (DBNull));
1037                 }
1038
1039                 [Test]
1040                 public void DeserializeEmptyObjectAsString ()
1041                 {
1042                         // looks like it is converted to ""
1043                         Assert.AreEqual (String.Empty, Deserialize ("{}", typeof (string)));
1044                 }
1045
1046                 [Test]
1047                 [ExpectedException (typeof (SerializationException))]
1048                 public void DeserializeEmptyObjectAsInt ()
1049                 {
1050                         Deserialize ("{}", typeof (int));
1051                 }
1052
1053                 [Test]
1054                 public void DeserializeEmptyObjectAsDBNull ()
1055                 {
1056                         Assert.AreEqual (DBNull.Value, Deserialize ("{}", typeof (DBNull)));
1057                 }
1058
1059                 [Test]
1060                 [ExpectedException (typeof (SerializationException))]
1061                 public void DeserializeEnumByName ()
1062                 {
1063                         // enum is parsed into long
1064                         Deserialize (@"""Red""", typeof (Colors));
1065                 }
1066
1067                 [Test]
1068                 public void DeserializeEnum2 ()
1069                 {
1070                         object o = Deserialize ("0", typeof (Colors));
1071
1072                         Assert.AreEqual (typeof (Colors), o.GetType (), "#de3");
1073                         Colors c = (Colors) o;
1074                         Assert.AreEqual (Colors.Red, c, "#de4");
1075                 }
1076                 
1077                 [Test]
1078                 [ExpectedException (typeof (SerializationException))]
1079                 public void DeserializeEnumInvalid ()
1080                 {
1081                         Deserialize ("", typeof (Colors));
1082                 }
1083
1084                 [Test]
1085                 [ExpectedException (typeof (SerializationException))]
1086                 [Ignore ("NotDotNet")] // 0.0 is an invalid Colors value.
1087                 public void DeserializeEnumInvalid3 ()
1088                 {
1089                         //"0.0" instead of "0"
1090                         Deserialize (
1091                                 "0.0",
1092                                 typeof (Colors));
1093                 }
1094
1095                 [Test]
1096                 public void DeserializeEnumWithDC ()
1097                 {
1098                         object o = Deserialize ("0", typeof (ColorsWithDC));
1099                         
1100                         Assert.AreEqual (typeof (ColorsWithDC), o.GetType (), "#de5");
1101                         ColorsWithDC cdc = (ColorsWithDC) o;
1102                         Assert.AreEqual (ColorsWithDC.Red, o, "#de6");
1103                 }
1104
1105                 [Test]
1106                 [ExpectedException (typeof (SerializationException))]
1107                 [Ignore ("NotDotNet")] // 4 is an invalid Colors value.
1108                 [Category ("NotWorking")]
1109                 public void DeserializeEnumWithDCInvalid ()
1110                 {
1111                         Deserialize (
1112                                 "4",
1113                                 typeof (ColorsWithDC));
1114                 }
1115
1116                 [Test]
1117                 public void DeserializeDCWithEnum ()
1118                 {
1119                         object o = Deserialize (
1120                                 "{\"_colors\":0}",
1121                                 typeof (DCWithEnum));
1122
1123                         Assert.AreEqual (typeof (DCWithEnum), o.GetType (), "#de7");
1124                         DCWithEnum dc = (DCWithEnum) o;
1125                         Assert.AreEqual (Colors.Red, dc.colors, "#de8");
1126                 }
1127
1128                 [Test]
1129                 public void ReadObjectVerifyObjectNameFalse ()
1130                 {
1131                         string xml = @"<any><Member1>bar</Member1></any>";
1132                         object o = new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
1133                                 .ReadObject (XmlReader.Create (new StringReader (xml)), false);
1134                         Assert.IsTrue (o is VerifyObjectNameTestData, "#1");
1135
1136                         string xml2 = @"<any><x:Member1 xmlns:x=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">bar</x:Member1></any>";
1137                         o = new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
1138                                 .ReadObject (XmlReader.Create (new StringReader (xml2)), false);
1139                         Assert.IsTrue (o is VerifyObjectNameTestData, "#2");
1140                 }
1141
1142                 [Test]
1143                 [ExpectedException (typeof (SerializationException))]
1144                 public void ReadObjectVerifyObjectNameTrue ()
1145                 {
1146                         string xml = @"<any><Member1>bar</Member1></any>";
1147                         new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
1148                                 .ReadObject (XmlReader.Create (new StringReader (xml)), true);
1149                 }
1150
1151                 [Test] // member name is out of scope
1152                 public void ReadObjectVerifyObjectNameTrue2 ()
1153                 {
1154                         string xml = @"<root><Member2>bar</Member2></root>";
1155                         new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
1156                                 .ReadObject (XmlReader.Create (new StringReader (xml)), true);
1157                 }
1158
1159                 [Test]
1160                 public void ReadTypedObjectJson ()
1161                 {
1162                         object o = Deserialize (@"{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0}", typeof (DCWithEnum));
1163                         Assert.AreEqual (typeof (DCWithEnum), o.GetType ());
1164                 }
1165
1166                 [Test]
1167                 public void ReadObjectDCArrayJson ()
1168                 {
1169                         object o = Deserialize (@"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0}]",
1170                                 typeof (object []), typeof (DCWithEnum));
1171                         Assert.AreEqual (typeof (object []), o.GetType (), "#1");
1172                         object [] arr = (object []) o;
1173                         Assert.AreEqual (typeof (DCWithEnum), arr [0].GetType (), "#2");
1174                 }
1175
1176                 [Test]
1177                 public void ReadObjectDCArray2Json ()
1178                 {
1179                         object o = Deserialize (@"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0},{""__type"":""DCSimple1:#MonoTests.System.Runtime.Serialization.Json"",""Foo"":""hello""}]",
1180                                 typeof (object []), typeof (DCWithEnum), typeof (DCSimple1));
1181                         Assert.AreEqual (typeof (object []), o.GetType (), "#1");
1182                         object [] arr = (object []) o;
1183                         Assert.AreEqual (typeof (DCWithEnum), arr [0].GetType (), "#2");
1184                         Assert.AreEqual (typeof (DCSimple1), arr [1].GetType (), "#3");
1185                 }
1186
1187                 private object Deserialize (string xml, Type type, params Type [] knownTypes)
1188                 {
1189                         DataContractJsonSerializer ser = new DataContractJsonSerializer (type, knownTypes);
1190                         XmlReader xr = JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (xml), new XmlDictionaryReaderQuotas ());
1191                         return ser.ReadObject (xr);
1192                 }
1193
1194                 public T Deserialize<T>(string json)
1195                 {
1196                         var bytes = Encoding.Unicode.GetBytes (json);
1197                         using (MemoryStream stream = new MemoryStream (bytes)) {
1198                                 var serializer = new DataContractJsonSerializer (typeof(T));
1199                                 return (T)serializer.ReadObject (stream);       
1200                         }
1201                 }
1202
1203                 [Test]
1204                 public void IsStartObject ()
1205                 {
1206                         DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCSimple1));
1207                         Assert.IsTrue (s.IsStartObject (XmlReader.Create (new StringReader ("<root></root>"))), "#1");
1208                         Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<dummy></dummy>"))), "#2");
1209                         Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<Foo></Foo>"))), "#3");
1210                         Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<root xmlns='urn:foo'></root>"))), "#4");
1211                 }
1212
1213                 [Test]
1214                 public void SerializeNonDC2 ()
1215                 {
1216                         var ser = new DataContractJsonSerializer (typeof (TestData));
1217                         StringWriter sw = new StringWriter ();
1218                         var obj = new TestData () { Foo = "foo", Bar = "bar", Baz = "baz" };
1219
1220                         // XML
1221                         using (var xw = XmlWriter.Create (sw))
1222                                 ser.WriteObject (xw, obj);
1223                         var s = sw.ToString ();
1224                         // since the order is not preserved, we compare only contents.
1225                         Assert.IsTrue (s.IndexOf ("<Foo>foo</Foo>") > 0, "#1-1");
1226                         Assert.IsTrue (s.IndexOf ("<Bar>bar</Bar>") > 0, "#1-2");
1227                         Assert.IsFalse (s.IndexOf ("<Baz>baz</Baz>") > 0, "#1-3");
1228
1229                         // JSON
1230                         MemoryStream ms = new MemoryStream ();
1231                         using (var xw = JsonReaderWriterFactory.CreateJsonWriter (ms))
1232                                 ser.WriteObject (ms, obj);
1233                         s = new StreamReader (new MemoryStream (ms.ToArray ())).ReadToEnd ().Replace ('"', '/');
1234                         // since the order is not preserved, we compare only contents.
1235                         Assert.IsTrue (s.IndexOf ("/Foo/:/foo/") > 0, "#2-1");
1236                         Assert.IsTrue (s.IndexOf ("/Bar/:/bar/") > 0, "#2-2");
1237                         Assert.IsFalse (s.IndexOf ("/Baz/:/baz/") > 0, "#2-3");
1238                 }
1239
1240                 [Test]
1241                 public void AlwaysEmitTypeInformation ()
1242                 {
1243                         var ms = new MemoryStream ();
1244                         var ds = new DataContractJsonSerializer (typeof (string), "root", null, 10, false, null, true);
1245                         ds.WriteObject (ms, "foobar");
1246                         var s = Encoding.UTF8.GetString (ms.ToArray ());
1247                         Assert.AreEqual ("\"foobar\"", s, "#1");
1248                 }
1249
1250                 [Test]
1251                 public void AlwaysEmitTypeInformation2 ()
1252                 {
1253                         var ms = new MemoryStream ();
1254                         var ds = new DataContractJsonSerializer (typeof (TestData), "root", null, 10, false, null, true);
1255                         ds.WriteObject (ms, new TestData () { Foo = "foo"});
1256                         var s = Encoding.UTF8.GetString (ms.ToArray ());
1257                         Assert.AreEqual (@"{""__type"":""TestData:#MonoTests.System.Runtime.Serialization.Json"",""Bar"":null,""Foo"":""foo""}", s, "#1");
1258                 }
1259
1260                 [Test]
1261                 public void AlwaysEmitTypeInformation3 ()
1262                 {
1263                         var ms = new MemoryStream ();
1264                         var ds = new DataContractJsonSerializer (typeof (TestData), "root", null, 10, false, null, false);
1265                         ds.WriteObject (ms, new TestData () { Foo = "foo"});
1266                         var s = Encoding.UTF8.GetString (ms.ToArray ());
1267                         Assert.AreEqual (@"{""Bar"":null,""Foo"":""foo""}", s, "#1");
1268                 }
1269
1270                 [Test]
1271                 public void TestNonpublicDeserialization ()
1272                 {
1273                         string s1= @"{""Bar"":""bar"", ""Foo"":""foo"", ""Baz"":""baz""}";
1274                         TestData o1 = ((TestData)(new DataContractJsonSerializer (typeof (TestData)).ReadObject (JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (s1), new XmlDictionaryReaderQuotas ()))));
1275
1276                         Assert.AreEqual (null, o1.Baz, "#1");
1277
1278                         string s2 = @"{""TestData"":[{""key"":""key1"",""value"":""value1""}]}";
1279                         KeyValueTestData o2 = ((KeyValueTestData)(new DataContractJsonSerializer (typeof (KeyValueTestData)).ReadObject (JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (s2), new XmlDictionaryReaderQuotas ()))));
1280
1281                         Assert.AreEqual (1, o2.TestData.Count, "#2");
1282                         Assert.AreEqual ("key1", o2.TestData[0].Key, "#3");
1283                         Assert.AreEqual ("value1", o2.TestData[0].Value, "#4");
1284                 }
1285
1286                 // [Test] use this case if you want to check lame silverlight parser behavior. Seealso #549756
1287                 public void QuotelessDeserialization ()
1288                 {
1289                         string s1 = @"{FooMember:""value""}";
1290                         var ds = new DataContractJsonSerializer (typeof (DCWithName));
1291                         ds.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (s1)));
1292
1293                         string s2 = @"{FooMember:"" \""{dummy:string}\""""}";
1294                         ds.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (s2)));
1295                 }
1296
1297                 [Test]
1298                 [Category ("NotWorking")]
1299                 public void TypeIsNotPartsOfKnownTypes ()
1300                 {
1301                         var dcs = new DataContractSerializer (typeof (string));
1302                         Assert.AreEqual (0, dcs.KnownTypes.Count, "KnownTypes #1");
1303                         var dcjs = new DataContractJsonSerializer (typeof (string));
1304                         Assert.AreEqual (0, dcjs.KnownTypes.Count, "KnownTypes #2");
1305                 }
1306
1307                 [Test]
1308                 public void ReadWriteNullObject ()
1309                 {
1310                         DataContractJsonSerializer dcjs = new DataContractJsonSerializer (typeof (string));
1311                         using (MemoryStream ms = new MemoryStream ()) {
1312                                 dcjs.WriteObject (ms, null);
1313                                 ms.Position = 0;
1314                                 using (StreamReader sr = new StreamReader (ms)) {
1315                                         string data = sr.ReadToEnd ();
1316                                         Assert.AreEqual ("null", data, "WriteObject(stream,null)");
1317
1318                                         ms.Position = 0;
1319                                         Assert.IsNull (dcjs.ReadObject (ms), "ReadObject(stream)");
1320                                 }
1321                         };
1322                 }
1323
1324                 object ReadWriteObject (Type type, object obj, string expected)
1325                 {
1326                         using (MemoryStream ms = new MemoryStream ()) {
1327                                 DataContractJsonSerializer dcjs = new DataContractJsonSerializer (type);
1328                                 dcjs.WriteObject (ms, obj);
1329                                 ms.Position = 0;
1330                                 using (StreamReader sr = new StreamReader (ms)) {
1331                                         Assert.AreEqual (expected, sr.ReadToEnd (), "WriteObject");
1332
1333                                         ms.Position = 0;
1334                                         return dcjs.ReadObject (ms);
1335                                 }
1336                         }
1337                 }
1338
1339                 [Test]
1340                 [Ignore ("Wrong test case. See bug #573691")]
1341                 public void ReadWriteObject_Single_SpecialCases ()
1342                 {
1343                         Assert.IsTrue (Single.IsNaN ((float) ReadWriteObject (typeof (float), Single.NaN, "NaN")));
1344                         Assert.IsTrue (Single.IsNegativeInfinity ((float) ReadWriteObject (typeof (float), Single.NegativeInfinity, "-INF")));
1345                         Assert.IsTrue (Single.IsPositiveInfinity ((float) ReadWriteObject (typeof (float), Single.PositiveInfinity, "INF")));
1346                 }
1347
1348                 [Test]
1349                 [Ignore ("Wrong test case. See bug #573691")]
1350                 public void ReadWriteObject_Double_SpecialCases ()
1351                 {
1352                         Assert.IsTrue (Double.IsNaN ((double) ReadWriteObject (typeof (double), Double.NaN, "NaN")));
1353                         Assert.IsTrue (Double.IsNegativeInfinity ((double) ReadWriteObject (typeof (double), Double.NegativeInfinity, "-INF")));
1354                         Assert.IsTrue (Double.IsPositiveInfinity ((double) ReadWriteObject (typeof (double), Double.PositiveInfinity, "INF")));
1355                 }
1356
1357                 [Test]
1358                 public void ReadWriteDateTime ()
1359                 {
1360                         var ms = new MemoryStream ();
1361                         DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (Query));
1362                         Query query = new Query () {
1363                                 StartDate = DateTime.SpecifyKind (new DateTime (2010, 3, 4, 5, 6, 7), DateTimeKind.Utc),
1364                                 EndDate = DateTime.SpecifyKind (new DateTime (2010, 4, 5, 6, 7, 8), DateTimeKind.Utc)
1365                                 };
1366                         serializer.WriteObject (ms, query);
1367                         Assert.AreEqual ("{\"StartDate\":\"\\/Date(1267679167000)\\/\",\"EndDate\":\"\\/Date(1270447628000)\\/\"}", Encoding.UTF8.GetString (ms.ToArray ()), "#1");
1368                         ms.Position = 0;
1369                         Console.WriteLine (new StreamReader (ms).ReadToEnd ());
1370                         ms.Position = 0;
1371                         var q = (Query) serializer.ReadObject(ms);
1372                         Assert.AreEqual (query.StartDate, q.StartDate, "#2");
1373                         Assert.AreEqual (query.EndDate, q.EndDate, "#3");
1374                 }
1375
1376                 [DataContract(Name = "DateTest")]
1377                 public class DateTest
1378                 {
1379                         [DataMember(Name = "should_have_value")]
1380                         public DateTime? ShouldHaveValue { get; set; }
1381                 }
1382
1383                 //
1384                 // This tests both the extended format "number-0500" as well
1385                 // as the nullable field in the structure
1386                 [Test]
1387                 public void BugXamarin163 ()
1388                 {
1389                         string json = @"{""should_have_value"":""\/Date(1277355600000)\/""}";
1390
1391                         byte[] bytes = global::System.Text.Encoding.UTF8.GetBytes(json);
1392                         Stream inputStream = new MemoryStream(bytes);
1393                         
1394                         DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DateTest));
1395                         DateTest t = serializer.ReadObject(inputStream) as DateTest;
1396                         Assert.AreEqual (634129524000000000, t.ShouldHaveValue.Value.Ticks, "#1");
1397                 }
1398
1399                 [Test]
1400                 public void NullableFieldsShouldSupportNullValue ()
1401                 {
1402                         string json = @"{""should_have_value"":null}";
1403                         var inputStream = new MemoryStream (Encoding.UTF8.GetBytes (json));
1404                         DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DateTest));
1405                         Console.WriteLine ("# serializer assembly: {0}", serializer.GetType ().Assembly.Location);
1406                         DateTest t = serializer.ReadObject (inputStream) as DateTest;
1407                         Assert.AreEqual (false, t.ShouldHaveValue.HasValue, "#2");
1408                 }
1409                 
1410                 [Test]
1411                 public void DeserializeNullMember ()
1412                 {
1413                         var ds = new DataContractJsonSerializer (typeof (ClassA));
1414                         var stream = new MemoryStream ();
1415                         var a = new ClassA ();
1416                         ds.WriteObject (stream, a);
1417                         stream.Position = 0;
1418                         a = (ClassA) ds.ReadObject (stream);
1419                         Assert.IsNull (a.B, "#1");
1420                 }
1421
1422                 [Test]
1423                 public void OnDeserializationMethods ()
1424                 {
1425                         var ds = new DataContractJsonSerializer (typeof (GSPlayerListErg));
1426                         var obj = new GSPlayerListErg ();
1427                         var ms = new MemoryStream ();
1428                         ds.WriteObject (ms, obj);
1429                         ms.Position = 0;
1430                         ds.ReadObject (ms);
1431                         Assert.IsTrue (GSPlayerListErg.A, "A");
1432                         Assert.IsTrue (GSPlayerListErg.B, "B");
1433                         Assert.IsTrue (GSPlayerListErg.C, "C");
1434                 }
1435                 
1436                 [Test]
1437                 public void WriteChar ()
1438                 {
1439                         DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof (CharTest));
1440                         using (MemoryStream ms = new MemoryStream()) {
1441                                 serializer.WriteObject(ms, new CharTest ());
1442                                 ms.Position = 0L;
1443                                 using (StreamReader reader = new StreamReader(ms)) {
1444                                         reader.ReadToEnd();
1445                                 }
1446                         }
1447                 }
1448
1449                 [Test]
1450                 public void DictionarySerialization ()
1451                 {
1452                         var dict = new MyDictionary<string,string> ();
1453                         dict.Add ("key", "value");
1454                         var serializer = new DataContractJsonSerializer (dict.GetType ());
1455                         var stream = new MemoryStream ();
1456                         serializer.WriteObject (stream, dict);
1457                         stream.Position = 0;
1458
1459                         Assert.AreEqual ("[{\"Key\":\"key\",\"Value\":\"value\"}]", new StreamReader (stream).ReadToEnd (), "#1");
1460                         stream.Position = 0;
1461                         dict = (MyDictionary<string,string>) serializer.ReadObject (stream);
1462                         Assert.AreEqual (1, dict.Count, "#2");
1463                         Assert.AreEqual ("value", dict ["key"], "#3");
1464                 }
1465
1466                 [Test]
1467                 public void ExplicitCustomDictionarySerialization ()
1468                 {
1469                         var dict = new MyExplicitDictionary<string,string> ();
1470                         dict.Add ("key", "value");
1471                         var serializer = new DataContractJsonSerializer (dict.GetType ());
1472                         var stream = new MemoryStream ();
1473                         serializer.WriteObject (stream, dict);
1474                         stream.Position = 0;
1475
1476                         Assert.AreEqual ("[{\"Key\":\"key\",\"Value\":\"value\"}]", new StreamReader (stream).ReadToEnd (), "#1");
1477                         stream.Position = 0;
1478                         dict = (MyExplicitDictionary<string,string>) serializer.ReadObject (stream);
1479                         Assert.AreEqual (1, dict.Count, "#2");
1480                         Assert.AreEqual ("value", dict ["key"], "#3");
1481                 }
1482
1483                 [Test]
1484                 public void Bug13485 ()
1485                 {
1486                         const string json = "{ \"Name\" : \"Test\", \"Value\" : \"ValueA\" }";
1487
1488                         string result = string.Empty;
1489                         var serializer = new DataContractJsonSerializer (typeof (Bug13485Type));
1490                         Bug13485Type entity;
1491                         using (var stream = new MemoryStream (Encoding.UTF8.GetBytes (json)))
1492                                 entity = (Bug13485Type) serializer.ReadObject (stream);
1493
1494                         result = entity.GetValue;
1495                         Assert.AreEqual ("ValueA", result, "#1");
1496                 }
1497
1498                 [DataContract(Name = "UriTest")]
1499                 public class UriTest
1500                 {
1501                         [DataMember(Name = "members")]
1502                         public Uri MembersRelativeLink { get; set; }
1503                 }
1504
1505                 [Test]
1506                 public void Bug15169 ()
1507                 {
1508                         const string json = "{\"members\":\"foo/bar/members\"}";
1509                         var serializer = new DataContractJsonSerializer (typeof (UriTest));
1510                         UriTest entity;
1511                         using (var stream = new MemoryStream (Encoding.UTF8.GetBytes (json)))
1512                                 entity = (UriTest) serializer.ReadObject (stream);
1513
1514                         Assert.AreEqual ("foo/bar/members", entity.MembersRelativeLink.ToString ());
1515                 }
1516                 
1517                 #region Test methods for collection serialization
1518                 
1519                 [Test]
1520                 public void TestArrayListSerialization ()
1521                 {
1522                         var collection = new ArrayListContainer ();
1523                         var expectedOutput = "{\"Items\":[\"banana\",\"apple\"]}";
1524                         var expectedItemsCount = 4;
1525                         
1526                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1527                         var stream = new MemoryStream ();
1528                         serializer.WriteObject (stream, collection);
1529
1530                         stream.Position = 0;
1531                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1532
1533                         stream.Position = 0;
1534                         collection = (ArrayListContainer) serializer.ReadObject (stream);
1535                         
1536                         Assert.AreEqual (expectedItemsCount, collection.Items.Count, "#2");
1537                 }
1538                 
1539                 [Test]
1540                 [ExpectedException (typeof (InvalidDataContractException))]
1541                 public void TestBitArraySerialization ()
1542                 {
1543                         var collection = new BitArrayContainer ();
1544                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1545                         var stream = new MemoryStream ();
1546                         serializer.WriteObject (stream, collection);
1547                 }
1548                 
1549                 [Test]
1550                 public void TestHashtableSerialization ()
1551                 {
1552                         var collection = new HashtableContainer ();
1553                         var expectedOutput = "{\"Items\":[{\"Key\":\"key1\",\"Value\":\"banana\"},{\"Key\":\"key2\",\"Value\":\"apple\"}]}";
1554                         
1555                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1556                         var stream = new MemoryStream ();
1557                         serializer.WriteObject (stream, collection);
1558
1559                         stream.Position = 0;
1560                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1561                 }
1562                 
1563                 [Test]
1564                 [ExpectedException (typeof (ArgumentException))]
1565                 public void TestHashtableDeserialization ()
1566                 {
1567                         var collection = new HashtableContainer ();
1568                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1569                         var stream = new MemoryStream ();
1570                         serializer.WriteObject (stream, collection);
1571                         
1572                         stream.Position = 0;
1573                         serializer.ReadObject (stream);
1574                 }
1575                 
1576                 [Test]
1577                 [ExpectedException (typeof (InvalidDataContractException))]
1578                 public void TestQueueSerialization ()
1579                 {
1580                         var collection = new QueueContainer ();
1581                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1582                         var stream = new MemoryStream ();
1583                         serializer.WriteObject (stream, collection);
1584                 }
1585                 
1586                 [Test]
1587                 public void TestSortedListSerialization ()
1588                 {
1589                         var collection = new SortedListContainer ();
1590                         var expectedOutput = "{\"Items\":[{\"Key\":\"key1\",\"Value\":\"banana\"},{\"Key\":\"key2\",\"Value\":\"apple\"}]}";
1591                         
1592                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1593                         var stream = new MemoryStream ();
1594                         serializer.WriteObject (stream, collection);
1595
1596                         stream.Position = 0;
1597                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1598                 }
1599                 
1600                 [Test]
1601                 [ExpectedException (typeof (ArgumentException))]
1602                 public void TestSortedListDeserialization ()
1603                 {
1604                         var collection = new SortedListContainer ();
1605                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1606                         var stream = new MemoryStream ();
1607                         serializer.WriteObject (stream, collection);
1608                         
1609                         stream.Position = 0;
1610                         serializer.ReadObject (stream);
1611                 }
1612                 
1613                 [Test]
1614                 [ExpectedException (typeof (InvalidDataContractException))]
1615                 public void TestStackSerialization ()
1616                 {
1617                         var collection = new StackContainer ();
1618                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1619                         var stream = new MemoryStream ();
1620                         serializer.WriteObject (stream, collection);
1621                 }
1622                 
1623                 [Test]
1624                 public void TestEnumerableWithAddSerialization ()
1625                 {
1626                         var collection = new EnumerableWithAddContainer ();
1627                         var expectedOutput = "{\"Items\":[\"banana\",\"apple\"]}";
1628                         var expectedItemsCount = 4;
1629                         
1630                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1631                         var stream = new MemoryStream ();
1632                         serializer.WriteObject (stream, collection);
1633
1634                         stream.Position = 0;
1635                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1636
1637                         stream.Position = 0;
1638                         collection = (EnumerableWithAddContainer) serializer.ReadObject (stream);
1639                         
1640                         Assert.AreEqual (expectedItemsCount, collection.Items.Count, "#2");
1641                 }
1642                 
1643                 [Test]
1644                 [ExpectedException (typeof (InvalidDataContractException))]
1645                 public void TestEnumerableWithSpecialAddSerialization ()
1646                 {
1647                         var collection = new EnumerableWithSpecialAddContainer ();                      
1648                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1649                         var stream = new MemoryStream ();
1650                         serializer.WriteObject (stream, collection);
1651                 }
1652         
1653                 [Test]
1654                 public void TestHashSetSerialization ()
1655                 {
1656                         var collection = new GenericHashSetContainer ();
1657                         var expectedOutput = "{\"Items\":[\"banana\",\"apple\"]}";
1658                         var expectedItemsCount = 2;
1659                         
1660                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1661                         var stream = new MemoryStream ();
1662                         serializer.WriteObject (stream, collection);
1663
1664                         stream.Position = 0;
1665                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1666
1667                         stream.Position = 0;
1668                         collection = (GenericHashSetContainer) serializer.ReadObject (stream);
1669                         
1670                         Assert.AreEqual (expectedItemsCount, collection.Items.Count, "#2");
1671                 }
1672                 
1673                 [Test]
1674                 public void TestLinkedListSerialization ()
1675                 {
1676                         var collection = new GenericLinkedListContainer ();
1677                         var expectedOutput = "{\"Items\":[\"banana\",\"apple\"]}";
1678                         var expectedItemsCount = 4;
1679                         
1680                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1681                         var stream = new MemoryStream ();
1682                         serializer.WriteObject (stream, collection);
1683
1684                         stream.Position = 0;
1685                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1686
1687                         stream.Position = 0;
1688                         collection = (GenericLinkedListContainer) serializer.ReadObject (stream);
1689                         
1690                         Assert.AreEqual (expectedItemsCount, collection.Items.Count, "#2");
1691                 }
1692                 
1693                 [Test]
1694                 [ExpectedException (typeof (InvalidDataContractException))]
1695                 public void TestGenericQueueSerialization ()
1696                 {
1697                         var collection = new GenericQueueContainer ();                  
1698                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1699                         var stream = new MemoryStream ();
1700                         serializer.WriteObject (stream, collection);
1701                 }
1702                 
1703                 [Test]
1704                 [ExpectedException (typeof (InvalidDataContractException))]
1705                 public void TestGenericStackSerialization ()
1706                 {
1707                         var collection = new GenericStackContainer ();                  
1708                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1709                         var stream = new MemoryStream ();
1710                         serializer.WriteObject (stream, collection);
1711                 }
1712                 
1713                 [Test]
1714                 public void TestGenericDictionarySerialization ()
1715                 {
1716                         var collection = new GenericDictionaryContainer ();                     
1717                         var expectedOutput = "{\"Items\":[{\"Key\":\"key1\",\"Value\":\"banana\"},{\"Key\":\"key2\",\"Value\":\"apple\"}]}";
1718                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1719                         var stream = new MemoryStream ();
1720                         serializer.WriteObject (stream, collection);
1721                         
1722                         stream.Position = 0;
1723                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1724                 }
1725                 
1726                 [Test]
1727                 [ExpectedException (typeof (ArgumentException))]
1728                 public void TestGenericDictionaryDeserialization ()
1729                 {
1730                         var collection = new GenericDictionaryContainer ();                     
1731                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1732                         var stream = new MemoryStream ();
1733                         serializer.WriteObject (stream, collection);
1734                         
1735                         stream.Position = 0;
1736                         serializer.ReadObject (stream);
1737                 }
1738                 
1739                 [Test]
1740                 public void TestGenericSortedListSerialization ()
1741                 {
1742                         var collection = new GenericSortedListContainer ();                     
1743                         var expectedOutput = "{\"Items\":[{\"Key\":\"key1\",\"Value\":\"banana\"},{\"Key\":\"key2\",\"Value\":\"apple\"}]}";
1744                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1745                         var stream = new MemoryStream ();
1746                         serializer.WriteObject (stream, collection);
1747                         
1748                         stream.Position = 0;
1749                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1750                 }
1751                 
1752                 [Test]
1753                 [ExpectedException (typeof (ArgumentException))]
1754                 public void TestGenericSortedListDeserialization ()
1755                 {
1756                         var collection = new GenericSortedListContainer ();                     
1757                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1758                         var stream = new MemoryStream ();
1759                         serializer.WriteObject (stream, collection);
1760                         
1761                         stream.Position = 0;
1762                         serializer.ReadObject (stream);
1763                 }
1764                 
1765                 [Test]
1766                 public void TestGenericSortedDictionarySerialization ()
1767                 {
1768                         var collection = new GenericSortedDictionaryContainer ();                       
1769                         var expectedOutput = "{\"Items\":[{\"Key\":\"key1\",\"Value\":\"banana\"},{\"Key\":\"key2\",\"Value\":\"apple\"}]}";
1770                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1771                         var stream = new MemoryStream ();
1772                         serializer.WriteObject (stream, collection);
1773                         
1774                         stream.Position = 0;
1775                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1776                 }
1777                 
1778                 [Test]
1779                 [ExpectedException (typeof (ArgumentException))]
1780                 public void TestGenericSortedDictionaryDeserialization ()
1781                 {
1782                         var collection = new GenericSortedDictionaryContainer ();                       
1783                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1784                         var stream = new MemoryStream ();
1785                         serializer.WriteObject (stream, collection);
1786                         
1787                         stream.Position = 0;
1788                         serializer.ReadObject (stream);
1789                 }
1790                 
1791                 [Test]
1792                 public void TestGenericEnumerableWithAddSerialization ()
1793                 {
1794                         var collection = new GenericEnumerableWithAddContainer ();
1795                         var expectedOutput = "{\"Items\":[\"banana\",\"apple\"]}";
1796                         var expectedItemsCount = 4;
1797                         
1798                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1799                         var stream = new MemoryStream ();
1800                         serializer.WriteObject (stream, collection);
1801
1802                         stream.Position = 0;
1803                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1804
1805                         stream.Position = 0;
1806                         collection = (GenericEnumerableWithAddContainer) serializer.ReadObject (stream);
1807                         
1808                         Assert.AreEqual (expectedItemsCount, collection.Items.Count, "#2");
1809                 }
1810                 
1811                 [Test]
1812                 [ExpectedException (typeof (InvalidDataContractException))]
1813                 public void TestGenericEnumerableWithSpecialAddSerialization ()
1814                 {
1815                         var collection = new GenericEnumerableWithSpecialAddContainer ();                       
1816                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1817                         var stream = new MemoryStream ();
1818                         serializer.WriteObject (stream, collection);
1819                 }
1820                 
1821                 [Test]
1822                 [ExpectedException (typeof (InvalidDataContractException))]
1823                 public void TestNonCollectionGetOnlyProperty ()
1824                 {
1825                         var o = new NonCollectionGetOnlyContainer ();                   
1826                         var serializer = new DataContractJsonSerializer (o.GetType ());
1827                         var stream = new MemoryStream ();
1828                         serializer.WriteObject (stream, o);
1829                 }
1830                 
1831                 // properly deserialize object with a polymorphic property (known derived type)
1832                 [Test]
1833                 public void Bug23058()
1834                 {
1835                         string serializedObj = @"{""PolymorphicProperty"":{""__type"":""KnownDerivedType:#MonoTests.System.Runtime.Serialization.Json"",""BaseTypeProperty"":""Base"",""DerivedProperty"":""Derived 1""},""Name"":""Parent2""}";
1836                         ParentType deserializedObj = Deserialize<ParentType> (serializedObj);
1837
1838                         Assert.AreEqual (deserializedObj.PolymorphicProperty.GetType ().FullName, "MonoTests.System.Runtime.Serialization.Json.KnownDerivedType");
1839                         Assert.AreEqual (deserializedObj.PolymorphicProperty.BaseTypeProperty, "Base");
1840                         Assert.AreEqual ((deserializedObj.PolymorphicProperty as KnownDerivedType).DerivedProperty, "Derived 1");
1841                         Assert.AreEqual (deserializedObj.Name, "Parent2");
1842                 }
1843
1844                 // properly deserialize object with a polymorphic property (base type with __type hint)
1845                 [Test]
1846                 public void DeserializeBaseTypePropHint()
1847                 {
1848                         string serializedObj = @"{""PolymorphicProperty"":{""__type"":""BaseType:#MonoTests.System.Runtime.Serialization.Json"",""BaseTypeProperty"":""Base""},""Name"":""Parent2""}";
1849                         ParentType deserializedObj = Deserialize<ParentType> (serializedObj);
1850
1851                         Assert.AreEqual (deserializedObj.PolymorphicProperty.GetType ().FullName, "MonoTests.System.Runtime.Serialization.Json.BaseType");
1852                         Assert.AreEqual (deserializedObj.PolymorphicProperty.BaseTypeProperty, "Base");
1853                 }
1854
1855                 // properly deserialize object with a polymorphic property (base type with __type hint)
1856                 [Test]
1857                 public void DeserializeBaseTypePropNoHint()
1858                 {
1859                         string serializedObj = @"{""PolymorphicProperty"":{""BaseTypeProperty"":""Base""},""Name"":""Parent2""}";
1860                         ParentType deserializedObj = Deserialize<ParentType> (serializedObj);
1861
1862                         Assert.AreEqual (deserializedObj.PolymorphicProperty.GetType ().FullName, "MonoTests.System.Runtime.Serialization.Json.BaseType");
1863                         Assert.AreEqual (deserializedObj.PolymorphicProperty.BaseTypeProperty, "Base");
1864                 }
1865
1866                 // properly fail deserializing object with a polymorphic property (unknown derived type)
1867                 [ExpectedException (typeof (SerializationException))]
1868                 [Test]
1869                 public void FailDeserializingUnknownTypeProp()
1870                 {
1871                         string serializedObj = @"{""PolymorphicProperty"":{""__type"":""UnknownDerivedType:#MonoTests.System.Runtime.Serialization.Json"",""BaseTypeProperty"":""Base"",""DerivedProperty"":""Derived 1""},""Name"":""Parent2""}";
1872                         ParentType deserializedObj = Deserialize<ParentType> (serializedObj);
1873                 }
1874
1875                 [Test]
1876                 public void SubclassTest ()
1877                 {
1878                         var knownTypes = new List<Type> { typeof(IntList) };
1879                         var serializer = new DataContractJsonSerializer(typeof(ListOfNumbers), knownTypes);
1880
1881                         string json = "{\"Numbers\": [85]}";
1882                         using (var stream = new MemoryStream(UTF8Encoding.Default.GetBytes(json)))
1883                         {
1884                                 var nums = (ListOfNumbers)serializer.ReadObject(stream);
1885                                 Assert.AreEqual (1, nums.Numbers.Count);
1886                         }
1887                 }
1888                 [DataContract]
1889                 public class ListOfNumbers
1890                 {
1891                         [DataMember]
1892                         public IntList Numbers;
1893                 }
1894
1895                 public class IntList : List<int>{}
1896                 #endregion
1897
1898                 [Test]
1899                 public void DefaultValueDeserialization ()
1900                 {
1901                         // value type
1902                         var person = new Person { name = "John" };
1903                         using (var ms = new MemoryStream()) {
1904                                 var serializer = new DataContractJsonSerializer (typeof (Person), new DataContractJsonSerializerSettings {
1905                                         SerializeReadOnlyTypes = true,
1906                                         UseSimpleDictionaryFormat = true
1907                                         });
1908                                 serializer.WriteObject (ms, person);
1909                         }
1910
1911                         // reference type
1912                         var person2 = new PersonWithContact {
1913                                 name = "Jane",
1914                                 contact = new Contact { url = "localhost", email = "jane@localhost" } };
1915                         using (var ms = new MemoryStream ()) {
1916                                 var serializer = new DataContractJsonSerializer (typeof (PersonWithContact), new DataContractJsonSerializerSettings {
1917                                         SerializeReadOnlyTypes = true,
1918                                         UseSimpleDictionaryFormat = true
1919                                         });
1920                                 serializer.WriteObject (ms, person2);
1921                         }
1922                 }
1923
1924                 [Test]
1925                 public void Bug15028()
1926                 {
1927                         DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Bug15028));
1928                         using (MemoryStream memoryStream = new MemoryStream())
1929                         {
1930                                 ser.WriteObject(memoryStream, new Bug15028());
1931                                 string output = Encoding.Default.GetString(memoryStream.ToArray());
1932                                 Assert.AreEqual(@"{""Int0"":1,""Int1"":1,""IntZero1"":0,""Str0"":"""",""Str1"":"""",""StrNull1"":null}", output);
1933                         }
1934                 }
1935         }
1936         
1937
1938         [DataContract]
1939         public class Bug15028
1940         {
1941                 [DataMember(EmitDefaultValue = false)]
1942                 public string StrNull0 { get; private set; }
1943
1944                 [DataMember(EmitDefaultValue = false)]
1945                 public string Str0 { get; private set; }
1946
1947                 [DataMember(EmitDefaultValue = true)]
1948                 public string StrNull1 { get; private set; }
1949
1950                 [DataMember(EmitDefaultValue = true)]
1951                 public string Str1 { get; private set; }
1952
1953                 [DataMember(EmitDefaultValue = false)]
1954                 public int IntZero0 { get; private set; }
1955
1956                 [DataMember(EmitDefaultValue = false)]
1957                 public int Int0 { get; private set; }
1958
1959                 [DataMember(EmitDefaultValue = true)]
1960                 public int IntZero1 { get; private set; }
1961
1962                 [DataMember(EmitDefaultValue = true)]
1963                 public int Int1 { get; private set; }
1964
1965                 public Bug15028()
1966                 {
1967                         Str0 = string.Empty;
1968                         Str1 = string.Empty;
1969                         Int0 = 1;
1970                         Int1 = 1;
1971                 }
1972         }
1973
1974         public class CharTest
1975         {
1976                 public char Foo;
1977         }
1978
1979         public class TestData
1980         {
1981                 public string Foo { get; set; }
1982                 public string Bar { get; set; }
1983                 internal string Baz { get; set; }
1984         }
1985
1986         public enum Colors {
1987                 Red, Green, Blue
1988         }
1989
1990         [DataContract (Name = "_ColorsWithDC")]
1991         public enum ColorsWithDC {
1992
1993                 [EnumMember (Value = "_Red")]
1994                 Red, 
1995                 [EnumMember]
1996                 Green, 
1997                 Blue
1998         }
1999
2000
2001         public enum ColorsEnumMemberNoDC {
2002                 [EnumMember (Value = "_Red")]
2003                 Red, 
2004                 [EnumMember]
2005                 Green, 
2006                 Blue
2007         }
2008
2009         [DataContract]
2010         public class DCWithEnum {
2011                 [DataMember (Name = "_colors")]
2012                 public Colors colors;
2013         }
2014
2015         [DataContract]
2016         public class DCEmpty
2017         {
2018                 // serializer doesn't touch it.
2019                 public string Foo = "TEST";
2020         }
2021
2022         [DataContract]
2023         public class DCSimple1
2024         {
2025                 [DataMember]
2026                 public string Foo = "TEST";
2027         }
2028
2029         [DataContract]
2030         public class DCHasNonDC
2031         {
2032                 [DataMember]
2033                 public NonDC Hoge= new NonDC ();
2034         }
2035
2036         public class NonDC
2037         {
2038                 public string Whee = "whee!";
2039         }
2040
2041         [DataContract]
2042         public class DCHasSerializable
2043         {
2044                 [DataMember]
2045                 public SimpleSer1 Ser = new SimpleSer1 ();
2046         }
2047
2048         [DataContract (Name = "Foo")]
2049         public class DCWithName
2050         {
2051                 [DataMember (Name = "FooMember")]
2052                 public string DMWithName = "value";
2053         }
2054
2055         [DataContract (Name = "")]
2056         public class DCWithEmptyName
2057         {
2058                 [DataMember]
2059                 public string Foo;
2060         }
2061
2062         [DataContract (Name = null)]
2063         public class DCWithNullName
2064         {
2065                 [DataMember]
2066                 public string Foo;
2067         }
2068
2069         [DataContract (Namespace = "")]
2070         public class DCWithEmptyNamespace
2071         {
2072                 [DataMember]
2073                 public string Foo;
2074         }
2075
2076         [Serializable]
2077         public class SimpleSer1
2078         {
2079                 public string Doh = "doh!";
2080         }
2081
2082         public class Wrapper
2083         {
2084                 [DataContract]
2085                 public class DCWrapped
2086                 {
2087                 }
2088         }
2089
2090         [DataContract]
2091         public class CollectionContainer
2092         {
2093                 Collection<string> items = new Collection<string> ();
2094
2095                 [DataMember]
2096                 public Collection<string> Items {
2097                         get { return items; }
2098                 }
2099         }
2100
2101         [CollectionDataContract]
2102         public class DataCollection<T> : Collection<T>
2103         {
2104         }
2105
2106         [DataContract]
2107         public class DataCollectionContainer
2108         {
2109                 DataCollection<string> items = new DataCollection<string> ();
2110
2111                 [DataMember]
2112                 public DataCollection<string> Items {
2113                         get { return items; }
2114                 }
2115         }
2116
2117         [DataContract]
2118         class SerializeNonDCArrayType
2119         {
2120                 [DataMember]
2121                 public NonDCItem [] IPAddresses = new NonDCItem [0];
2122         }
2123
2124         public class NonDCItem
2125         {
2126                 public byte [] Data { get; set; }
2127         }
2128
2129         [DataContract]
2130         public class VerifyObjectNameTestData
2131         {
2132                 [DataMember]
2133                 string Member1 = "foo";
2134         }
2135
2136         [Serializable]
2137         public class KeyValueTestData {
2138                 public List<KeyValuePair<string,string>> TestData = new List<KeyValuePair<string,string>>();
2139         }
2140
2141         [DataContract] // bug #586169
2142         public class Query
2143         {
2144                 [DataMember (Order=1)]
2145                 public DateTime StartDate { get; set; }
2146                 [DataMember (Order=2)]
2147                 public DateTime EndDate { get; set; }
2148         }
2149
2150         public class ClassA {
2151                 public ClassB B { get; set; }
2152         }
2153
2154         public class ClassB
2155         {
2156         }
2157
2158         public class GSPlayerListErg
2159         {
2160                 public GSPlayerListErg ()
2161                 {
2162                         Init ();
2163                 }
2164
2165                 void Init ()
2166                 {
2167                         C = true;
2168                         ServerTimeUTC = DateTime.SpecifyKind (DateTime.MinValue, DateTimeKind.Utc);
2169                 }
2170
2171                 [OnDeserializing]
2172                 public void OnDeserializing (StreamingContext c)
2173                 {
2174                         A = true;
2175                         Init ();
2176                 }
2177
2178                 [OnDeserialized]
2179                 void OnDeserialized (StreamingContext c)
2180                 {
2181                         B = true;
2182                 }
2183
2184                 public static bool A, B, C;
2185
2186                 [DataMember (Name = "T")]
2187                 public long CodedServerTimeUTC { get; set; }
2188                 public DateTime ServerTimeUTC { get; set; }
2189         }
2190
2191         #region polymorphism test helper classes
2192
2193         [DataContract]
2194         [KnownType (typeof (KnownDerivedType))]
2195         public class ParentType
2196         {
2197                 [DataMember]
2198                 public string Name { get; set; }
2199
2200                 [DataMember]
2201                 public BaseType PolymorphicProperty { get; set; }
2202         }
2203
2204         [DataContract]
2205         public class BaseType
2206         {
2207                 [DataMember]
2208                 public string BaseTypeProperty { get; set; }
2209         }
2210
2211         [DataContract]
2212         public class KnownDerivedType : BaseType
2213         {
2214                 [DataMemberAttribute]
2215                 public string DerivedProperty { get; set; }
2216         }
2217
2218         [DataContract]
2219         public class UnknownDerivedType : BaseType
2220         {
2221                 [DataMember]
2222                 public string DerivedProperty { get; set; }
2223         }
2224
2225         #endregion
2226 }
2227
2228 [DataContract]
2229 class GlobalSample1
2230 {
2231 }
2232
2233
2234 public class MyDictionary<K, V> : System.Collections.Generic.IDictionary<K, V>
2235 {
2236         Dictionary<K,V> dic = new Dictionary<K,V> ();
2237
2238         public void Add (K key, V value)
2239         {
2240                 dic.Add (key,  value);
2241         }
2242
2243         public bool ContainsKey (K key)
2244         {
2245                 return dic.ContainsKey (key);
2246         }
2247
2248         public ICollection<K> Keys {
2249                 get { return dic.Keys; }
2250         }
2251
2252         public bool Remove (K key)
2253         {
2254                 return dic.Remove (key);
2255         }
2256
2257         public bool TryGetValue (K key, out V value)
2258         {
2259                 return dic.TryGetValue (key, out value);
2260         }
2261
2262         public ICollection<V> Values {
2263                 get { return dic.Values; }
2264         }
2265
2266         public V this [K key] {
2267                 get { return dic [key]; }
2268                 set { dic [key] = value; }
2269         }
2270
2271         IEnumerator IEnumerable.GetEnumerator ()
2272         {
2273                 return dic.GetEnumerator ();
2274         }
2275
2276         ICollection<KeyValuePair<K,V>> Coll {
2277                 get { return (ICollection<KeyValuePair<K,V>>) dic; }
2278         }
2279
2280         public void Add (KeyValuePair<K, V> item)
2281         {
2282                 Coll.Add (item);
2283         }
2284
2285         public void Clear ()
2286         {
2287                 dic.Clear ();
2288         }
2289
2290         public bool Contains (KeyValuePair<K, V> item)
2291         {
2292                 return Coll.Contains (item);
2293         }
2294
2295         public void CopyTo (KeyValuePair<K, V> [] array, int arrayIndex)
2296         {
2297                 Coll.CopyTo (array, arrayIndex);
2298         }
2299
2300         public int Count {
2301                 get { return dic.Count; }
2302         }
2303
2304         public bool IsReadOnly {
2305                 get { return Coll.IsReadOnly; }
2306         }
2307
2308         public bool Remove (KeyValuePair<K, V> item)
2309         {
2310                 return Coll.Remove (item);
2311         }
2312
2313         public IEnumerator<KeyValuePair<K, V>> GetEnumerator ()
2314         {
2315                 return Coll.GetEnumerator ();
2316         }
2317 }
2318
2319 public class MyExplicitDictionary<K, V> : IDictionary<K, V> {
2320
2321         Dictionary<K,V> dic = new Dictionary<K,V> ();
2322
2323         public void Add (K key, V value)
2324         {
2325                 dic.Add (key,  value);
2326         }
2327
2328         public bool ContainsKey (K key)
2329         {
2330                 return dic.ContainsKey (key);
2331         }
2332
2333         ICollection<K> IDictionary<K, V>.Keys {
2334                 get { return dic.Keys; }
2335         }
2336
2337         public bool Remove (K key)
2338         {
2339                 return dic.Remove (key);
2340         }
2341
2342         public bool TryGetValue (K key, out V value)
2343         {
2344                 return dic.TryGetValue (key, out value);
2345         }
2346
2347         ICollection<V> IDictionary<K, V>.Values {
2348                 get { return dic.Values; }
2349         }
2350
2351         public V this [K key] {
2352                 get { return dic [key]; }
2353                 set { dic [key] = value; }
2354         }
2355
2356         IEnumerator IEnumerable.GetEnumerator ()
2357         {
2358                 return dic.GetEnumerator ();
2359         }
2360
2361         ICollection<KeyValuePair<K,V>> Coll {
2362                 get { return (ICollection<KeyValuePair<K,V>>) dic; }
2363         }
2364
2365         public void Add (KeyValuePair<K, V> item)
2366         {
2367                 Coll.Add (item);
2368         }
2369
2370         public void Clear ()
2371         {
2372                 dic.Clear ();
2373         }
2374
2375         public bool Contains (KeyValuePair<K, V> item)
2376         {
2377                 return Coll.Contains (item);
2378         }
2379
2380         public void CopyTo (KeyValuePair<K, V> [] array, int arrayIndex)
2381         {
2382                 Coll.CopyTo (array, arrayIndex);
2383         }
2384
2385         public int Count {
2386                 get { return dic.Count; }
2387         }
2388
2389         public bool IsReadOnly {
2390                 get { return Coll.IsReadOnly; }
2391         }
2392
2393         public bool Remove (KeyValuePair<K, V> item)
2394         {
2395                 return Coll.Remove (item);
2396         }
2397
2398         public IEnumerator<KeyValuePair<K, V>> GetEnumerator ()
2399         {
2400                 return Coll.GetEnumerator ();
2401         }
2402 }
2403
2404 [DataContract]
2405 public class Bug13485Type
2406 {
2407         [DataMember]
2408         public string Name { get; set; }
2409
2410         [DataMember (Name = "Value")]
2411         private string Value { get; set; }
2412
2413         public string GetValue { get { return this.Value; } }
2414 }
2415
2416 #region Test classes for Collection serialization
2417
2418 [DataContract]
2419         public abstract class CollectionContainer <V>
2420         {
2421                 V items;
2422
2423                 [DataMember]
2424                 public V Items
2425                 {
2426                         get {
2427                                 if (items == null) items = Init ();
2428                                 return items;
2429                         }
2430                 }
2431                 
2432                 public CollectionContainer ()
2433                 {
2434                         Init ();
2435                 }
2436         
2437                 protected abstract V Init ();
2438         }
2439         
2440         [DataContract]
2441         public class ArrayListContainer : CollectionContainer<ArrayList> {
2442                 protected override ArrayList Init ()
2443                 {
2444                         return new ArrayList { "banana", "apple" };
2445                 }
2446         }
2447         
2448         [DataContract]
2449         public class BitArrayContainer : CollectionContainer<BitArray> {
2450                 protected override BitArray Init ()
2451                 {
2452                         return new BitArray (new [] { false, true });
2453                 }
2454         }
2455         
2456         [DataContract]
2457         public class HashtableContainer : CollectionContainer<Hashtable> {
2458                 protected override Hashtable Init ()
2459                 {
2460                         var ht = new Hashtable ();
2461                         ht.Add ("key1", "banana");
2462                         ht.Add ("key2", "apple");
2463                         return ht;
2464                 }
2465         }
2466         
2467         [DataContract]
2468         public class QueueContainer : CollectionContainer<Queue> {
2469                 protected override Queue Init ()
2470                 {
2471                         var q = new Queue ();
2472                         q.Enqueue ("banana");
2473                         q.Enqueue ("apple");
2474                         return q;
2475                 }
2476         }
2477         
2478         [DataContract]
2479         public class SortedListContainer : CollectionContainer<SortedList> {
2480                 protected override SortedList Init ()
2481                 {
2482                         var l = new SortedList ();
2483                         l.Add ("key1", "banana");
2484                         l.Add ("key2", "apple");
2485                         return l;
2486                 }
2487         }
2488         
2489         [DataContract]
2490         public class StackContainer : CollectionContainer<Stack> {
2491                 protected override Stack Init ()
2492                 {
2493                         var s = new Stack ();
2494                         s.Push ("banana");
2495                         s.Push ("apple");
2496                         return s;
2497                 }
2498         }
2499
2500         public class EnumerableWithAdd : IEnumerable
2501         {
2502                 private ArrayList items;
2503
2504                 public EnumerableWithAdd()
2505                 {
2506                         items = new ArrayList();
2507                 }
2508
2509                 public IEnumerator GetEnumerator()
2510                 {
2511                         return items.GetEnumerator();
2512                 }
2513
2514                 public void Add(object value)
2515                 {
2516                         items.Add(value);
2517                 }
2518
2519                 public int Count
2520                 {
2521                         get {
2522                                 return items.Count;
2523                         }
2524                 }
2525         }
2526
2527         public class EnumerableWithSpecialAdd : IEnumerable
2528         {
2529                 private ArrayList items;
2530
2531                 public EnumerableWithSpecialAdd()
2532                 {
2533                         items = new ArrayList();
2534                 }
2535
2536                 public IEnumerator GetEnumerator()
2537                 {
2538                         return items.GetEnumerator();
2539                 }
2540
2541                 public void Add(object value, int index)
2542                 {
2543                         items.Add(value);
2544                 }
2545
2546                 public int Count
2547                 {
2548                         get
2549                         {
2550                                 return items.Count;
2551                         }
2552                 }
2553         }
2554
2555         [DataContract]
2556         public class EnumerableWithAddContainer : CollectionContainer<EnumerableWithAdd>
2557         {
2558                 protected override EnumerableWithAdd Init()
2559                 {
2560                         var s = new EnumerableWithAdd();
2561                         s.Add ("banana");
2562                         s.Add ("apple");
2563                         return s;
2564                 }
2565         }
2566
2567         [DataContract]
2568         public class EnumerableWithSpecialAddContainer : CollectionContainer<EnumerableWithSpecialAdd>
2569         {
2570                 protected override EnumerableWithSpecialAdd Init()
2571                 {
2572                         var s = new EnumerableWithSpecialAdd();
2573                         s.Add("banana", 0);
2574                         s.Add("apple", 0);
2575                         return s;
2576                 }
2577         }
2578
2579         [DataContract]
2580         public class GenericDictionaryContainer : CollectionContainer<Dictionary<string, string>> {
2581                 protected override Dictionary<string, string> Init ()
2582                 {
2583                         var d = new Dictionary<string, string> ();
2584                         d.Add ("key1", "banana");
2585                         d.Add ("key2", "apple");
2586                         return d;
2587                 }
2588         }
2589
2590         [DataContract]
2591         public class GenericHashSetContainer : CollectionContainer<HashSet<string>> {
2592                 protected override HashSet<string> Init ()
2593                 {
2594                         return new HashSet<string> { "banana", "apple" };
2595                 }
2596         }
2597
2598         [DataContract]
2599         public class GenericLinkedListContainer : CollectionContainer<LinkedList<string>> {
2600                 protected override LinkedList<string> Init ()
2601                 {
2602                         var l = new LinkedList<string> ();
2603                         l.AddFirst ("apple");
2604                         l.AddFirst ("banana");
2605                         return l;
2606                 }
2607         }
2608
2609         [DataContract]
2610         public class GenericListContainer : CollectionContainer<List<string>> {
2611                 protected override List<string> Init ()
2612                 {
2613                         return new List<string> { "banana", "apple" };
2614                 }
2615         }
2616
2617         [DataContract]
2618         public class GenericQueueContainer : CollectionContainer<Queue<string>> {
2619                 protected override Queue<string> Init ()
2620                 {
2621                         var q = new Queue<string> ();
2622                         q.Enqueue ("banana");
2623                         q.Enqueue ("apple" );
2624                         return q;
2625                 }
2626         }
2627
2628         [DataContract]
2629         public class GenericSortedDictionaryContainer : CollectionContainer<SortedDictionary<string, string>> {
2630                 protected override SortedDictionary<string, string> Init ()
2631                 {
2632                         var d = new SortedDictionary<string, string> ();
2633                         d.Add ("key1", "banana");
2634                         d.Add ("key2", "apple");
2635                         return d;
2636                 }
2637         }
2638
2639         [DataContract]
2640         public class GenericSortedListContainer : CollectionContainer<SortedList<string, string>> {
2641                 protected override SortedList<string, string> Init ()
2642                 {
2643                         var d = new SortedList<string, string> ();
2644                         d.Add ("key1", "banana");
2645                         d.Add ("key2", "apple");
2646                         return d;
2647                 }
2648         }
2649
2650         [DataContract]
2651         public class GenericStackContainer : CollectionContainer<Stack<string>> {
2652                 protected override Stack<string> Init ()
2653                 {
2654                         var s = new Stack<string> ();
2655                         s.Push ("banana");
2656                         s.Push ("apple" );
2657                         return s;
2658                 }
2659         }
2660
2661         public class GenericEnumerableWithAdd : IEnumerable<string>
2662         {
2663                 private List<string> items;
2664
2665                 public GenericEnumerableWithAdd()
2666                 {
2667                         items = new List<string>();
2668                 }
2669
2670                 IEnumerator IEnumerable.GetEnumerator()
2671                 {
2672                         return items.GetEnumerator ();
2673                 }
2674
2675                 public IEnumerator<string> GetEnumerator()
2676                 {
2677                         return items.GetEnumerator ();
2678                 }
2679
2680                 public void Add(string value)
2681                 {
2682                         items.Add(value);
2683                 }
2684
2685                 public int Count
2686                 {
2687                         get {
2688                                 return items.Count;
2689                         }
2690                 }
2691         }
2692
2693         public class GenericEnumerableWithSpecialAdd : IEnumerable<string>
2694         {
2695                 private List<string> items;
2696
2697                 public GenericEnumerableWithSpecialAdd()
2698                 {
2699                         items = new List<string>();
2700                 }
2701
2702                 IEnumerator IEnumerable.GetEnumerator()
2703                 {
2704                         return items.GetEnumerator ();
2705                 }
2706
2707                 public IEnumerator<string> GetEnumerator()
2708                 {
2709                         return items.GetEnumerator ();
2710                 }
2711
2712                 public void Add(string value, int index)
2713                 {
2714                         items.Add(value);
2715                 }
2716
2717                 public int Count
2718                 {
2719                         get
2720                         {
2721                                 return items.Count;
2722                         }
2723                 }
2724         }
2725
2726         [DataContract]
2727         public class GenericEnumerableWithAddContainer : CollectionContainer<GenericEnumerableWithAdd>
2728         {
2729                 protected override GenericEnumerableWithAdd Init()
2730                 {
2731                         var s = new GenericEnumerableWithAdd();
2732                         s.Add ("banana");
2733                         s.Add ("apple");
2734                         return s;
2735                 }
2736         }
2737
2738         [DataContract]
2739         public class GenericEnumerableWithSpecialAddContainer : CollectionContainer<GenericEnumerableWithSpecialAdd>
2740         {
2741                 protected override GenericEnumerableWithSpecialAdd Init()
2742                 {
2743                         var s = new GenericEnumerableWithSpecialAdd();
2744                         s.Add("banana", 0);
2745                         s.Add("apple", 0);
2746                         return s;
2747                 }
2748         }       
2749
2750         [DataContract]
2751         public class NonCollectionGetOnlyContainer
2752         {
2753                 string _test = "my string";
2754         
2755                 [DataMember]
2756                 public string MyString {
2757                         get {
2758                                 return _test;
2759                         }
2760                 }
2761         }       
2762
2763 #endregion
2764
2765 #region DefaultValueDeserialization
2766     [DataContract]
2767     public class Person
2768     {
2769         [DataMember(EmitDefaultValue = false)]
2770         public string name { get; set; }
2771     }
2772
2773     [DataContract]
2774     public class PersonWithContact
2775     {
2776         [DataMember(EmitDefaultValue = false)]
2777         public string name { get; set; }
2778
2779         [DataMember(EmitDefaultValue = false)]
2780         public Contact contact { get; set; }
2781     }
2782
2783     [DataContract]
2784     public class Contact
2785     {
2786         [DataMember(EmitDefaultValue = false)]
2787         public string url { get; set; }
2788
2789         [DataMember(EmitDefaultValue = false)]
2790         public string email{ get; set; }
2791     }
2792 #endregion