Merge pull request #1068 from esdrubal/bug18421
[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                 [Category ("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                 [Category ("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                 [Test]
1195                 public void IsStartObject ()
1196                 {
1197                         DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCSimple1));
1198                         Assert.IsTrue (s.IsStartObject (XmlReader.Create (new StringReader ("<root></root>"))), "#1");
1199                         Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<dummy></dummy>"))), "#2");
1200                         Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<Foo></Foo>"))), "#3");
1201                         Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<root xmlns='urn:foo'></root>"))), "#4");
1202                 }
1203
1204                 [Test]
1205                 public void SerializeNonDC2 ()
1206                 {
1207                         var ser = new DataContractJsonSerializer (typeof (TestData));
1208                         StringWriter sw = new StringWriter ();
1209                         var obj = new TestData () { Foo = "foo", Bar = "bar", Baz = "baz" };
1210
1211                         // XML
1212                         using (var xw = XmlWriter.Create (sw))
1213                                 ser.WriteObject (xw, obj);
1214                         var s = sw.ToString ();
1215                         // since the order is not preserved, we compare only contents.
1216                         Assert.IsTrue (s.IndexOf ("<Foo>foo</Foo>") > 0, "#1-1");
1217                         Assert.IsTrue (s.IndexOf ("<Bar>bar</Bar>") > 0, "#1-2");
1218                         Assert.IsFalse (s.IndexOf ("<Baz>baz</Baz>") > 0, "#1-3");
1219
1220                         // JSON
1221                         MemoryStream ms = new MemoryStream ();
1222                         using (var xw = JsonReaderWriterFactory.CreateJsonWriter (ms))
1223                                 ser.WriteObject (ms, obj);
1224                         s = new StreamReader (new MemoryStream (ms.ToArray ())).ReadToEnd ().Replace ('"', '/');
1225                         // since the order is not preserved, we compare only contents.
1226                         Assert.IsTrue (s.IndexOf ("/Foo/:/foo/") > 0, "#2-1");
1227                         Assert.IsTrue (s.IndexOf ("/Bar/:/bar/") > 0, "#2-2");
1228                         Assert.IsFalse (s.IndexOf ("/Baz/:/baz/") > 0, "#2-3");
1229                 }
1230
1231                 [Test]
1232                 public void AlwaysEmitTypeInformation ()
1233                 {
1234                         var ms = new MemoryStream ();
1235                         var ds = new DataContractJsonSerializer (typeof (string), "root", null, 10, false, null, true);
1236                         ds.WriteObject (ms, "foobar");
1237                         var s = Encoding.UTF8.GetString (ms.ToArray ());
1238                         Assert.AreEqual ("\"foobar\"", s, "#1");
1239                 }
1240
1241                 [Test]
1242                 public void AlwaysEmitTypeInformation2 ()
1243                 {
1244                         var ms = new MemoryStream ();
1245                         var ds = new DataContractJsonSerializer (typeof (TestData), "root", null, 10, false, null, true);
1246                         ds.WriteObject (ms, new TestData () { Foo = "foo"});
1247                         var s = Encoding.UTF8.GetString (ms.ToArray ());
1248                         Assert.AreEqual (@"{""__type"":""TestData:#MonoTests.System.Runtime.Serialization.Json"",""Bar"":null,""Foo"":""foo""}", s, "#1");
1249                 }
1250
1251                 [Test]
1252                 public void AlwaysEmitTypeInformation3 ()
1253                 {
1254                         var ms = new MemoryStream ();
1255                         var ds = new DataContractJsonSerializer (typeof (TestData), "root", null, 10, false, null, false);
1256                         ds.WriteObject (ms, new TestData () { Foo = "foo"});
1257                         var s = Encoding.UTF8.GetString (ms.ToArray ());
1258                         Assert.AreEqual (@"{""Bar"":null,""Foo"":""foo""}", s, "#1");
1259                 }
1260
1261                 [Test]
1262                 public void TestNonpublicDeserialization ()
1263                 {
1264                         string s1= @"{""Bar"":""bar"", ""Foo"":""foo"", ""Baz"":""baz""}";
1265                         TestData o1 = ((TestData)(new DataContractJsonSerializer (typeof (TestData)).ReadObject (JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (s1), new XmlDictionaryReaderQuotas ()))));
1266
1267                         Assert.AreEqual (null, o1.Baz, "#1");
1268
1269                         string s2 = @"{""TestData"":[{""key"":""key1"",""value"":""value1""}]}";
1270                         KeyValueTestData o2 = ((KeyValueTestData)(new DataContractJsonSerializer (typeof (KeyValueTestData)).ReadObject (JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (s2), new XmlDictionaryReaderQuotas ()))));
1271
1272                         Assert.AreEqual (1, o2.TestData.Count, "#2");
1273                         Assert.AreEqual ("key1", o2.TestData[0].Key, "#3");
1274                         Assert.AreEqual ("value1", o2.TestData[0].Value, "#4");
1275                 }
1276
1277                 // [Test] use this case if you want to check lame silverlight parser behavior. Seealso #549756
1278                 public void QuotelessDeserialization ()
1279                 {
1280                         string s1 = @"{FooMember:""value""}";
1281                         var ds = new DataContractJsonSerializer (typeof (DCWithName));
1282                         ds.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (s1)));
1283
1284                         string s2 = @"{FooMember:"" \""{dummy:string}\""""}";
1285                         ds.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (s2)));
1286                 }
1287
1288                 [Test]
1289                 [Category ("NotWorking")]
1290                 public void TypeIsNotPartsOfKnownTypes ()
1291                 {
1292                         var dcs = new DataContractSerializer (typeof (string));
1293                         Assert.AreEqual (0, dcs.KnownTypes.Count, "KnownTypes #1");
1294                         var dcjs = new DataContractJsonSerializer (typeof (string));
1295                         Assert.AreEqual (0, dcjs.KnownTypes.Count, "KnownTypes #2");
1296                 }
1297
1298                 [Test]
1299                 public void ReadWriteNullObject ()
1300                 {
1301                         DataContractJsonSerializer dcjs = new DataContractJsonSerializer (typeof (string));
1302                         using (MemoryStream ms = new MemoryStream ()) {
1303                                 dcjs.WriteObject (ms, null);
1304                                 ms.Position = 0;
1305                                 using (StreamReader sr = new StreamReader (ms)) {
1306                                         string data = sr.ReadToEnd ();
1307                                         Assert.AreEqual ("null", data, "WriteObject(stream,null)");
1308
1309                                         ms.Position = 0;
1310                                         Assert.IsNull (dcjs.ReadObject (ms), "ReadObject(stream)");
1311                                 }
1312                         };
1313                 }
1314
1315                 object ReadWriteObject (Type type, object obj, string expected)
1316                 {
1317                         using (MemoryStream ms = new MemoryStream ()) {
1318                                 DataContractJsonSerializer dcjs = new DataContractJsonSerializer (type);
1319                                 dcjs.WriteObject (ms, obj);
1320                                 ms.Position = 0;
1321                                 using (StreamReader sr = new StreamReader (ms)) {
1322                                         Assert.AreEqual (expected, sr.ReadToEnd (), "WriteObject");
1323
1324                                         ms.Position = 0;
1325                                         return dcjs.ReadObject (ms);
1326                                 }
1327                         }
1328                 }
1329
1330                 [Test]
1331                 [Ignore ("Wrong test case. See bug #573691")]
1332                 public void ReadWriteObject_Single_SpecialCases ()
1333                 {
1334                         Assert.IsTrue (Single.IsNaN ((float) ReadWriteObject (typeof (float), Single.NaN, "NaN")));
1335                         Assert.IsTrue (Single.IsNegativeInfinity ((float) ReadWriteObject (typeof (float), Single.NegativeInfinity, "-INF")));
1336                         Assert.IsTrue (Single.IsPositiveInfinity ((float) ReadWriteObject (typeof (float), Single.PositiveInfinity, "INF")));
1337                 }
1338
1339                 [Test]
1340                 [Ignore ("Wrong test case. See bug #573691")]
1341                 public void ReadWriteObject_Double_SpecialCases ()
1342                 {
1343                         Assert.IsTrue (Double.IsNaN ((double) ReadWriteObject (typeof (double), Double.NaN, "NaN")));
1344                         Assert.IsTrue (Double.IsNegativeInfinity ((double) ReadWriteObject (typeof (double), Double.NegativeInfinity, "-INF")));
1345                         Assert.IsTrue (Double.IsPositiveInfinity ((double) ReadWriteObject (typeof (double), Double.PositiveInfinity, "INF")));
1346                 }
1347
1348                 [Test]
1349                 public void ReadWriteDateTime ()
1350                 {
1351                         var ms = new MemoryStream ();
1352                         DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (Query));
1353                         Query query = new Query () {
1354                                 StartDate = new DateTime (2010, 3, 4, 5, 6, 7),
1355                                 EndDate = new DateTime (2010, 4, 5, 6, 7, 8)
1356                                 };
1357                         serializer.WriteObject (ms, query);
1358                         Assert.AreEqual ("{\"StartDate\":\"\\/Date(1267679167000)\\/\",\"EndDate\":\"\\/Date(1270447628000)\\/\"}", Encoding.UTF8.GetString (ms.ToArray ()), "#1");
1359                         ms.Position = 0;
1360                         Console.WriteLine (new StreamReader (ms).ReadToEnd ());
1361                         ms.Position = 0;
1362                         var q = (Query) serializer.ReadObject(ms);
1363                         Assert.AreEqual (query.StartDate, q.StartDate, "#2");
1364                         Assert.AreEqual (query.EndDate, q.EndDate, "#3");
1365                 }
1366
1367                 [DataContract(Name = "DateTest")]
1368                 public class DateTest
1369                 {
1370                         [DataMember(Name = "should_have_value")]
1371                         public DateTime? ShouldHaveValue { get; set; }
1372                 }
1373
1374                 //
1375                 // This tests both the extended format "number-0500" as well
1376                 // as the nullable field in the structure
1377                 [Test]
1378                 public void BugXamarin163 ()
1379                 {
1380                         string json = @"{""should_have_value"":""\/Date(1277355600000-0500)\/""}";
1381
1382                         byte[] bytes = global::System.Text.Encoding.UTF8.GetBytes(json);
1383                         Stream inputStream = new MemoryStream(bytes);
1384                         
1385                         DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DateTest));
1386                         DateTest t = serializer.ReadObject(inputStream) as DateTest;
1387                         Assert.AreEqual (634129344000000000, t.ShouldHaveValue.Value.Ticks, "#1");
1388                 }
1389
1390                 [Test]
1391                 public void NullableFieldsShouldSupportNullValue ()
1392                 {
1393                         string json = @"{""should_have_value"":null}";
1394                         var inputStream = new MemoryStream (Encoding.UTF8.GetBytes (json));
1395                         DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DateTest));
1396                         Console.WriteLine ("# serializer assembly: {0}", serializer.GetType ().Assembly.Location);
1397                         DateTest t = serializer.ReadObject (inputStream) as DateTest;
1398                         Assert.AreEqual (false, t.ShouldHaveValue.HasValue, "#2");
1399                 }
1400                 
1401                 [Test]
1402                 public void DeserializeNullMember ()
1403                 {
1404                         var ds = new DataContractJsonSerializer (typeof (ClassA));
1405                         var stream = new MemoryStream ();
1406                         var a = new ClassA ();
1407                         ds.WriteObject (stream, a);
1408                         stream.Position = 0;
1409                         a = (ClassA) ds.ReadObject (stream);
1410                         Assert.IsNull (a.B, "#1");
1411                 }
1412
1413                 [Test]
1414                 public void OnDeserializationMethods ()
1415                 {
1416                         var ds = new DataContractJsonSerializer (typeof (GSPlayerListErg));
1417                         var obj = new GSPlayerListErg ();
1418                         var ms = new MemoryStream ();
1419                         ds.WriteObject (ms, obj);
1420                         ms.Position = 0;
1421                         ds.ReadObject (ms);
1422                         Assert.IsTrue (GSPlayerListErg.A, "A");
1423                         Assert.IsTrue (GSPlayerListErg.B, "B");
1424                         Assert.IsTrue (GSPlayerListErg.C, "C");
1425                 }
1426                 
1427                 [Test]
1428                 public void WriteChar ()
1429                 {
1430                         DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof (CharTest));
1431                         using (MemoryStream ms = new MemoryStream()) {
1432                                 serializer.WriteObject(ms, new CharTest ());
1433                                 ms.Position = 0L;
1434                                 using (StreamReader reader = new StreamReader(ms)) {
1435                                         reader.ReadToEnd();
1436                                 }
1437                         }
1438                 }
1439
1440                 [Test]
1441                 public void DictionarySerialization ()
1442                 {
1443                         var dict = new MyDictionary<string,string> ();
1444                         dict.Add ("key", "value");
1445                         var serializer = new DataContractJsonSerializer (dict.GetType ());
1446                         var stream = new MemoryStream ();
1447                         serializer.WriteObject (stream, dict);
1448                         stream.Position = 0;
1449
1450                         Assert.AreEqual ("[{\"Key\":\"key\",\"Value\":\"value\"}]", new StreamReader (stream).ReadToEnd (), "#1");
1451                         stream.Position = 0;
1452                         dict = (MyDictionary<string,string>) serializer.ReadObject (stream);
1453                         Assert.AreEqual (1, dict.Count, "#2");
1454                         Assert.AreEqual ("value", dict ["key"], "#3");
1455                 }
1456
1457                 [Test]
1458                 public void ExplicitCustomDictionarySerialization ()
1459                 {
1460                         var dict = new MyExplicitDictionary<string,string> ();
1461                         dict.Add ("key", "value");
1462                         var serializer = new DataContractJsonSerializer (dict.GetType ());
1463                         var stream = new MemoryStream ();
1464                         serializer.WriteObject (stream, dict);
1465                         stream.Position = 0;
1466
1467                         Assert.AreEqual ("[{\"Key\":\"key\",\"Value\":\"value\"}]", new StreamReader (stream).ReadToEnd (), "#1");
1468                         stream.Position = 0;
1469                         dict = (MyExplicitDictionary<string,string>) serializer.ReadObject (stream);
1470                         Assert.AreEqual (1, dict.Count, "#2");
1471                         Assert.AreEqual ("value", dict ["key"], "#3");
1472                 }
1473
1474                 [Test]
1475                 public void Bug13485 ()
1476                 {
1477                         const string json = "{ \"Name\" : \"Test\", \"Value\" : \"ValueA\" }";
1478
1479                         string result = string.Empty;
1480                         var serializer = new DataContractJsonSerializer (typeof (Bug13485Type));
1481                         Bug13485Type entity;
1482                         using (var stream = new MemoryStream (Encoding.UTF8.GetBytes (json)))
1483                                 entity = (Bug13485Type) serializer.ReadObject (stream);
1484
1485                         result = entity.GetValue;
1486                         Assert.AreEqual ("ValueA", result, "#1");
1487                 }
1488
1489                 [DataContract(Name = "UriTest")]
1490                 public class UriTest
1491                 {
1492                         [DataMember(Name = "members")]
1493                         public Uri MembersRelativeLink { get; set; }
1494                 }
1495
1496                 [Test]
1497                 public void Bug15169 ()
1498                 {
1499                         const string json = "{\"members\":\"foo/bar/members\"}";
1500                         var serializer = new DataContractJsonSerializer (typeof (UriTest));
1501                         UriTest entity;
1502                         using (var stream = new MemoryStream (Encoding.UTF8.GetBytes (json)))
1503                                 entity = (UriTest) serializer.ReadObject (stream);
1504
1505                         Assert.AreEqual ("foo/bar/members", entity.MembersRelativeLink.ToString ());
1506                 }
1507                 
1508                 #region Test methods for collection serialization
1509                 
1510                 [Test]
1511                 public void TestArrayListSerialization ()
1512                 {
1513                         var collection = new ArrayListContainer ();
1514                         var expectedOutput = "{\"Items\":[\"banana\",\"apple\"]}";
1515                         var expectedItemsCount = 4;
1516                         
1517                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1518                         var stream = new MemoryStream ();
1519                         serializer.WriteObject (stream, collection);
1520
1521                         stream.Position = 0;
1522                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1523
1524                         stream.Position = 0;
1525                         collection = (ArrayListContainer) serializer.ReadObject (stream);
1526                         
1527                         Assert.AreEqual (expectedItemsCount, collection.Items.Count, "#2");
1528                 }
1529                 
1530                 [Test]
1531                 [ExpectedException (typeof (InvalidDataContractException))]
1532                 public void TestBitArraySerialization ()
1533                 {
1534                         var collection = new BitArrayContainer ();
1535                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1536                         var stream = new MemoryStream ();
1537                         serializer.WriteObject (stream, collection);
1538                 }
1539                 
1540                 [Test]
1541                 public void TestHashtableSerialization ()
1542                 {
1543                         var collection = new HashtableContainer ();
1544                         var expectedOutput = "{\"Items\":[{\"Key\":\"key1\",\"Value\":\"banana\"},{\"Key\":\"key2\",\"Value\":\"apple\"}]}";
1545                         
1546                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1547                         var stream = new MemoryStream ();
1548                         serializer.WriteObject (stream, collection);
1549
1550                         stream.Position = 0;
1551                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1552                 }
1553                 
1554                 [Test]
1555                 [ExpectedException (typeof (ArgumentException))]
1556                 public void TestHashtableDeserialization ()
1557                 {
1558                         var collection = new HashtableContainer ();
1559                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1560                         var stream = new MemoryStream ();
1561                         serializer.WriteObject (stream, collection);
1562                         
1563                         stream.Position = 0;
1564                         serializer.ReadObject (stream);
1565                 }
1566                 
1567                 [Test]
1568                 [ExpectedException (typeof (InvalidDataContractException))]
1569                 public void TestQueueSerialization ()
1570                 {
1571                         var collection = new QueueContainer ();
1572                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1573                         var stream = new MemoryStream ();
1574                         serializer.WriteObject (stream, collection);
1575                 }
1576                 
1577                 [Test]
1578                 public void TestSortedListSerialization ()
1579                 {
1580                         var collection = new SortedListContainer ();
1581                         var expectedOutput = "{\"Items\":[{\"Key\":\"key1\",\"Value\":\"banana\"},{\"Key\":\"key2\",\"Value\":\"apple\"}]}";
1582                         
1583                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1584                         var stream = new MemoryStream ();
1585                         serializer.WriteObject (stream, collection);
1586
1587                         stream.Position = 0;
1588                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1589                 }
1590                 
1591                 [Test]
1592                 [ExpectedException (typeof (ArgumentException))]
1593                 public void TestSortedListDeserialization ()
1594                 {
1595                         var collection = new SortedListContainer ();
1596                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1597                         var stream = new MemoryStream ();
1598                         serializer.WriteObject (stream, collection);
1599                         
1600                         stream.Position = 0;
1601                         serializer.ReadObject (stream);
1602                 }
1603                 
1604                 [Test]
1605                 [ExpectedException (typeof (InvalidDataContractException))]
1606                 public void TestStackSerialization ()
1607                 {
1608                         var collection = new StackContainer ();
1609                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1610                         var stream = new MemoryStream ();
1611                         serializer.WriteObject (stream, collection);
1612                 }
1613                 
1614                 [Test]
1615                 public void TestEnumerableWithAddSerialization ()
1616                 {
1617                         var collection = new EnumerableWithAddContainer ();
1618                         var expectedOutput = "{\"Items\":[\"banana\",\"apple\"]}";
1619                         var expectedItemsCount = 4;
1620                         
1621                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1622                         var stream = new MemoryStream ();
1623                         serializer.WriteObject (stream, collection);
1624
1625                         stream.Position = 0;
1626                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1627
1628                         stream.Position = 0;
1629                         collection = (EnumerableWithAddContainer) serializer.ReadObject (stream);
1630                         
1631                         Assert.AreEqual (expectedItemsCount, collection.Items.Count, "#2");
1632                 }
1633                 
1634                 [Test]
1635                 [ExpectedException (typeof (InvalidDataContractException))]
1636                 public void TestEnumerableWithSpecialAddSerialization ()
1637                 {
1638                         var collection = new EnumerableWithSpecialAddContainer ();                      
1639                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1640                         var stream = new MemoryStream ();
1641                         serializer.WriteObject (stream, collection);
1642                 }
1643         
1644                 [Test]
1645                 public void TestHashSetSerialization ()
1646                 {
1647                         var collection = new GenericHashSetContainer ();
1648                         var expectedOutput = "{\"Items\":[\"banana\",\"apple\"]}";
1649                         var expectedItemsCount = 2;
1650                         
1651                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1652                         var stream = new MemoryStream ();
1653                         serializer.WriteObject (stream, collection);
1654
1655                         stream.Position = 0;
1656                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1657
1658                         stream.Position = 0;
1659                         collection = (GenericHashSetContainer) serializer.ReadObject (stream);
1660                         
1661                         Assert.AreEqual (expectedItemsCount, collection.Items.Count, "#2");
1662                 }
1663                 
1664                 [Test]
1665                 public void TestLinkedListSerialization ()
1666                 {
1667                         var collection = new GenericLinkedListContainer ();
1668                         var expectedOutput = "{\"Items\":[\"banana\",\"apple\"]}";
1669                         var expectedItemsCount = 4;
1670                         
1671                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1672                         var stream = new MemoryStream ();
1673                         serializer.WriteObject (stream, collection);
1674
1675                         stream.Position = 0;
1676                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1677
1678                         stream.Position = 0;
1679                         collection = (GenericLinkedListContainer) serializer.ReadObject (stream);
1680                         
1681                         Assert.AreEqual (expectedItemsCount, collection.Items.Count, "#2");
1682                 }
1683                 
1684                 [Test]
1685                 [ExpectedException (typeof (InvalidDataContractException))]
1686                 public void TestGenericQueueSerialization ()
1687                 {
1688                         var collection = new GenericQueueContainer ();                  
1689                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1690                         var stream = new MemoryStream ();
1691                         serializer.WriteObject (stream, collection);
1692                 }
1693                 
1694                 [Test]
1695                 [ExpectedException (typeof (InvalidDataContractException))]
1696                 public void TestGenericStackSerialization ()
1697                 {
1698                         var collection = new GenericStackContainer ();                  
1699                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1700                         var stream = new MemoryStream ();
1701                         serializer.WriteObject (stream, collection);
1702                 }
1703                 
1704                 [Test]
1705                 public void TestGenericDictionarySerialization ()
1706                 {
1707                         var collection = new GenericDictionaryContainer ();                     
1708                         var expectedOutput = "{\"Items\":[{\"Key\":\"key1\",\"Value\":\"banana\"},{\"Key\":\"key2\",\"Value\":\"apple\"}]}";
1709                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1710                         var stream = new MemoryStream ();
1711                         serializer.WriteObject (stream, collection);
1712                         
1713                         stream.Position = 0;
1714                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1715                 }
1716                 
1717                 [Test]
1718                 [ExpectedException (typeof (ArgumentException))]
1719                 public void TestGenericDictionaryDeserialization ()
1720                 {
1721                         var collection = new GenericDictionaryContainer ();                     
1722                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1723                         var stream = new MemoryStream ();
1724                         serializer.WriteObject (stream, collection);
1725                         
1726                         stream.Position = 0;
1727                         serializer.ReadObject (stream);
1728                 }
1729                 
1730                 [Test]
1731                 public void TestGenericSortedListSerialization ()
1732                 {
1733                         var collection = new GenericSortedListContainer ();                     
1734                         var expectedOutput = "{\"Items\":[{\"Key\":\"key1\",\"Value\":\"banana\"},{\"Key\":\"key2\",\"Value\":\"apple\"}]}";
1735                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1736                         var stream = new MemoryStream ();
1737                         serializer.WriteObject (stream, collection);
1738                         
1739                         stream.Position = 0;
1740                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1741                 }
1742                 
1743                 [Test]
1744                 [ExpectedException (typeof (ArgumentException))]
1745                 public void TestGenericSortedListDeserialization ()
1746                 {
1747                         var collection = new GenericSortedListContainer ();                     
1748                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1749                         var stream = new MemoryStream ();
1750                         serializer.WriteObject (stream, collection);
1751                         
1752                         stream.Position = 0;
1753                         serializer.ReadObject (stream);
1754                 }
1755                 
1756                 [Test]
1757                 public void TestGenericSortedDictionarySerialization ()
1758                 {
1759                         var collection = new GenericSortedDictionaryContainer ();                       
1760                         var expectedOutput = "{\"Items\":[{\"Key\":\"key1\",\"Value\":\"banana\"},{\"Key\":\"key2\",\"Value\":\"apple\"}]}";
1761                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1762                         var stream = new MemoryStream ();
1763                         serializer.WriteObject (stream, collection);
1764                         
1765                         stream.Position = 0;
1766                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1767                 }
1768                 
1769                 [Test]
1770                 [ExpectedException (typeof (ArgumentException))]
1771                 public void TestGenericSortedDictionaryDeserialization ()
1772                 {
1773                         var collection = new GenericSortedDictionaryContainer ();                       
1774                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1775                         var stream = new MemoryStream ();
1776                         serializer.WriteObject (stream, collection);
1777                         
1778                         stream.Position = 0;
1779                         serializer.ReadObject (stream);
1780                 }
1781                 
1782                 [Test]
1783                 public void TestGenericEnumerableWithAddSerialization ()
1784                 {
1785                         var collection = new GenericEnumerableWithAddContainer ();
1786                         var expectedOutput = "{\"Items\":[\"banana\",\"apple\"]}";
1787                         var expectedItemsCount = 4;
1788                         
1789                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1790                         var stream = new MemoryStream ();
1791                         serializer.WriteObject (stream, collection);
1792
1793                         stream.Position = 0;
1794                         Assert.AreEqual (expectedOutput, new StreamReader (stream).ReadToEnd (), "#1");
1795
1796                         stream.Position = 0;
1797                         collection = (GenericEnumerableWithAddContainer) serializer.ReadObject (stream);
1798                         
1799                         Assert.AreEqual (expectedItemsCount, collection.Items.Count, "#2");
1800                 }
1801                 
1802                 [Test]
1803                 [ExpectedException (typeof (InvalidDataContractException))]
1804                 public void TestGenericEnumerableWithSpecialAddSerialization ()
1805                 {
1806                         var collection = new GenericEnumerableWithSpecialAddContainer ();                       
1807                         var serializer = new DataContractJsonSerializer (collection.GetType ());
1808                         var stream = new MemoryStream ();
1809                         serializer.WriteObject (stream, collection);
1810                 }
1811                 
1812                 [Test]
1813                 [ExpectedException (typeof (InvalidDataContractException))]
1814                 public void TestNonCollectionGetOnlyProperty ()
1815                 {
1816                         var o = new NonCollectionGetOnlyContainer ();                   
1817                         var serializer = new DataContractJsonSerializer (o.GetType ());
1818                         var stream = new MemoryStream ();
1819                         serializer.WriteObject (stream, o);
1820                 }
1821                 
1822                 #endregion
1823         }
1824         
1825         public class CharTest
1826         {
1827                 public char Foo;
1828         }
1829
1830         public class TestData
1831         {
1832                 public string Foo { get; set; }
1833                 public string Bar { get; set; }
1834                 internal string Baz { get; set; }
1835         }
1836
1837         public enum Colors {
1838                 Red, Green, Blue
1839         }
1840
1841         [DataContract (Name = "_ColorsWithDC")]
1842         public enum ColorsWithDC {
1843
1844                 [EnumMember (Value = "_Red")]
1845                 Red, 
1846                 [EnumMember]
1847                 Green, 
1848                 Blue
1849         }
1850
1851
1852         public enum ColorsEnumMemberNoDC {
1853                 [EnumMember (Value = "_Red")]
1854                 Red, 
1855                 [EnumMember]
1856                 Green, 
1857                 Blue
1858         }
1859
1860         [DataContract]
1861         public class DCWithEnum {
1862                 [DataMember (Name = "_colors")]
1863                 public Colors colors;
1864         }
1865
1866         [DataContract]
1867         public class DCEmpty
1868         {
1869                 // serializer doesn't touch it.
1870                 public string Foo = "TEST";
1871         }
1872
1873         [DataContract]
1874         public class DCSimple1
1875         {
1876                 [DataMember]
1877                 public string Foo = "TEST";
1878         }
1879
1880         [DataContract]
1881         public class DCHasNonDC
1882         {
1883                 [DataMember]
1884                 public NonDC Hoge= new NonDC ();
1885         }
1886
1887         public class NonDC
1888         {
1889                 public string Whee = "whee!";
1890         }
1891
1892         [DataContract]
1893         public class DCHasSerializable
1894         {
1895                 [DataMember]
1896                 public SimpleSer1 Ser = new SimpleSer1 ();
1897         }
1898
1899         [DataContract (Name = "Foo")]
1900         public class DCWithName
1901         {
1902                 [DataMember (Name = "FooMember")]
1903                 public string DMWithName = "value";
1904         }
1905
1906         [DataContract (Name = "")]
1907         public class DCWithEmptyName
1908         {
1909                 [DataMember]
1910                 public string Foo;
1911         }
1912
1913         [DataContract (Name = null)]
1914         public class DCWithNullName
1915         {
1916                 [DataMember]
1917                 public string Foo;
1918         }
1919
1920         [DataContract (Namespace = "")]
1921         public class DCWithEmptyNamespace
1922         {
1923                 [DataMember]
1924                 public string Foo;
1925         }
1926
1927         [Serializable]
1928         public class SimpleSer1
1929         {
1930                 public string Doh = "doh!";
1931         }
1932
1933         public class Wrapper
1934         {
1935                 [DataContract]
1936                 public class DCWrapped
1937                 {
1938                 }
1939         }
1940
1941         [DataContract]
1942         public class CollectionContainer
1943         {
1944                 Collection<string> items = new Collection<string> ();
1945
1946                 [DataMember]
1947                 public Collection<string> Items {
1948                         get { return items; }
1949                 }
1950         }
1951
1952         [CollectionDataContract]
1953         public class DataCollection<T> : Collection<T>
1954         {
1955         }
1956
1957         [DataContract]
1958         public class DataCollectionContainer
1959         {
1960                 DataCollection<string> items = new DataCollection<string> ();
1961
1962                 [DataMember]
1963                 public DataCollection<string> Items {
1964                         get { return items; }
1965                 }
1966         }
1967
1968         [DataContract]
1969         class SerializeNonDCArrayType
1970         {
1971                 [DataMember]
1972                 public NonDCItem [] IPAddresses = new NonDCItem [0];
1973         }
1974
1975         public class NonDCItem
1976         {
1977                 public byte [] Data { get; set; }
1978         }
1979
1980         [DataContract]
1981         public class VerifyObjectNameTestData
1982         {
1983                 [DataMember]
1984                 string Member1 = "foo";
1985         }
1986
1987         [Serializable]
1988         public class KeyValueTestData {
1989                 public List<KeyValuePair<string,string>> TestData = new List<KeyValuePair<string,string>>();
1990         }
1991
1992         [DataContract] // bug #586169
1993         public class Query
1994         {
1995                 [DataMember (Order=1)]
1996                 public DateTime StartDate { get; set; }
1997                 [DataMember (Order=2)]
1998                 public DateTime EndDate { get; set; }
1999         }
2000
2001         public class ClassA {
2002                 public ClassB B { get; set; }
2003         }
2004
2005         public class ClassB
2006         {
2007         }
2008
2009         public class GSPlayerListErg
2010         {
2011                 public GSPlayerListErg ()
2012                 {
2013                         Init ();
2014                 }
2015
2016                 void Init ()
2017                 {
2018                         C = true;
2019                 }
2020
2021                 [OnDeserializing]
2022                 public void OnDeserializing (StreamingContext c)
2023                 {
2024                         A = true;
2025                         Init ();
2026                 }
2027
2028                 [OnDeserialized]
2029                 void OnDeserialized (StreamingContext c)
2030                 {
2031                         B = true;
2032                 }
2033
2034                 public static bool A, B, C;
2035
2036                 [DataMember (Name = "T")]
2037                 public long CodedServerTimeUTC { get; set; }
2038                 public DateTime ServerTimeUTC { get; set; }
2039         }
2040 }
2041
2042 [DataContract]
2043 class GlobalSample1
2044 {
2045 }
2046
2047
2048 public class MyDictionary<K, V> : System.Collections.Generic.IDictionary<K, V>
2049 {
2050         Dictionary<K,V> dic = new Dictionary<K,V> ();
2051
2052         public void Add (K key, V value)
2053         {
2054                 dic.Add (key,  value);
2055         }
2056
2057         public bool ContainsKey (K key)
2058         {
2059                 return dic.ContainsKey (key);
2060         }
2061
2062         public ICollection<K> Keys {
2063                 get { return dic.Keys; }
2064         }
2065
2066         public bool Remove (K key)
2067         {
2068                 return dic.Remove (key);
2069         }
2070
2071         public bool TryGetValue (K key, out V value)
2072         {
2073                 return dic.TryGetValue (key, out value);
2074         }
2075
2076         public ICollection<V> Values {
2077                 get { return dic.Values; }
2078         }
2079
2080         public V this [K key] {
2081                 get { return dic [key]; }
2082                 set { dic [key] = value; }
2083         }
2084
2085         IEnumerator IEnumerable.GetEnumerator ()
2086         {
2087                 return dic.GetEnumerator ();
2088         }
2089
2090         ICollection<KeyValuePair<K,V>> Coll {
2091                 get { return (ICollection<KeyValuePair<K,V>>) dic; }
2092         }
2093
2094         public void Add (KeyValuePair<K, V> item)
2095         {
2096                 Coll.Add (item);
2097         }
2098
2099         public void Clear ()
2100         {
2101                 dic.Clear ();
2102         }
2103
2104         public bool Contains (KeyValuePair<K, V> item)
2105         {
2106                 return Coll.Contains (item);
2107         }
2108
2109         public void CopyTo (KeyValuePair<K, V> [] array, int arrayIndex)
2110         {
2111                 Coll.CopyTo (array, arrayIndex);
2112         }
2113
2114         public int Count {
2115                 get { return dic.Count; }
2116         }
2117
2118         public bool IsReadOnly {
2119                 get { return Coll.IsReadOnly; }
2120         }
2121
2122         public bool Remove (KeyValuePair<K, V> item)
2123         {
2124                 return Coll.Remove (item);
2125         }
2126
2127         public IEnumerator<KeyValuePair<K, V>> GetEnumerator ()
2128         {
2129                 return Coll.GetEnumerator ();
2130         }
2131 }
2132
2133 public class MyExplicitDictionary<K, V> : IDictionary<K, V> {
2134
2135         Dictionary<K,V> dic = new Dictionary<K,V> ();
2136
2137         public void Add (K key, V value)
2138         {
2139                 dic.Add (key,  value);
2140         }
2141
2142         public bool ContainsKey (K key)
2143         {
2144                 return dic.ContainsKey (key);
2145         }
2146
2147         ICollection<K> IDictionary<K, V>.Keys {
2148                 get { return dic.Keys; }
2149         }
2150
2151         public bool Remove (K key)
2152         {
2153                 return dic.Remove (key);
2154         }
2155
2156         public bool TryGetValue (K key, out V value)
2157         {
2158                 return dic.TryGetValue (key, out value);
2159         }
2160
2161         ICollection<V> IDictionary<K, V>.Values {
2162                 get { return dic.Values; }
2163         }
2164
2165         public V this [K key] {
2166                 get { return dic [key]; }
2167                 set { dic [key] = value; }
2168         }
2169
2170         IEnumerator IEnumerable.GetEnumerator ()
2171         {
2172                 return dic.GetEnumerator ();
2173         }
2174
2175         ICollection<KeyValuePair<K,V>> Coll {
2176                 get { return (ICollection<KeyValuePair<K,V>>) dic; }
2177         }
2178
2179         public void Add (KeyValuePair<K, V> item)
2180         {
2181                 Coll.Add (item);
2182         }
2183
2184         public void Clear ()
2185         {
2186                 dic.Clear ();
2187         }
2188
2189         public bool Contains (KeyValuePair<K, V> item)
2190         {
2191                 return Coll.Contains (item);
2192         }
2193
2194         public void CopyTo (KeyValuePair<K, V> [] array, int arrayIndex)
2195         {
2196                 Coll.CopyTo (array, arrayIndex);
2197         }
2198
2199         public int Count {
2200                 get { return dic.Count; }
2201         }
2202
2203         public bool IsReadOnly {
2204                 get { return Coll.IsReadOnly; }
2205         }
2206
2207         public bool Remove (KeyValuePair<K, V> item)
2208         {
2209                 return Coll.Remove (item);
2210         }
2211
2212         public IEnumerator<KeyValuePair<K, V>> GetEnumerator ()
2213         {
2214                 return Coll.GetEnumerator ();
2215         }
2216 }
2217
2218 [DataContract]
2219 public class Bug13485Type
2220 {
2221         [DataMember]
2222         public string Name { get; set; }
2223
2224         [DataMember (Name = "Value")]
2225         private string Value { get; set; }
2226
2227         public string GetValue { get { return this.Value; } }
2228 }
2229
2230 #region Test classes for Collection serialization
2231
2232 [DataContract]
2233         public abstract class CollectionContainer <V>
2234         {
2235                 V items;
2236
2237                 [DataMember]
2238                 public V Items
2239                 {
2240                         get {
2241                                 if (items == null) items = Init ();
2242                                 return items;
2243                         }
2244                 }
2245                 
2246                 public CollectionContainer ()
2247                 {
2248                         Init ();
2249                 }
2250         
2251                 protected abstract V Init ();
2252         }
2253         
2254         [DataContract]
2255         public class ArrayListContainer : CollectionContainer<ArrayList> {
2256                 protected override ArrayList Init ()
2257                 {
2258                         return new ArrayList { "banana", "apple" };
2259                 }
2260         }
2261         
2262         [DataContract]
2263         public class BitArrayContainer : CollectionContainer<BitArray> {
2264                 protected override BitArray Init ()
2265                 {
2266                         return new BitArray (new [] { false, true });
2267                 }
2268         }
2269         
2270         [DataContract]
2271         public class HashtableContainer : CollectionContainer<Hashtable> {
2272                 protected override Hashtable Init ()
2273                 {
2274                         var ht = new Hashtable ();
2275                         ht.Add ("key1", "banana");
2276                         ht.Add ("key2", "apple");
2277                         return ht;
2278                 }
2279         }
2280         
2281         [DataContract]
2282         public class QueueContainer : CollectionContainer<Queue> {
2283                 protected override Queue Init ()
2284                 {
2285                         var q = new Queue ();
2286                         q.Enqueue ("banana");
2287                         q.Enqueue ("apple");
2288                         return q;
2289                 }
2290         }
2291         
2292         [DataContract]
2293         public class SortedListContainer : CollectionContainer<SortedList> {
2294                 protected override SortedList Init ()
2295                 {
2296                         var l = new SortedList ();
2297                         l.Add ("key1", "banana");
2298                         l.Add ("key2", "apple");
2299                         return l;
2300                 }
2301         }
2302         
2303         [DataContract]
2304         public class StackContainer : CollectionContainer<Stack> {
2305                 protected override Stack Init ()
2306                 {
2307                         var s = new Stack ();
2308                         s.Push ("banana");
2309                         s.Push ("apple");
2310                         return s;
2311                 }
2312         }
2313
2314         public class EnumerableWithAdd : IEnumerable
2315         {
2316                 private ArrayList items;
2317
2318                 public EnumerableWithAdd()
2319                 {
2320                         items = new ArrayList();
2321                 }
2322
2323                 public IEnumerator GetEnumerator()
2324                 {
2325                         return items.GetEnumerator();
2326                 }
2327
2328                 public void Add(object value)
2329                 {
2330                         items.Add(value);
2331                 }
2332
2333                 public int Count
2334                 {
2335                         get {
2336                                 return items.Count;
2337                         }
2338                 }
2339         }
2340
2341         public class EnumerableWithSpecialAdd : IEnumerable
2342         {
2343                 private ArrayList items;
2344
2345                 public EnumerableWithSpecialAdd()
2346                 {
2347                         items = new ArrayList();
2348                 }
2349
2350                 public IEnumerator GetEnumerator()
2351                 {
2352                         return items.GetEnumerator();
2353                 }
2354
2355                 public void Add(object value, int index)
2356                 {
2357                         items.Add(value);
2358                 }
2359
2360                 public int Count
2361                 {
2362                         get
2363                         {
2364                                 return items.Count;
2365                         }
2366                 }
2367         }
2368
2369         [DataContract]
2370         public class EnumerableWithAddContainer : CollectionContainer<EnumerableWithAdd>
2371         {
2372                 protected override EnumerableWithAdd Init()
2373                 {
2374                         var s = new EnumerableWithAdd();
2375                         s.Add ("banana");
2376                         s.Add ("apple");
2377                         return s;
2378                 }
2379         }
2380
2381         [DataContract]
2382         public class EnumerableWithSpecialAddContainer : CollectionContainer<EnumerableWithSpecialAdd>
2383         {
2384                 protected override EnumerableWithSpecialAdd Init()
2385                 {
2386                         var s = new EnumerableWithSpecialAdd();
2387                         s.Add("banana", 0);
2388                         s.Add("apple", 0);
2389                         return s;
2390                 }
2391         }
2392
2393         [DataContract]
2394         public class GenericDictionaryContainer : CollectionContainer<Dictionary<string, string>> {
2395                 protected override Dictionary<string, string> Init ()
2396                 {
2397                         var d = new Dictionary<string, string> ();
2398                         d.Add ("key1", "banana");
2399                         d.Add ("key2", "apple");
2400                         return d;
2401                 }
2402         }
2403
2404         [DataContract]
2405         public class GenericHashSetContainer : CollectionContainer<HashSet<string>> {
2406                 protected override HashSet<string> Init ()
2407                 {
2408                         return new HashSet<string> { "banana", "apple" };
2409                 }
2410         }
2411
2412         [DataContract]
2413         public class GenericLinkedListContainer : CollectionContainer<LinkedList<string>> {
2414                 protected override LinkedList<string> Init ()
2415                 {
2416                         var l = new LinkedList<string> ();
2417                         l.AddFirst ("apple");
2418                         l.AddFirst ("banana");
2419                         return l;
2420                 }
2421         }
2422
2423         [DataContract]
2424         public class GenericListContainer : CollectionContainer<List<string>> {
2425                 protected override List<string> Init ()
2426                 {
2427                         return new List<string> { "banana", "apple" };
2428                 }
2429         }
2430
2431         [DataContract]
2432         public class GenericQueueContainer : CollectionContainer<Queue<string>> {
2433                 protected override Queue<string> Init ()
2434                 {
2435                         var q = new Queue<string> ();
2436                         q.Enqueue ("banana");
2437                         q.Enqueue ("apple" );
2438                         return q;
2439                 }
2440         }
2441
2442         [DataContract]
2443         public class GenericSortedDictionaryContainer : CollectionContainer<SortedDictionary<string, string>> {
2444                 protected override SortedDictionary<string, string> Init ()
2445                 {
2446                         var d = new SortedDictionary<string, string> ();
2447                         d.Add ("key1", "banana");
2448                         d.Add ("key2", "apple");
2449                         return d;
2450                 }
2451         }
2452
2453         [DataContract]
2454         public class GenericSortedListContainer : CollectionContainer<SortedList<string, string>> {
2455                 protected override SortedList<string, string> Init ()
2456                 {
2457                         var d = new SortedList<string, string> ();
2458                         d.Add ("key1", "banana");
2459                         d.Add ("key2", "apple");
2460                         return d;
2461                 }
2462         }
2463
2464         [DataContract]
2465         public class GenericStackContainer : CollectionContainer<Stack<string>> {
2466                 protected override Stack<string> Init ()
2467                 {
2468                         var s = new Stack<string> ();
2469                         s.Push ("banana");
2470                         s.Push ("apple" );
2471                         return s;
2472                 }
2473         }
2474
2475         public class GenericEnumerableWithAdd : IEnumerable<string>
2476         {
2477                 private List<string> items;
2478
2479                 public GenericEnumerableWithAdd()
2480                 {
2481                         items = new List<string>();
2482                 }
2483
2484                 IEnumerator IEnumerable.GetEnumerator()
2485                 {
2486                         return items.GetEnumerator ();
2487                 }
2488
2489                 public IEnumerator<string> GetEnumerator()
2490                 {
2491                         return items.GetEnumerator ();
2492                 }
2493
2494                 public void Add(string value)
2495                 {
2496                         items.Add(value);
2497                 }
2498
2499                 public int Count
2500                 {
2501                         get {
2502                                 return items.Count;
2503                         }
2504                 }
2505         }
2506
2507         public class GenericEnumerableWithSpecialAdd : IEnumerable<string>
2508         {
2509                 private List<string> items;
2510
2511                 public GenericEnumerableWithSpecialAdd()
2512                 {
2513                         items = new List<string>();
2514                 }
2515
2516                 IEnumerator IEnumerable.GetEnumerator()
2517                 {
2518                         return items.GetEnumerator ();
2519                 }
2520
2521                 public IEnumerator<string> GetEnumerator()
2522                 {
2523                         return items.GetEnumerator ();
2524                 }
2525
2526                 public void Add(string value, int index)
2527                 {
2528                         items.Add(value);
2529                 }
2530
2531                 public int Count
2532                 {
2533                         get
2534                         {
2535                                 return items.Count;
2536                         }
2537                 }
2538         }
2539
2540         [DataContract]
2541         public class GenericEnumerableWithAddContainer : CollectionContainer<GenericEnumerableWithAdd>
2542         {
2543                 protected override GenericEnumerableWithAdd Init()
2544                 {
2545                         var s = new GenericEnumerableWithAdd();
2546                         s.Add ("banana");
2547                         s.Add ("apple");
2548                         return s;
2549                 }
2550         }
2551
2552         [DataContract]
2553         public class GenericEnumerableWithSpecialAddContainer : CollectionContainer<GenericEnumerableWithSpecialAdd>
2554         {
2555                 protected override GenericEnumerableWithSpecialAdd Init()
2556                 {
2557                         var s = new GenericEnumerableWithSpecialAdd();
2558                         s.Add("banana", 0);
2559                         s.Add("apple", 0);
2560                         return s;
2561                 }
2562         }       
2563
2564         [DataContract]
2565         public class NonCollectionGetOnlyContainer
2566         {
2567                 string _test = "my string";
2568         
2569                 [DataMember]
2570                 public string MyString {
2571                         get {
2572                                 return _test;
2573                         }
2574                 }
2575         }       
2576
2577 #endregion