New tests.
[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 //
8 // Copyright (C) 2005-2007 Novell, Inc.  http://www.novell.com
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 //
32 // This test code contains tests for DataContractJsonSerializer, which is
33 // imported from DataContractSerializerTest.cs.
34 //
35
36 using System;
37 using System.Collections.Generic;
38 using System.Collections.ObjectModel;
39 using System.IO;
40 using System.Net;
41 using System.Runtime.Serialization;
42 using System.Runtime.Serialization.Json;
43 using System.Text;
44 using System.Xml;
45 using NUnit.Framework;
46
47 namespace MonoTests.System.Runtime.Serialization.Json
48 {
49         [TestFixture]
50         public class DataContractJsonSerializerTest
51         {
52                 static readonly XmlWriterSettings settings;
53
54                 static DataContractJsonSerializerTest ()
55                 {
56                         settings = new XmlWriterSettings ();
57                         settings.OmitXmlDeclaration = true;
58                 }
59
60                 [DataContract]
61                 class Sample1
62                 {
63                         [DataMember]
64                         public string Member1;
65                 }
66
67                 [Test]
68                 [ExpectedException (typeof (ArgumentNullException))]
69                 public void ConstructorTypeNull ()
70                 {
71                         new DataContractJsonSerializer (null);
72                 }
73
74                 [Test]
75                 public void ConstructorKnownTypesNull ()
76                 {
77                         // null knownTypes is allowed.
78                         new DataContractJsonSerializer (typeof (Sample1), (IEnumerable<Type>) null);
79                         new DataContractJsonSerializer (typeof (Sample1), "Foo", null);
80                 }
81
82                 [Test]
83                 [ExpectedException (typeof (ArgumentNullException))]
84                 public void ConstructorNameNull ()
85                 {
86                         new DataContractJsonSerializer (typeof (Sample1), (string) null);
87                 }
88
89                 [Test]
90                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
91                 public void ConstructorNegativeMaxObjects ()
92                 {
93                         new DataContractJsonSerializer (typeof (Sample1), "Sample1",
94                                 null, -1, false, null, false);
95                 }
96
97                 [Test]
98                 public void ConstructorMisc ()
99                 {
100                         new DataContractJsonSerializer (typeof (GlobalSample1)).WriteObject (new MemoryStream (), new GlobalSample1 ());
101                 }
102
103                 [Test]
104                 public void WriteObjectContent ()
105                 {
106                         StringWriter sw = new StringWriter ();
107                         using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
108                                 DataContractJsonSerializer ser =
109                                         new DataContractJsonSerializer (typeof (string));
110                                 xw.WriteStartElement ("my-element");
111                                 ser.WriteObjectContent (xw, "TEST STRING");
112                                 xw.WriteEndElement ();
113                         }
114                         Assert.AreEqual ("<my-element>TEST STRING</my-element>",
115                                 sw.ToString ());
116                 }
117
118                 // int
119
120                 [Test]
121                 public void SerializeIntXml ()
122                 {
123                         StringWriter sw = new StringWriter ();
124                         SerializeInt (XmlWriter.Create (sw, settings));
125                         Assert.AreEqual (
126                                 @"<root type=""number"">1</root>",
127                                 sw.ToString ());
128                 }
129
130                 [Test]
131                 public void SerializeIntJson ()
132                 {
133                         MemoryStream ms = new MemoryStream ();
134                         SerializeInt (JsonReaderWriterFactory.CreateJsonWriter (ms));
135                         Assert.AreEqual (
136                                 "1",
137                                 Encoding.UTF8.GetString (ms.ToArray ()));
138                 }
139
140                 void SerializeInt (XmlWriter writer)
141                 {
142                         DataContractJsonSerializer ser =
143                                 new DataContractJsonSerializer (typeof (int));
144                         using (XmlWriter w = writer) {
145                                 ser.WriteObject (w, 1);
146                         }
147                 }
148
149                 // int, with rootName
150
151                 [Test]
152                 public void SerializeIntXmlWithRootName ()
153                 {
154                         StringWriter sw = new StringWriter ();
155                         SerializeIntWithRootName (XmlWriter.Create (sw, settings));
156                         Assert.AreEqual (
157                                 @"<myroot type=""number"">1</myroot>",
158                                 sw.ToString ());
159                 }
160
161                 [Test]
162                 // since JsonWriter supports only "root" as the root name, using
163                 // XmlWriter from JsonReaderWriterFactory will always fail with
164                 // an explicit rootName.
165                 [ExpectedException (typeof (SerializationException))]
166                 public void SerializeIntJsonWithRootName ()
167                 {
168                         MemoryStream ms = new MemoryStream ();
169                         SerializeIntWithRootName (JsonReaderWriterFactory.CreateJsonWriter (ms));
170                         Assert.AreEqual (
171                                 "1",
172                                 Encoding.UTF8.GetString (ms.ToArray ()));
173                 }
174
175                 void SerializeIntWithRootName (XmlWriter writer)
176                 {
177                         DataContractJsonSerializer ser =
178                                 new DataContractJsonSerializer (typeof (int), "myroot");
179                         using (XmlWriter w = writer) {
180                                 ser.WriteObject (w, 1);
181                         }
182                 }
183
184                 // pass typeof(DCEmpty), serialize int
185
186                 [Test]
187                 public void SerializeIntForDCEmptyXml ()
188                 {
189                         StringWriter sw = new StringWriter ();
190                         SerializeIntForDCEmpty (XmlWriter.Create (sw, settings));
191                         Assert.AreEqual (
192                                 @"<root type=""number"">1</root>",
193                                 sw.ToString ());
194                 }
195
196                 [Test]
197                 public void SerializeIntForDCEmptyJson ()
198                 {
199                         MemoryStream ms = new MemoryStream ();
200                         SerializeIntForDCEmpty (JsonReaderWriterFactory.CreateJsonWriter (ms));
201                         Assert.AreEqual (
202                                 "1",
203                                 Encoding.UTF8.GetString (ms.ToArray ()));
204                 }
205
206                 void SerializeIntForDCEmpty (XmlWriter writer)
207                 {
208                         DataContractJsonSerializer ser =
209                                 new DataContractJsonSerializer (typeof (DCEmpty));
210                         using (XmlWriter w = writer) {
211                                 ser.WriteObject (w, 1);
212                         }
213                 }
214
215                 // DCEmpty
216
217                 [Test]
218                 public void SerializeEmptyClassXml ()
219                 {
220                         StringWriter sw = new StringWriter ();
221                         SerializeEmptyClass (XmlWriter.Create (sw, settings));
222                         Assert.AreEqual (
223                                 @"<root type=""object"" />",
224                                 sw.ToString ());
225                 }
226
227                 [Test]
228                 public void SerializeEmptyClassJson ()
229                 {
230                         MemoryStream ms = new MemoryStream ();
231                         SerializeEmptyClass (JsonReaderWriterFactory.CreateJsonWriter (ms));
232                         Assert.AreEqual (
233                                 "{}",
234                                 Encoding.UTF8.GetString (ms.ToArray ()));
235                 }
236
237                 void SerializeEmptyClass (XmlWriter writer)
238                 {
239                         DataContractJsonSerializer ser =
240                                 new DataContractJsonSerializer (typeof (DCEmpty));
241                         using (XmlWriter w = writer) {
242                                 ser.WriteObject (w, new DCEmpty ());
243                         }
244                 }
245
246                 // string (primitive)
247
248                 [Test]
249                 public void SerializePrimitiveStringXml ()
250                 {
251                         StringWriter sw = new StringWriter ();
252                         SerializePrimitiveString (XmlWriter.Create (sw, settings));
253                         Assert.AreEqual (
254                                 "<root>TEST</root>",
255                                 sw.ToString ());
256                 }
257
258                 [Test]
259                 public void SerializePrimitiveStringJson ()
260                 {
261                         MemoryStream ms = new MemoryStream ();
262                         SerializePrimitiveString (JsonReaderWriterFactory.CreateJsonWriter (ms));
263                         Assert.AreEqual (
264                                 @"""TEST""",
265                                 Encoding.UTF8.GetString (ms.ToArray ()));
266                 }
267
268                 void SerializePrimitiveString (XmlWriter writer)
269                 {
270                         XmlObjectSerializer ser =
271                                 new DataContractJsonSerializer (typeof (string));
272                         using (XmlWriter w = writer) {
273                                 ser.WriteObject (w, "TEST");
274                         }
275                 }
276
277                 // QName (primitive but ...)
278
279                 [Test]
280                 public void SerializePrimitiveQNameXml ()
281                 {
282                         StringWriter sw = new StringWriter ();
283                         SerializePrimitiveQName (XmlWriter.Create (sw, settings));
284                         Assert.AreEqual (
285                                 "<root>foo:urn:foo</root>",
286                                 sw.ToString ());
287                 }
288
289                 [Test]
290                 public void SerializePrimitiveQNameJson ()
291                 {
292                         MemoryStream ms = new MemoryStream ();
293                         SerializePrimitiveQName (JsonReaderWriterFactory.CreateJsonWriter (ms));
294                         Assert.AreEqual (
295                                 @"""foo:urn:foo""",
296                                 Encoding.UTF8.GetString (ms.ToArray ()));
297                 }
298
299                 void SerializePrimitiveQName (XmlWriter writer)
300                 {
301                         XmlObjectSerializer ser =
302                                 new DataContractJsonSerializer (typeof (XmlQualifiedName));
303                         using (XmlWriter w = writer) {
304                                 ser.WriteObject (w, new XmlQualifiedName ("foo", "urn:foo"));
305                         }
306                 }
307
308                 // DBNull (primitive)
309
310                 [Test]
311                 public void SerializeDBNullXml ()
312                 {
313                         StringWriter sw = new StringWriter ();
314                         SerializeDBNull (XmlWriter.Create (sw, settings));
315                         Assert.AreEqual (
316                                 @"<root type=""object"" />",
317                                 sw.ToString ());
318                 }
319
320                 [Test]
321                 public void SerializeDBNullJson ()
322                 {
323                         MemoryStream ms = new MemoryStream ();
324                         SerializeDBNull (JsonReaderWriterFactory.CreateJsonWriter (ms));
325                         Assert.AreEqual (
326                                 "{}",
327                                 Encoding.UTF8.GetString (ms.ToArray ()));
328                 }
329
330                 void SerializeDBNull (XmlWriter writer)
331                 {
332                         DataContractJsonSerializer ser =
333                                 new DataContractJsonSerializer (typeof (DBNull));
334                         using (XmlWriter w = writer) {
335                                 ser.WriteObject (w, DBNull.Value);
336                         }
337                 }
338
339                 // DCSimple1
340
341                 [Test]
342                 public void SerializeSimpleClass1Xml ()
343                 {
344                         StringWriter sw = new StringWriter ();
345                         SerializeSimpleClass1 (XmlWriter.Create (sw, settings));
346                         Assert.AreEqual (
347                                 @"<root type=""object""><Foo>TEST</Foo></root>",
348                                 sw.ToString ());
349                 }
350
351                 [Test]
352                 public void SerializeSimpleClass1Json ()
353                 {
354                         MemoryStream ms = new MemoryStream ();
355                         SerializeSimpleClass1 (JsonReaderWriterFactory.CreateJsonWriter (ms));
356                         Assert.AreEqual (
357                                 @"{""Foo"":""TEST""}",
358                                 Encoding.UTF8.GetString (ms.ToArray ()));
359                 }
360
361                 void SerializeSimpleClass1 (XmlWriter writer)
362                 {
363                         DataContractJsonSerializer ser =
364                                 new DataContractJsonSerializer (typeof (DCSimple1));
365                         using (XmlWriter w = writer) {
366                                 ser.WriteObject (w, new DCSimple1 ());
367                         }
368                 }
369
370                 // NonDC
371
372                 [Test]
373                 // NonDC is not a DataContract type.
374                 public void SerializeNonDCOnlyCtor ()
375                 {
376                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (NonDC));
377                 }
378
379                 [Test]
380                 //[ExpectedException (typeof (InvalidDataContractException))]
381                 // NonDC is not a DataContract type.
382                 // UPDATE: non-DataContract types are became valid in RTM.
383                 public void SerializeNonDC ()
384                 {
385                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (NonDC));
386                         using (XmlWriter w = XmlWriter.Create (TextWriter.Null, settings)) {
387                                 ser.WriteObject (w, new NonDC ());
388                         }
389                 }
390
391                 // DCHasNonDC
392
393                 [Test]
394                 //[ExpectedException (typeof (InvalidDataContractException))]
395                 // DCHasNonDC itself is a DataContract type whose field is
396                 // marked as DataMember but its type is not DataContract.
397                 // UPDATE: non-DataContract types are became valid in RTM.
398                 public void SerializeDCHasNonDC ()
399                 {
400                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCHasNonDC));
401                         using (XmlWriter w = XmlWriter.Create (TextWriter.Null, settings)) {
402                                 ser.WriteObject (w, new DCHasNonDC ());
403                         }
404                 }
405
406                 // DCHasSerializable
407
408                 [Test]
409                 public void SerializeSimpleSerializable1Xml ()
410                 {
411                         StringWriter sw = new StringWriter ();
412                         SerializeSimpleSerializable1 (XmlWriter.Create (sw, settings));
413                         Assert.AreEqual (
414                                 @"<root type=""object""><Ser type=""object""><Doh>doh!</Doh></Ser></root>",
415                                 sw.ToString ());
416                 }
417
418                 [Test]
419                 public void SerializeSimpleSerializable1Json ()
420                 {
421                         MemoryStream ms = new MemoryStream ();
422                         SerializeSimpleSerializable1 (JsonReaderWriterFactory.CreateJsonWriter (ms));
423                         Assert.AreEqual (
424                                 @"{""Ser"":{""Doh"":""doh!""}}",
425                                 Encoding.UTF8.GetString (ms.ToArray ()));
426                 }
427
428                 // DCHasSerializable itself is DataContract and has a field
429                 // whose type is not contract but serializable.
430                 void SerializeSimpleSerializable1 (XmlWriter writer)
431                 {
432                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCHasSerializable));
433                         using (XmlWriter w = writer) {
434                                 ser.WriteObject (w, new DCHasSerializable ());
435                         }
436                 }
437
438                 [Test]
439                 public void SerializeDCWithNameXml ()
440                 {
441                         StringWriter sw = new StringWriter ();
442                         SerializeDCWithName (XmlWriter.Create (sw, settings));
443                         Assert.AreEqual (
444                                 @"<root type=""object""><FooMember>value</FooMember></root>",
445                                 sw.ToString ());
446                 }
447
448                 [Test]
449                 public void SerializeDCWithNameJson ()
450                 {
451                         MemoryStream ms = new MemoryStream ();
452                         SerializeDCWithName (JsonReaderWriterFactory.CreateJsonWriter (ms));
453                         Assert.AreEqual (
454                                 @"{""FooMember"":""value""}",
455                                 Encoding.UTF8.GetString (ms.ToArray ()));
456                 }
457
458                 void SerializeDCWithName (XmlWriter writer)
459                 {
460                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithName));
461                         using (XmlWriter w = writer) {
462                                 ser.WriteObject (w, new DCWithName ());
463                         }
464                 }
465
466                 [Test]
467                 public void SerializeDCWithEmptyName1 ()
468                 {
469                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEmptyName));
470                         StringWriter sw = new StringWriter ();
471                         DCWithEmptyName dc = new DCWithEmptyName ();
472                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
473                                 try {
474                                         ser.WriteObject (w, dc);
475                                 } catch (InvalidDataContractException) {
476                                         return;
477                                 }
478                         }
479                         Assert.Fail ("Expected InvalidDataContractException");
480                 }
481
482                 [Test]
483                 public void SerializeDCWithEmptyName2 ()
484                 {
485                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithName));
486                         StringWriter sw = new StringWriter ();
487
488                         /* DataContractAttribute.Name == "", not valid */
489                         DCWithEmptyName dc = new DCWithEmptyName ();
490                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
491                                 try {
492                                         ser.WriteObject (w, dc);
493                                 } catch (InvalidDataContractException) {
494                                         return;
495                                 }
496                         }
497                         Assert.Fail ("Expected InvalidDataContractException");
498                 }
499
500                 [Test]
501                 [Category("NotWorking")]
502                 public void SerializeDCWithNullName ()
503                 {
504                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithNullName));
505                         StringWriter sw = new StringWriter ();
506                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
507                                 try {
508                                         /* DataContractAttribute.Name == "", not valid */
509                                         ser.WriteObject (w, new DCWithNullName ());
510                                 } catch (InvalidDataContractException) {
511                                         return;
512                                 }
513                         }
514                         Assert.Fail ("Expected InvalidDataContractException");
515                 }
516
517                 [Test]
518                 public void SerializeDCWithEmptyNamespace1 ()
519                 {
520                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEmptyNamespace));
521                         StringWriter sw = new StringWriter ();
522                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
523                                 ser.WriteObject (w, new DCWithEmptyNamespace ());
524                         }
525                 }
526
527                 [Test]
528                 public void SerializeWrappedClassXml ()
529                 {
530                         StringWriter sw = new StringWriter ();
531                         SerializeWrappedClass (XmlWriter.Create (sw, settings));
532                         Assert.AreEqual (
533                                 @"<root type=""object"" />",
534                                 sw.ToString ());
535                 }
536
537                 [Test]
538                 public void SerializeWrappedClassJson ()
539                 {
540                         MemoryStream ms = new MemoryStream ();
541                         SerializeWrappedClass (JsonReaderWriterFactory.CreateJsonWriter (ms));
542                         Assert.AreEqual (
543                                 "{}",
544                                 Encoding.UTF8.GetString (ms.ToArray ()));
545                 }
546
547                 void SerializeWrappedClass (XmlWriter writer)
548                 {
549                         DataContractJsonSerializer ser =
550                                 new DataContractJsonSerializer (typeof (Wrapper.DCWrapped));
551                         using (XmlWriter w = writer) {
552                                 ser.WriteObject (w, new Wrapper.DCWrapped ());
553                         }
554                 }
555
556                 // CollectionContainer : Items must have a setter. (but became valid in RTM).
557                 [Test]
558                 public void SerializeReadOnlyCollectionMember ()
559                 {
560                         DataContractJsonSerializer ser =
561                                 new DataContractJsonSerializer (typeof (CollectionContainer));
562                         StringWriter sw = new StringWriter ();
563                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
564                                 ser.WriteObject (w, null);
565                         }
566                 }
567
568                 // DataCollectionContainer : Items must have a setter. (but became valid in RTM).
569                 [Test]
570                 public void SerializeReadOnlyDataCollectionMember ()
571                 {
572                         DataContractJsonSerializer ser =
573                                 new DataContractJsonSerializer (typeof (DataCollectionContainer));
574                         StringWriter sw = new StringWriter ();
575                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
576                                 ser.WriteObject (w, null);
577                         }
578                 }
579
580                 [Test]
581                 [Ignore ("https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=409970")]
582                 [ExpectedException (typeof (SerializationException))]
583                 public void DeserializeReadOnlyDataCollection_NullCollection ()
584                 {
585                         DataContractJsonSerializer ser =
586                                 new DataContractJsonSerializer (typeof (CollectionContainer));
587                         StringWriter sw = new StringWriter ();
588                         var c = new CollectionContainer ();
589                         c.Items.Add ("foo");
590                         c.Items.Add ("bar");
591                         using (XmlWriter w = XmlWriter.Create (sw, settings))
592                                 ser.WriteObject (w, c);
593                         // CollectionContainer.Items is null, so it cannot deserialize non-null collection.
594                         using (XmlReader r = XmlReader.Create (new StringReader (sw.ToString ())))
595                                 c = (CollectionContainer) ser.ReadObject (r);
596                 }
597
598                 [Test]
599                 public void SerializeGuidXml ()
600                 {
601                         StringWriter sw = new StringWriter ();
602                         SerializeGuid (XmlWriter.Create (sw, settings));
603                         Assert.AreEqual (
604                                 @"<root>00000000-0000-0000-0000-000000000000</root>",
605                                 sw.ToString ());
606                 }
607
608                 [Test]
609                 public void SerializeGuidJson ()
610                 {
611                         MemoryStream ms = new MemoryStream ();
612                         SerializeGuid (JsonReaderWriterFactory.CreateJsonWriter (ms));
613                         Assert.AreEqual (
614                                 @"""00000000-0000-0000-0000-000000000000""",
615                                 Encoding.UTF8.GetString (ms.ToArray ()));
616                 }
617
618                 void SerializeGuid (XmlWriter writer)
619                 {
620                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Guid));
621                         using (XmlWriter w = writer) {
622                                 ser.WriteObject (w, Guid.Empty);
623                         }
624                 }
625
626                 [Test]
627                 public void SerializeEnumXml ()
628                 {
629                         StringWriter sw = new StringWriter ();
630                         SerializeEnum (XmlWriter.Create (sw, settings));
631                         Assert.AreEqual (
632                                 @"<root type=""number"">0</root>",
633                                 sw.ToString ());
634                 }
635
636                 [Test]
637                 public void SerializeEnumJson ()
638                 {
639                         MemoryStream ms = new MemoryStream ();
640                         SerializeEnum (JsonReaderWriterFactory.CreateJsonWriter (ms));
641                         Assert.AreEqual (
642                                 "0",
643                                 Encoding.UTF8.GetString (ms.ToArray ()));
644                 }
645
646                 void SerializeEnum (XmlWriter writer)
647                 {
648                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Colors));
649                         using (XmlWriter w = writer) {
650                                 ser.WriteObject (w, new Colors ());
651                         }
652                 }
653
654                 [Test]
655                 public void SerializeEnum2Xml ()
656                 {
657                         StringWriter sw = new StringWriter ();
658                         SerializeEnum2 (XmlWriter.Create (sw, settings));
659                         Assert.AreEqual (
660                                 @"<root type=""number"">0</root>",
661                                 sw.ToString ());
662                 }
663
664                 [Test]
665                 public void SerializeEnum2Json ()
666                 {
667                         MemoryStream ms = new MemoryStream ();
668                         SerializeEnum2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
669                         Assert.AreEqual (
670                                 "0",
671                                 Encoding.UTF8.GetString (ms.ToArray ()));
672                 }
673
674                 void SerializeEnum2 (XmlWriter writer)
675                 {
676                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Colors));
677                         using (XmlWriter w = writer) {
678                                 ser.WriteObject (w, 0);
679                         }
680                 }
681
682                 [Test] // so, DataContract does not affect here.
683                 public void SerializeEnumWithDCXml ()
684                 {
685                         StringWriter sw = new StringWriter ();
686                         SerializeEnumWithDC (XmlWriter.Create (sw, settings));
687                         Assert.AreEqual (
688                                 @"<root type=""number"">0</root>",
689                                 sw.ToString ());
690                 }
691
692                 [Test]
693                 public void SerializeEnumWithDCJson ()
694                 {
695                         MemoryStream ms = new MemoryStream ();
696                         SerializeEnumWithDC (JsonReaderWriterFactory.CreateJsonWriter (ms));
697                         Assert.AreEqual (
698                                 "0",
699                                 Encoding.UTF8.GetString (ms.ToArray ()));
700                 }
701
702                 void SerializeEnumWithDC (XmlWriter writer)
703                 {
704                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
705                         using (XmlWriter w = writer) {
706                                 ser.WriteObject (w, new ColorsWithDC ());
707                         }
708                 }
709
710                 [Test]
711                 public void SerializeEnumWithNoDCXml ()
712                 {
713                         StringWriter sw = new StringWriter ();
714                         SerializeEnumWithNoDC (XmlWriter.Create (sw, settings));
715                         Assert.AreEqual (
716                                 @"<root type=""number"">0</root>",
717                                 sw.ToString ());
718                 }
719
720                 [Test]
721                 public void SerializeEnumWithNoDCJson ()
722                 {
723                         MemoryStream ms = new MemoryStream ();
724                         SerializeEnumWithNoDC (JsonReaderWriterFactory.CreateJsonWriter (ms));
725                         Assert.AreEqual (
726                                 "0",
727                                 Encoding.UTF8.GetString (ms.ToArray ()));
728                 }
729
730                 void SerializeEnumWithNoDC (XmlWriter writer)
731                 {
732                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsEnumMemberNoDC));
733                         using (XmlWriter w = writer) {
734                                 ser.WriteObject (w, new ColorsEnumMemberNoDC ());
735                         }
736                 }
737
738                 [Test]
739                 public void SerializeEnumWithDC2Xml ()
740                 {
741                         StringWriter sw = new StringWriter ();
742                         SerializeEnumWithDC2 (XmlWriter.Create (sw, settings));
743                         Assert.AreEqual (
744                                 @"<root type=""number"">3</root>",
745                                 sw.ToString ());
746                 }
747
748                 [Test]
749                 public void SerializeEnumWithDC2Json ()
750                 {
751                         MemoryStream ms = new MemoryStream ();
752                         SerializeEnumWithDC2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
753                         Assert.AreEqual (
754                                 "3",
755                                 Encoding.UTF8.GetString (ms.ToArray ()));
756                 }
757
758                 void SerializeEnumWithDC2 (XmlWriter writer)
759                 {
760                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
761                         using (XmlWriter w = writer) {
762                                 ser.WriteObject (w, 3);
763                         }
764                 }
765
766 /*
767                 [Test]
768                 [ExpectedException (typeof (SerializationException))]
769                 public void SerializeEnumWithDCInvalid ()
770                 {
771                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
772                         StringWriter sw = new StringWriter ();
773                         ColorsWithDC cdc = ColorsWithDC.Blue;
774                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
775                                 ser.WriteObject (w, cdc);
776                         }
777                 }
778 */
779
780                 [Test]
781                 public void SerializeDCWithEnumXml ()
782                 {
783                         StringWriter sw = new StringWriter ();
784                         SerializeDCWithEnum (XmlWriter.Create (sw, settings));
785                         Assert.AreEqual (
786                                 @"<root type=""object""><_colors type=""number"">0</_colors></root>",
787                                 sw.ToString ());
788                 }
789
790                 [Test]
791                 public void SerializeDCWithEnumJson ()
792                 {
793                         MemoryStream ms = new MemoryStream ();
794                         SerializeDCWithEnum (JsonReaderWriterFactory.CreateJsonWriter (ms));
795                         Assert.AreEqual (
796                                 @"{""_colors"":0}",
797                                 Encoding.UTF8.GetString (ms.ToArray ()));
798                 }
799
800                 void SerializeDCWithEnum (XmlWriter writer)
801                 {
802                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEnum));
803                         using (XmlWriter w = writer) {
804                                 ser.WriteObject (w, new DCWithEnum ());
805                         }
806                 }
807
808                 [Test]
809                 public void SerializerDCArrayXml ()
810                 {
811                         StringWriter sw = new StringWriter ();
812                         SerializerDCArray (XmlWriter.Create (sw, settings));
813                         Assert.AreEqual (
814                                 @"<root type=""array""><item type=""object""><_colors type=""number"">0</_colors></item><item type=""object""><_colors type=""number"">1</_colors></item></root>",
815                                 sw.ToString ());
816                 }
817
818                 [Test]
819                 public void SerializerDCArrayJson ()
820                 {
821                         MemoryStream ms = new MemoryStream ();
822                         SerializerDCArray (JsonReaderWriterFactory.CreateJsonWriter (ms));
823                         Assert.AreEqual (
824                                 @"[{""_colors"":0},{""_colors"":1}]",
825                                 Encoding.UTF8.GetString (ms.ToArray ()));
826                 }
827
828                 void SerializerDCArray (XmlWriter writer)
829                 {
830                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEnum []));
831                         DCWithEnum [] arr = new DCWithEnum [2];
832                         arr [0] = new DCWithEnum (); arr [0].colors = Colors.Red;
833                         arr [1] = new DCWithEnum (); arr [1].colors = Colors.Green;
834                         using (XmlWriter w = writer) {
835                                 ser.WriteObject (w, arr);
836                         }
837                 }
838
839                 [Test]
840                 public void SerializerDCArray2Xml ()
841                 {
842                         StringWriter sw = new StringWriter ();
843                         SerializerDCArray2 (XmlWriter.Create (sw, settings));
844                         Assert.AreEqual (
845                                 @"<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>",
846                                 sw.ToString ());
847                 }
848
849                 [Test]
850                 public void SerializerDCArray2Json ()
851                 {
852                         MemoryStream ms = new MemoryStream ();
853                         SerializerDCArray2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
854                         Assert.AreEqual (
855                                 @"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0},{""__type"":""DCSimple1:#MonoTests.System.Runtime.Serialization.Json"",""Foo"":""hello""}]",
856                                 Encoding.UTF8.GetString (ms.ToArray ()));
857                 }
858
859                 void SerializerDCArray2 (XmlWriter writer)
860                 {
861                         List<Type> known = new List<Type> ();
862                         known.Add (typeof (DCWithEnum));
863                         known.Add (typeof (DCSimple1));
864                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (object []), known);
865                         object [] arr = new object [2];
866                         arr [0] = new DCWithEnum (); ((DCWithEnum)arr [0]).colors = Colors.Red;
867                         arr [1] = new DCSimple1 (); ((DCSimple1) arr [1]).Foo = "hello";
868
869                         using (XmlWriter w = writer) {
870                                 ser.WriteObject (w, arr);
871                         }
872                 }
873
874                 [Test]
875                 public void SerializerDCArray3Xml ()
876                 {
877                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (int []));
878                         StringWriter sw = new StringWriter ();
879                         int [] arr = new int [2];
880                         arr [0] = 1; arr [1] = 2;
881
882                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
883                                 ser.WriteObject (w, arr);
884                         }
885
886                         Assert.AreEqual (
887                                 @"<root type=""array""><item type=""number"">1</item><item type=""number"">2</item></root>",
888                                 sw.ToString ());
889                 }
890
891                 [Test]
892                 public void SerializerDCArray3Json ()
893                 {
894                         MemoryStream ms = new MemoryStream ();
895                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (int []));
896                         int [] arr = new int [2];
897                         arr [0] = 1; arr [1] = 2;
898
899                         using (XmlWriter w = JsonReaderWriterFactory.CreateJsonWriter (ms)) {
900                                 ser.WriteObject (w, arr);
901                         }
902
903                         Assert.AreEqual (
904                                 @"[1,2]",
905                                 Encoding.UTF8.GetString (ms.ToArray ()));
906                 }
907
908                 [Test]
909                 // ... so, non-JSON XmlWriter is still accepted.
910                 public void SerializeNonDCArrayXml ()
911                 {
912                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (SerializeNonDCArrayType));
913                         StringWriter sw = new StringWriter ();
914                         using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
915                                 ser.WriteObject (xw, new SerializeNonDCArrayType ());
916                         }
917                         Assert.AreEqual (@"<root type=""object""><IPAddresses type=""array"" /></root>",
918                                 sw.ToString ());
919                 }
920
921                 [Test]
922                 public void SerializeNonDCArrayJson ()
923                 {
924                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (SerializeNonDCArrayType));
925                         MemoryStream ms = new MemoryStream ();
926                         using (XmlWriter xw = JsonReaderWriterFactory.CreateJsonWriter (ms)) {
927                                 ser.WriteObject (xw, new SerializeNonDCArrayType ());
928                         }
929                         Assert.AreEqual (@"{""IPAddresses"":[]}",
930                                 Encoding.UTF8.GetString (ms.ToArray ()));
931                 }
932
933                 [Test]
934                 public void SerializeNonDCArrayItems ()
935                 {
936                         DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (SerializeNonDCArrayType));
937                         StringWriter sw = new StringWriter ();
938                         using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
939                                 SerializeNonDCArrayType obj = new SerializeNonDCArrayType ();
940                                 obj.IPAddresses = new NonDCItem [] {new NonDCItem () { Data = new byte [] {1, 2, 3, 4} } };
941                                 ser.WriteObject (xw, obj);
942                         }
943
944                         XmlDocument doc = new XmlDocument ();
945                         doc.LoadXml (sw.ToString ());
946                         XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
947                         nsmgr.AddNamespace ("s", "http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization");
948                         nsmgr.AddNamespace ("n", "http://schemas.datacontract.org/2004/07/System.Net");
949                         nsmgr.AddNamespace ("a", "http://schemas.microsoft.com/2003/10/Serialization/Arrays");
950
951                         Assert.AreEqual (1, doc.SelectNodes ("/root/IPAddresses/item", nsmgr).Count, "#1");
952                         XmlElement el = doc.SelectSingleNode ("/root/IPAddresses/item/Data", nsmgr) as XmlElement;
953                         Assert.IsNotNull (el, "#3");
954                         Assert.AreEqual (4, el.SelectNodes ("item", nsmgr).Count, "#4");
955                 }
956
957                 [Test]
958                 public void MaxItemsInObjectGraph1 ()
959                 {
960                         // object count == maximum
961                         DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCEmpty), null, 1, false, null, false);
962                         s.WriteObject (XmlWriter.Create (TextWriter.Null), new DCEmpty ());
963                 }
964
965                 [Test]
966                 [ExpectedException (typeof (SerializationException))]
967                 public void MaxItemsInObjectGraph2 ()
968                 {
969                         // object count > maximum
970                         DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCSimple1), null, 1, false, null, false);
971                         s.WriteObject (XmlWriter.Create (TextWriter.Null), new DCSimple1 ());
972                 }
973
974                 [Test]
975                 public void DeserializeString ()
976                 {
977                         Assert.AreEqual ("ABC", Deserialize ("\"ABC\"", typeof (string)));
978                 }
979
980                 [Test]
981                 public void DeserializeInt ()
982                 {
983                         Assert.AreEqual (5, Deserialize ("5", typeof (int)));
984                 }
985
986                 [Test]
987                 public void DeserializeArray ()
988                 {
989                         int [] ret = (int []) Deserialize ("[5,6,7]", typeof (int []));
990                         Assert.AreEqual (5, ret [0], "#1");
991                         Assert.AreEqual (6, ret [1], "#2");
992                         Assert.AreEqual (7, ret [2], "#3");
993                 }
994
995                 [Test]
996                 public void DeserializeArrayUntyped ()
997                 {
998                         object [] ret = (object []) Deserialize ("[5,6,7]", typeof (object []));
999                         Assert.AreEqual (5, ret [0], "#1");
1000                         Assert.AreEqual (6, ret [1], "#2");
1001                         Assert.AreEqual (7, ret [2], "#3");
1002                 }
1003
1004                 [Test]
1005                 public void DeserializeMixedArray ()
1006                 {
1007                         object [] ret = (object []) Deserialize ("[5,\"6\",false]", typeof (object []));
1008                         Assert.AreEqual (5, ret [0], "#1");
1009                         Assert.AreEqual ("6", ret [1], "#2");
1010                         Assert.AreEqual (false, ret [2], "#3");
1011                 }
1012
1013                 [Test]
1014                 [ExpectedException (typeof (SerializationException))]
1015                 public void DeserializeEmptyAsString ()
1016                 {
1017                         // it somehow expects "root" which should have been already consumed.
1018                         Deserialize ("", typeof (string));
1019                 }
1020
1021                 [Test]
1022                 [ExpectedException (typeof (SerializationException))]
1023                 public void DeserializeEmptyAsInt ()
1024                 {
1025                         // it somehow expects "root" which should have been already consumed.
1026                         Deserialize ("", typeof (int));
1027                 }
1028
1029                 [Test]
1030                 [ExpectedException (typeof (SerializationException))]
1031                 public void DeserializeEmptyAsDBNull ()
1032                 {
1033                         // it somehow expects "root" which should have been already consumed.
1034                         Deserialize ("", typeof (DBNull));
1035                 }
1036
1037                 [Test]
1038                 public void DeserializeEmptyObjectAsString ()
1039                 {
1040                         // looks like it is converted to ""
1041                         Assert.AreEqual (String.Empty, Deserialize ("{}", typeof (string)));
1042                 }
1043
1044                 [Test]
1045                 [ExpectedException (typeof (SerializationException))]
1046                 public void DeserializeEmptyObjectAsInt ()
1047                 {
1048                         Deserialize ("{}", typeof (int));
1049                 }
1050
1051                 [Test]
1052                 public void DeserializeEmptyObjectAsDBNull ()
1053                 {
1054                         Assert.AreEqual (DBNull.Value, Deserialize ("{}", typeof (DBNull)));
1055                 }
1056
1057                 [Test]
1058                 [ExpectedException (typeof (SerializationException))]
1059                 public void DeserializeEnumByName ()
1060                 {
1061                         // enum is parsed into long
1062                         Deserialize (@"""Red""", typeof (Colors));
1063                 }
1064
1065                 [Test]
1066                 public void DeserializeEnum2 ()
1067                 {
1068                         object o = Deserialize ("0", typeof (Colors));
1069
1070                         Assert.AreEqual (typeof (Colors), o.GetType (), "#de3");
1071                         Colors c = (Colors) o;
1072                         Assert.AreEqual (Colors.Red, c, "#de4");
1073                 }
1074                 
1075                 [Test]
1076                 [ExpectedException (typeof (SerializationException))]
1077                 public void DeserializeEnumInvalid ()
1078                 {
1079                         Deserialize ("", typeof (Colors));
1080                 }
1081
1082                 [Test]
1083                 [ExpectedException (typeof (SerializationException))]
1084                 [Category ("NotDotNet")] // 0.0 is an invalid Colors value.
1085                 public void DeserializeEnumInvalid3 ()
1086                 {
1087                         //"0.0" instead of "0"
1088                         Deserialize (
1089                                 "0.0",
1090                                 typeof (Colors));
1091                 }
1092
1093                 [Test]
1094                 public void DeserializeEnumWithDC ()
1095                 {
1096                         object o = Deserialize ("0", typeof (ColorsWithDC));
1097                         
1098                         Assert.AreEqual (typeof (ColorsWithDC), o.GetType (), "#de5");
1099                         ColorsWithDC cdc = (ColorsWithDC) o;
1100                         Assert.AreEqual (ColorsWithDC.Red, o, "#de6");
1101                 }
1102
1103                 [Test]
1104                 [ExpectedException (typeof (SerializationException))]
1105                 [Category ("NotDotNet")] // 4 is an invalid Colors value.
1106                 [Category ("NotWorking")]
1107                 public void DeserializeEnumWithDCInvalid ()
1108                 {
1109                         Deserialize (
1110                                 "4",
1111                                 typeof (ColorsWithDC));
1112                 }
1113
1114                 [Test]
1115                 public void DeserializeDCWithEnum ()
1116                 {
1117                         object o = Deserialize (
1118                                 "{\"_colors\":0}",
1119                                 typeof (DCWithEnum));
1120
1121                         Assert.AreEqual (typeof (DCWithEnum), o.GetType (), "#de7");
1122                         DCWithEnum dc = (DCWithEnum) o;
1123                         Assert.AreEqual (Colors.Red, dc.colors, "#de8");
1124                 }
1125
1126                 [Test]
1127                 public void ReadObjectVerifyObjectNameFalse ()
1128                 {
1129                         string xml = @"<any><Member1>bar</Member1></any>";
1130                         object o = new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
1131                                 .ReadObject (XmlReader.Create (new StringReader (xml)), false);
1132                         Assert.IsTrue (o is VerifyObjectNameTestData, "#1");
1133
1134                         string xml2 = @"<any><x:Member1 xmlns:x=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">bar</x:Member1></any>";
1135                         o = new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
1136                                 .ReadObject (XmlReader.Create (new StringReader (xml2)), false);
1137                         Assert.IsTrue (o is VerifyObjectNameTestData, "#2");
1138                 }
1139
1140                 [Test]
1141                 [ExpectedException (typeof (SerializationException))]
1142                 public void ReadObjectVerifyObjectNameTrue ()
1143                 {
1144                         string xml = @"<any><Member1>bar</Member1></any>";
1145                         new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
1146                                 .ReadObject (XmlReader.Create (new StringReader (xml)), true);
1147                 }
1148
1149                 [Test] // member name is out of scope
1150                 public void ReadObjectVerifyObjectNameTrue2 ()
1151                 {
1152                         string xml = @"<root><Member2>bar</Member2></root>";
1153                         new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
1154                                 .ReadObject (XmlReader.Create (new StringReader (xml)), true);
1155                 }
1156
1157                 [Test]
1158                 public void ReadTypedObjectJson ()
1159                 {
1160                         object o = Deserialize (@"{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0}", typeof (DCWithEnum));
1161                         Assert.AreEqual (typeof (DCWithEnum), o.GetType ());
1162                 }
1163
1164                 [Test]
1165                 public void ReadObjectDCArrayJson ()
1166                 {
1167                         object o = Deserialize (@"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0}]",
1168                                 typeof (object []), typeof (DCWithEnum));
1169                         Assert.AreEqual (typeof (object []), o.GetType (), "#1");
1170                         object [] arr = (object []) o;
1171                         Assert.AreEqual (typeof (DCWithEnum), arr [0].GetType (), "#2");
1172                 }
1173
1174                 [Test]
1175                 public void ReadObjectDCArray2Json ()
1176                 {
1177                         object o = Deserialize (@"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0},{""__type"":""DCSimple1:#MonoTests.System.Runtime.Serialization.Json"",""Foo"":""hello""}]",
1178                                 typeof (object []), typeof (DCWithEnum), typeof (DCSimple1));
1179                         Assert.AreEqual (typeof (object []), o.GetType (), "#1");
1180                         object [] arr = (object []) o;
1181                         Assert.AreEqual (typeof (DCWithEnum), arr [0].GetType (), "#2");
1182                         Assert.AreEqual (typeof (DCSimple1), arr [1].GetType (), "#3");
1183                 }
1184
1185                 private object Deserialize (string xml, Type type, params Type [] knownTypes)
1186                 {
1187                         DataContractJsonSerializer ser = new DataContractJsonSerializer (type, knownTypes);
1188                         XmlReader xr = JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (xml), new XmlDictionaryReaderQuotas ());
1189                         return ser.ReadObject (xr);
1190                 }
1191
1192                 [Test]
1193                 public void IsStartObject ()
1194                 {
1195                         DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCSimple1));
1196                         Assert.IsTrue (s.IsStartObject (XmlReader.Create (new StringReader ("<root></root>"))), "#1");
1197                         Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<dummy></dummy>"))), "#2");
1198                         Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<Foo></Foo>"))), "#3");
1199                         Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<root xmlns='urn:foo'></root>"))), "#4");
1200                 }
1201
1202                 [Test]
1203                 public void SerializeNonDC2 ()
1204                 {
1205                         var ser = new DataContractJsonSerializer (typeof (TestData));
1206                         StringWriter sw = new StringWriter ();
1207                         var obj = new TestData () { Foo = "foo", Bar = "bar", Baz = "baz" };
1208
1209                         // XML
1210                         using (var xw = XmlWriter.Create (sw))
1211                                 ser.WriteObject (xw, obj);
1212                         var s = sw.ToString ();
1213                         // since the order is not preserved, we compare only contents.
1214                         Assert.IsTrue (s.IndexOf ("<Foo>foo</Foo>") > 0, "#1-1");
1215                         Assert.IsTrue (s.IndexOf ("<Bar>bar</Bar>") > 0, "#1-2");
1216                         Assert.IsFalse (s.IndexOf ("<Baz>baz</Baz>") > 0, "#1-3");
1217
1218                         // JSON
1219                         MemoryStream ms = new MemoryStream ();
1220                         using (var xw = JsonReaderWriterFactory.CreateJsonWriter (ms))
1221                                 ser.WriteObject (ms, obj);
1222                         s = new StreamReader (new MemoryStream (ms.ToArray ())).ReadToEnd ().Replace ('"', '/');
1223                         // since the order is not preserved, we compare only contents.
1224                         Assert.IsTrue (s.IndexOf ("/Foo/:/foo/") > 0, "#2-1");
1225                         Assert.IsTrue (s.IndexOf ("/Bar/:/bar/") > 0, "#2-2");
1226                         Assert.IsFalse (s.IndexOf ("/Baz/:/baz/") > 0, "#2-3");
1227                 }
1228
1229                 [Test]
1230                 public void AlwaysEmitTypeInformation ()
1231                 {
1232                         var ms = new MemoryStream ();
1233                         var ds = new DataContractJsonSerializer (typeof (string), "root", null, 10, false, null, true);
1234                         ds.WriteObject (ms, "foobar");
1235                         var s = Encoding.UTF8.GetString (ms.ToArray ());
1236                         Assert.AreEqual ("\"foobar\"", s, "#1");
1237                 }
1238
1239                 [Test]
1240                 public void AlwaysEmitTypeInformation2 ()
1241                 {
1242                         var ms = new MemoryStream ();
1243                         var ds = new DataContractJsonSerializer (typeof (TestData), "root", null, 10, false, null, true);
1244                         ds.WriteObject (ms, new TestData () { Foo = "foo"});
1245                         var s = Encoding.UTF8.GetString (ms.ToArray ());
1246                         Assert.AreEqual (@"{""__type"":""TestData:#MonoTests.System.Runtime.Serialization.Json"",""Bar"":null,""Foo"":""foo""}", s, "#1");
1247                 }
1248
1249                 [Test]
1250                 public void AlwaysEmitTypeInformation3 ()
1251                 {
1252                         var ms = new MemoryStream ();
1253                         var ds = new DataContractJsonSerializer (typeof (TestData), "root", null, 10, false, null, false);
1254                         ds.WriteObject (ms, new TestData () { Foo = "foo"});
1255                         var s = Encoding.UTF8.GetString (ms.ToArray ());
1256                         Assert.AreEqual (@"{""Bar"":null,""Foo"":""foo""}", s, "#1");
1257                 }
1258
1259                 [Test]
1260                 public void TestNonpublicDeserialization ()
1261                 {
1262                         string s1= @"{""Bar"":""bar"", ""Foo"":""foo"", ""Baz"":""baz""}";
1263                         TestData o1 = ((TestData)(new DataContractJsonSerializer (typeof (TestData)).ReadObject (JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (s1), new XmlDictionaryReaderQuotas ()))));
1264
1265                         Assert.AreEqual (null, o1.Baz, "#1");
1266
1267                         string s2 = @"{""TestData"":[{""key"":""key1"",""value"":""value1""}]}";
1268                         KeyValueTestData o2 = ((KeyValueTestData)(new DataContractJsonSerializer (typeof (KeyValueTestData)).ReadObject (JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (s2), new XmlDictionaryReaderQuotas ()))));
1269
1270                         Assert.AreEqual (1, o2.TestData.Count, "#2");
1271                         Assert.AreEqual ("key1", o2.TestData[0].Key, "#3");
1272                         Assert.AreEqual ("value1", o2.TestData[0].Value, "#4");
1273                 }
1274
1275                 // [Test] use this case if you want to check lame silverlight parser behavior. Seealso #549756
1276                 public void QuotelessDeserialization ()
1277                 {
1278                         string s1 = @"{FooMember:""value""}";
1279                         var ds = new DataContractJsonSerializer (typeof (DCWithName));
1280                         ds.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (s1)));
1281
1282                         string s2 = @"{FooMember:"" \""{dummy:string}\""""}";
1283                         ds.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (s2)));
1284                 }
1285
1286                 [Test]
1287                 [Category ("NotWorking")]
1288                 public void TypeIsNotPartsOfKnownTypes ()
1289                 {
1290                         var dcs = new DataContractSerializer (typeof (string));
1291                         Assert.AreEqual (0, dcs.KnownTypes.Count, "KnownTypes #1");
1292                         var dcjs = new DataContractJsonSerializer (typeof (string));
1293                         Assert.AreEqual (0, dcjs.KnownTypes.Count, "KnownTypes #2");
1294                 }
1295
1296                 [Test]
1297                 public void ReadWriteNullObject ()
1298                 {
1299                         DataContractJsonSerializer dcjs = new DataContractJsonSerializer (typeof (string));
1300                         using (MemoryStream ms = new MemoryStream ()) {
1301                                 dcjs.WriteObject (ms, null);
1302                                 ms.Position = 0;
1303                                 using (StreamReader sr = new StreamReader (ms)) {
1304                                         string data = sr.ReadToEnd ();
1305                                         Assert.AreEqual ("null", data, "WriteObject(stream,null)");
1306
1307                                         ms.Position = 0;
1308                                         Assert.IsNull (dcjs.ReadObject (ms), "ReadObject(stream)");
1309                                 }
1310                         };
1311                 }
1312
1313                 object ReadWriteObject (Type type, object obj, string expected)
1314                 {
1315                         using (MemoryStream ms = new MemoryStream ()) {
1316                                 DataContractJsonSerializer dcjs = new DataContractJsonSerializer (type);
1317                                 dcjs.WriteObject (ms, obj);
1318                                 ms.Position = 0;
1319                                 using (StreamReader sr = new StreamReader (ms)) {
1320                                         Assert.AreEqual (expected, sr.ReadToEnd (), "WriteObject");
1321
1322                                         ms.Position = 0;
1323                                         return dcjs.ReadObject (ms);
1324                                 }
1325                         }
1326                 }
1327
1328                 [Test]
1329                 [Ignore ("Wrong test case. See bug #573691")]
1330                 public void ReadWriteObject_Single_SpecialCases ()
1331                 {
1332                         Assert.IsTrue (Single.IsNaN ((float) ReadWriteObject (typeof (float), Single.NaN, "NaN")));
1333                         Assert.IsTrue (Single.IsNegativeInfinity ((float) ReadWriteObject (typeof (float), Single.NegativeInfinity, "-INF")));
1334                         Assert.IsTrue (Single.IsPositiveInfinity ((float) ReadWriteObject (typeof (float), Single.PositiveInfinity, "INF")));
1335                 }
1336
1337                 [Test]
1338                 [Ignore ("Wrong test case. See bug #573691")]
1339                 public void ReadWriteObject_Double_SpecialCases ()
1340                 {
1341                         Assert.IsTrue (Double.IsNaN ((double) ReadWriteObject (typeof (double), Double.NaN, "NaN")));
1342                         Assert.IsTrue (Double.IsNegativeInfinity ((double) ReadWriteObject (typeof (double), Double.NegativeInfinity, "-INF")));
1343                         Assert.IsTrue (Double.IsPositiveInfinity ((double) ReadWriteObject (typeof (double), Double.PositiveInfinity, "INF")));
1344                 }
1345
1346                 [Test]
1347                 public void ReadWriteDateTime ()
1348                 {
1349                         var ms = new MemoryStream ();
1350                         DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (Query));
1351                         Query query = new Query () {
1352                                 StartDate = new DateTime (2010, 3, 4, 5, 6, 7),
1353                                 EndDate = new DateTime (2010, 4, 5, 6, 7, 8)
1354                                 };
1355                         serializer.WriteObject (ms, query);
1356                         Assert.AreEqual ("{\"StartDate\":\"\\/Date(1267679167000)\\/\",\"EndDate\":\"\\/Date(1270447628000)\\/\"}", Encoding.UTF8.GetString (ms.ToArray ()), "#1");
1357                         ms.Position = 0;
1358                         Console.WriteLine (new StreamReader (ms).ReadToEnd ());
1359                         ms.Position = 0;
1360                         var q = (Query) serializer.ReadObject(ms);
1361                         Assert.AreEqual (query.StartDate, q.StartDate, "#2");
1362                         Assert.AreEqual (query.EndDate, q.EndDate, "#3");
1363                 }
1364         }
1365
1366         public class TestData
1367         {
1368                 public string Foo { get; set; }
1369                 public string Bar { get; set; }
1370                 internal string Baz { get; set; }
1371         }
1372
1373         public enum Colors {
1374                 Red, Green, Blue
1375         }
1376
1377         [DataContract (Name = "_ColorsWithDC")]
1378         public enum ColorsWithDC {
1379
1380                 [EnumMember (Value = "_Red")]
1381                 Red, 
1382                 [EnumMember]
1383                 Green, 
1384                 Blue
1385         }
1386
1387
1388         public enum ColorsEnumMemberNoDC {
1389                 [EnumMember (Value = "_Red")]
1390                 Red, 
1391                 [EnumMember]
1392                 Green, 
1393                 Blue
1394         }
1395
1396         [DataContract]
1397         public class DCWithEnum {
1398                 [DataMember (Name = "_colors")]
1399                 public Colors colors;
1400         }
1401
1402         [DataContract]
1403         public class DCEmpty
1404         {
1405                 // serializer doesn't touch it.
1406                 public string Foo = "TEST";
1407         }
1408
1409         [DataContract]
1410         public class DCSimple1
1411         {
1412                 [DataMember]
1413                 public string Foo = "TEST";
1414         }
1415
1416         [DataContract]
1417         public class DCHasNonDC
1418         {
1419                 [DataMember]
1420                 public NonDC Hoge= new NonDC ();
1421         }
1422
1423         public class NonDC
1424         {
1425                 public string Whee = "whee!";
1426         }
1427
1428         [DataContract]
1429         public class DCHasSerializable
1430         {
1431                 [DataMember]
1432                 public SimpleSer1 Ser = new SimpleSer1 ();
1433         }
1434
1435         [DataContract (Name = "Foo")]
1436         public class DCWithName
1437         {
1438                 [DataMember (Name = "FooMember")]
1439                 public string DMWithName = "value";
1440         }
1441
1442         [DataContract (Name = "")]
1443         public class DCWithEmptyName
1444         {
1445                 [DataMember]
1446                 public string Foo;
1447         }
1448
1449         [DataContract (Name = null)]
1450         public class DCWithNullName
1451         {
1452                 [DataMember]
1453                 public string Foo;
1454         }
1455
1456         [DataContract (Namespace = "")]
1457         public class DCWithEmptyNamespace
1458         {
1459                 [DataMember]
1460                 public string Foo;
1461         }
1462
1463         [Serializable]
1464         public class SimpleSer1
1465         {
1466                 public string Doh = "doh!";
1467         }
1468
1469         public class Wrapper
1470         {
1471                 [DataContract]
1472                 public class DCWrapped
1473                 {
1474                 }
1475         }
1476
1477         [DataContract]
1478         public class CollectionContainer
1479         {
1480                 Collection<string> items = new Collection<string> ();
1481
1482                 [DataMember]
1483                 public Collection<string> Items {
1484                         get { return items; }
1485                 }
1486         }
1487
1488         [CollectionDataContract]
1489         public class DataCollection<T> : Collection<T>
1490         {
1491         }
1492
1493         [DataContract]
1494         public class DataCollectionContainer
1495         {
1496                 DataCollection<string> items = new DataCollection<string> ();
1497
1498                 [DataMember]
1499                 public DataCollection<string> Items {
1500                         get { return items; }
1501                 }
1502         }
1503
1504         [DataContract]
1505         class SerializeNonDCArrayType
1506         {
1507                 [DataMember]
1508                 public NonDCItem [] IPAddresses = new NonDCItem [0];
1509         }
1510
1511         public class NonDCItem
1512         {
1513                 public byte [] Data { get; set; }
1514         }
1515
1516         [DataContract]
1517         public class VerifyObjectNameTestData
1518         {
1519                 [DataMember]
1520                 string Member1 = "foo";
1521         }
1522
1523         [Serializable]
1524         public class KeyValueTestData {
1525                 public List<KeyValuePair<string,string>> TestData = new List<KeyValuePair<string,string>>();
1526         }
1527
1528         [DataContract] // bug #586169
1529         public class Query
1530         {
1531                 [DataMember (Order=1)]
1532                 public DateTime StartDate { get; set; }
1533                 [DataMember (Order=2)]
1534                 public DateTime EndDate { get; set; }
1535         }
1536 }
1537
1538 [DataContract]
1539 class GlobalSample1
1540 {
1541 }