JsonSerializationWriter: handle char values
[mono-project.git] / mcs / class / System.ServiceModel.Web / Test / System.Runtime.Serialization.Json / DataContractJsonSerializerTest.cs
blob6b2aab98c4c95a6ec9a844241ac6707be55a8a71
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
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.
32 // This test code contains tests for DataContractJsonSerializer, which is
33 // imported from DataContractSerializerTest.cs.
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;
47 namespace MonoTests.System.Runtime.Serialization.Json
49 [TestFixture]
50 public class DataContractJsonSerializerTest
52 static readonly XmlWriterSettings settings;
54 static DataContractJsonSerializerTest ()
56 settings = new XmlWriterSettings ();
57 settings.OmitXmlDeclaration = true;
60 [DataContract]
61 class Sample1
63 [DataMember]
64 public string Member1;
67 [Test]
68 [ExpectedException (typeof (ArgumentNullException))]
69 public void ConstructorTypeNull ()
71 new DataContractJsonSerializer (null);
74 [Test]
75 public void ConstructorKnownTypesNull ()
77 // null knownTypes is allowed.
78 new DataContractJsonSerializer (typeof (Sample1), (IEnumerable<Type>) null);
79 new DataContractJsonSerializer (typeof (Sample1), "Foo", null);
82 [Test]
83 [ExpectedException (typeof (ArgumentNullException))]
84 public void ConstructorNameNull ()
86 new DataContractJsonSerializer (typeof (Sample1), (string) null);
89 [Test]
90 [ExpectedException (typeof (ArgumentOutOfRangeException))]
91 public void ConstructorNegativeMaxObjects ()
93 new DataContractJsonSerializer (typeof (Sample1), "Sample1",
94 null, -1, false, null, false);
97 [Test]
98 public void ConstructorMisc ()
100 new DataContractJsonSerializer (typeof (GlobalSample1)).WriteObject (new MemoryStream (), new GlobalSample1 ());
103 [Test]
104 public void WriteObjectContent ()
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 ();
114 Assert.AreEqual ("<my-element>TEST STRING</my-element>",
115 sw.ToString ());
118 // int
120 [Test]
121 public void SerializeIntXml ()
123 StringWriter sw = new StringWriter ();
124 SerializeInt (XmlWriter.Create (sw, settings));
125 Assert.AreEqual (
126 @"<root type=""number"">1</root>",
127 sw.ToString ());
130 [Test]
131 public void SerializeIntJson ()
133 MemoryStream ms = new MemoryStream ();
134 SerializeInt (JsonReaderWriterFactory.CreateJsonWriter (ms));
135 Assert.AreEqual (
136 "1",
137 Encoding.UTF8.GetString (ms.ToArray ()));
140 void SerializeInt (XmlWriter writer)
142 DataContractJsonSerializer ser =
143 new DataContractJsonSerializer (typeof (int));
144 using (XmlWriter w = writer) {
145 ser.WriteObject (w, 1);
149 // int, with rootName
151 [Test]
152 public void SerializeIntXmlWithRootName ()
154 StringWriter sw = new StringWriter ();
155 SerializeIntWithRootName (XmlWriter.Create (sw, settings));
156 Assert.AreEqual (
157 @"<myroot type=""number"">1</myroot>",
158 sw.ToString ());
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 ()
168 MemoryStream ms = new MemoryStream ();
169 SerializeIntWithRootName (JsonReaderWriterFactory.CreateJsonWriter (ms));
170 Assert.AreEqual (
171 "1",
172 Encoding.UTF8.GetString (ms.ToArray ()));
175 void SerializeIntWithRootName (XmlWriter writer)
177 DataContractJsonSerializer ser =
178 new DataContractJsonSerializer (typeof (int), "myroot");
179 using (XmlWriter w = writer) {
180 ser.WriteObject (w, 1);
184 // pass typeof(DCEmpty), serialize int
186 [Test]
187 public void SerializeIntForDCEmptyXml ()
189 StringWriter sw = new StringWriter ();
190 SerializeIntForDCEmpty (XmlWriter.Create (sw, settings));
191 Assert.AreEqual (
192 @"<root type=""number"">1</root>",
193 sw.ToString ());
196 [Test]
197 public void SerializeIntForDCEmptyJson ()
199 MemoryStream ms = new MemoryStream ();
200 SerializeIntForDCEmpty (JsonReaderWriterFactory.CreateJsonWriter (ms));
201 Assert.AreEqual (
202 "1",
203 Encoding.UTF8.GetString (ms.ToArray ()));
206 void SerializeIntForDCEmpty (XmlWriter writer)
208 DataContractJsonSerializer ser =
209 new DataContractJsonSerializer (typeof (DCEmpty));
210 using (XmlWriter w = writer) {
211 ser.WriteObject (w, 1);
215 // DCEmpty
217 [Test]
218 public void SerializeEmptyClassXml ()
220 StringWriter sw = new StringWriter ();
221 SerializeEmptyClass (XmlWriter.Create (sw, settings));
222 Assert.AreEqual (
223 @"<root type=""object"" />",
224 sw.ToString ());
227 [Test]
228 public void SerializeEmptyClassJson ()
230 MemoryStream ms = new MemoryStream ();
231 SerializeEmptyClass (JsonReaderWriterFactory.CreateJsonWriter (ms));
232 Assert.AreEqual (
233 "{}",
234 Encoding.UTF8.GetString (ms.ToArray ()));
237 void SerializeEmptyClass (XmlWriter writer)
239 DataContractJsonSerializer ser =
240 new DataContractJsonSerializer (typeof (DCEmpty));
241 using (XmlWriter w = writer) {
242 ser.WriteObject (w, new DCEmpty ());
246 // string (primitive)
248 [Test]
249 public void SerializePrimitiveStringXml ()
251 StringWriter sw = new StringWriter ();
252 SerializePrimitiveString (XmlWriter.Create (sw, settings));
253 Assert.AreEqual (
254 "<root>TEST</root>",
255 sw.ToString ());
258 [Test]
259 public void SerializePrimitiveStringJson ()
261 MemoryStream ms = new MemoryStream ();
262 SerializePrimitiveString (JsonReaderWriterFactory.CreateJsonWriter (ms));
263 Assert.AreEqual (
264 @"""TEST""",
265 Encoding.UTF8.GetString (ms.ToArray ()));
268 void SerializePrimitiveString (XmlWriter writer)
270 XmlObjectSerializer ser =
271 new DataContractJsonSerializer (typeof (string));
272 using (XmlWriter w = writer) {
273 ser.WriteObject (w, "TEST");
277 // QName (primitive but ...)
279 [Test]
280 public void SerializePrimitiveQNameXml ()
282 StringWriter sw = new StringWriter ();
283 SerializePrimitiveQName (XmlWriter.Create (sw, settings));
284 Assert.AreEqual (
285 "<root>foo:urn:foo</root>",
286 sw.ToString ());
289 [Test]
290 public void SerializePrimitiveQNameJson ()
292 MemoryStream ms = new MemoryStream ();
293 SerializePrimitiveQName (JsonReaderWriterFactory.CreateJsonWriter (ms));
294 Assert.AreEqual (
295 @"""foo:urn:foo""",
296 Encoding.UTF8.GetString (ms.ToArray ()));
299 void SerializePrimitiveQName (XmlWriter writer)
301 XmlObjectSerializer ser =
302 new DataContractJsonSerializer (typeof (XmlQualifiedName));
303 using (XmlWriter w = writer) {
304 ser.WriteObject (w, new XmlQualifiedName ("foo", "urn:foo"));
308 // DBNull (primitive)
310 [Test]
311 public void SerializeDBNullXml ()
313 StringWriter sw = new StringWriter ();
314 SerializeDBNull (XmlWriter.Create (sw, settings));
315 Assert.AreEqual (
316 @"<root type=""object"" />",
317 sw.ToString ());
320 [Test]
321 public void SerializeDBNullJson ()
323 MemoryStream ms = new MemoryStream ();
324 SerializeDBNull (JsonReaderWriterFactory.CreateJsonWriter (ms));
325 Assert.AreEqual (
326 "{}",
327 Encoding.UTF8.GetString (ms.ToArray ()));
330 void SerializeDBNull (XmlWriter writer)
332 DataContractJsonSerializer ser =
333 new DataContractJsonSerializer (typeof (DBNull));
334 using (XmlWriter w = writer) {
335 ser.WriteObject (w, DBNull.Value);
339 // DCSimple1
341 [Test]
342 public void SerializeSimpleClass1Xml ()
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 ());
351 [Test]
352 public void SerializeSimpleClass1Json ()
354 MemoryStream ms = new MemoryStream ();
355 SerializeSimpleClass1 (JsonReaderWriterFactory.CreateJsonWriter (ms));
356 Assert.AreEqual (
357 @"{""Foo"":""TEST""}",
358 Encoding.UTF8.GetString (ms.ToArray ()));
361 void SerializeSimpleClass1 (XmlWriter writer)
363 DataContractJsonSerializer ser =
364 new DataContractJsonSerializer (typeof (DCSimple1));
365 using (XmlWriter w = writer) {
366 ser.WriteObject (w, new DCSimple1 ());
370 // NonDC
372 [Test]
373 // NonDC is not a DataContract type.
374 public void SerializeNonDCOnlyCtor ()
376 DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (NonDC));
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 ()
385 DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (NonDC));
386 using (XmlWriter w = XmlWriter.Create (TextWriter.Null, settings)) {
387 ser.WriteObject (w, new NonDC ());
391 // DCHasNonDC
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 ()
400 DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCHasNonDC));
401 using (XmlWriter w = XmlWriter.Create (TextWriter.Null, settings)) {
402 ser.WriteObject (w, new DCHasNonDC ());
406 // DCHasSerializable
408 [Test]
409 public void SerializeSimpleSerializable1Xml ()
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 ());
418 [Test]
419 public void SerializeSimpleSerializable1Json ()
421 MemoryStream ms = new MemoryStream ();
422 SerializeSimpleSerializable1 (JsonReaderWriterFactory.CreateJsonWriter (ms));
423 Assert.AreEqual (
424 @"{""Ser"":{""Doh"":""doh!""}}",
425 Encoding.UTF8.GetString (ms.ToArray ()));
428 // DCHasSerializable itself is DataContract and has a field
429 // whose type is not contract but serializable.
430 void SerializeSimpleSerializable1 (XmlWriter writer)
432 DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCHasSerializable));
433 using (XmlWriter w = writer) {
434 ser.WriteObject (w, new DCHasSerializable ());
438 [Test]
439 public void SerializeDCWithNameXml ()
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 ());
448 [Test]
449 public void SerializeDCWithNameJson ()
451 MemoryStream ms = new MemoryStream ();
452 SerializeDCWithName (JsonReaderWriterFactory.CreateJsonWriter (ms));
453 Assert.AreEqual (
454 @"{""FooMember"":""value""}",
455 Encoding.UTF8.GetString (ms.ToArray ()));
458 void SerializeDCWithName (XmlWriter writer)
460 DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithName));
461 using (XmlWriter w = writer) {
462 ser.WriteObject (w, new DCWithName ());
466 [Test]
467 public void SerializeDCWithEmptyName1 ()
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;
479 Assert.Fail ("Expected InvalidDataContractException");
482 [Test]
483 public void SerializeDCWithEmptyName2 ()
485 DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithName));
486 StringWriter sw = new StringWriter ();
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;
497 Assert.Fail ("Expected InvalidDataContractException");
500 [Test]
501 [Category("NotWorking")]
502 public void SerializeDCWithNullName ()
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;
514 Assert.Fail ("Expected InvalidDataContractException");
517 [Test]
518 public void SerializeDCWithEmptyNamespace1 ()
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 ());
527 [Test]
528 public void SerializeWrappedClassXml ()
530 StringWriter sw = new StringWriter ();
531 SerializeWrappedClass (XmlWriter.Create (sw, settings));
532 Assert.AreEqual (
533 @"<root type=""object"" />",
534 sw.ToString ());
537 [Test]
538 public void SerializeWrappedClassJson ()
540 MemoryStream ms = new MemoryStream ();
541 SerializeWrappedClass (JsonReaderWriterFactory.CreateJsonWriter (ms));
542 Assert.AreEqual (
543 "{}",
544 Encoding.UTF8.GetString (ms.ToArray ()));
547 void SerializeWrappedClass (XmlWriter writer)
549 DataContractJsonSerializer ser =
550 new DataContractJsonSerializer (typeof (Wrapper.DCWrapped));
551 using (XmlWriter w = writer) {
552 ser.WriteObject (w, new Wrapper.DCWrapped ());
556 // CollectionContainer : Items must have a setter. (but became valid in RTM).
557 [Test]
558 public void SerializeReadOnlyCollectionMember ()
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);
568 // DataCollectionContainer : Items must have a setter. (but became valid in RTM).
569 [Test]
570 public void SerializeReadOnlyDataCollectionMember ()
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);
580 [Test]
581 [Ignore ("https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=409970")]
582 [ExpectedException (typeof (SerializationException))]
583 public void DeserializeReadOnlyDataCollection_NullCollection ()
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);
598 [Test]
599 public void SerializeGuidXml ()
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 ());
608 [Test]
609 public void SerializeGuidJson ()
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 ()));
618 void SerializeGuid (XmlWriter writer)
620 DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Guid));
621 using (XmlWriter w = writer) {
622 ser.WriteObject (w, Guid.Empty);
626 [Test]
627 public void SerializeEnumXml ()
629 StringWriter sw = new StringWriter ();
630 SerializeEnum (XmlWriter.Create (sw, settings));
631 Assert.AreEqual (
632 @"<root type=""number"">0</root>",
633 sw.ToString ());
636 [Test]
637 public void SerializeEnumJson ()
639 MemoryStream ms = new MemoryStream ();
640 SerializeEnum (JsonReaderWriterFactory.CreateJsonWriter (ms));
641 Assert.AreEqual (
642 "0",
643 Encoding.UTF8.GetString (ms.ToArray ()));
646 void SerializeEnum (XmlWriter writer)
648 DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Colors));
649 using (XmlWriter w = writer) {
650 ser.WriteObject (w, new Colors ());
654 [Test]
655 public void SerializeEnum2Xml ()
657 StringWriter sw = new StringWriter ();
658 SerializeEnum2 (XmlWriter.Create (sw, settings));
659 Assert.AreEqual (
660 @"<root type=""number"">0</root>",
661 sw.ToString ());
664 [Test]
665 public void SerializeEnum2Json ()
667 MemoryStream ms = new MemoryStream ();
668 SerializeEnum2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
669 Assert.AreEqual (
670 "0",
671 Encoding.UTF8.GetString (ms.ToArray ()));
674 void SerializeEnum2 (XmlWriter writer)
676 DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Colors));
677 using (XmlWriter w = writer) {
678 ser.WriteObject (w, 0);
682 [Test] // so, DataContract does not affect here.
683 public void SerializeEnumWithDCXml ()
685 StringWriter sw = new StringWriter ();
686 SerializeEnumWithDC (XmlWriter.Create (sw, settings));
687 Assert.AreEqual (
688 @"<root type=""number"">0</root>",
689 sw.ToString ());
692 [Test]
693 public void SerializeEnumWithDCJson ()
695 MemoryStream ms = new MemoryStream ();
696 SerializeEnumWithDC (JsonReaderWriterFactory.CreateJsonWriter (ms));
697 Assert.AreEqual (
698 "0",
699 Encoding.UTF8.GetString (ms.ToArray ()));
702 void SerializeEnumWithDC (XmlWriter writer)
704 DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
705 using (XmlWriter w = writer) {
706 ser.WriteObject (w, new ColorsWithDC ());
710 [Test]
711 public void SerializeEnumWithNoDCXml ()
713 StringWriter sw = new StringWriter ();
714 SerializeEnumWithNoDC (XmlWriter.Create (sw, settings));
715 Assert.AreEqual (
716 @"<root type=""number"">0</root>",
717 sw.ToString ());
720 [Test]
721 public void SerializeEnumWithNoDCJson ()
723 MemoryStream ms = new MemoryStream ();
724 SerializeEnumWithNoDC (JsonReaderWriterFactory.CreateJsonWriter (ms));
725 Assert.AreEqual (
726 "0",
727 Encoding.UTF8.GetString (ms.ToArray ()));
730 void SerializeEnumWithNoDC (XmlWriter writer)
732 DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsEnumMemberNoDC));
733 using (XmlWriter w = writer) {
734 ser.WriteObject (w, new ColorsEnumMemberNoDC ());
738 [Test]
739 public void SerializeEnumWithDC2Xml ()
741 StringWriter sw = new StringWriter ();
742 SerializeEnumWithDC2 (XmlWriter.Create (sw, settings));
743 Assert.AreEqual (
744 @"<root type=""number"">3</root>",
745 sw.ToString ());
748 [Test]
749 public void SerializeEnumWithDC2Json ()
751 MemoryStream ms = new MemoryStream ();
752 SerializeEnumWithDC2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
753 Assert.AreEqual (
754 "3",
755 Encoding.UTF8.GetString (ms.ToArray ()));
758 void SerializeEnumWithDC2 (XmlWriter writer)
760 DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
761 using (XmlWriter w = writer) {
762 ser.WriteObject (w, 3);
767 [Test]
768 [ExpectedException (typeof (SerializationException))]
769 public void SerializeEnumWithDCInvalid ()
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);
780 [Test]
781 public void SerializeDCWithEnumXml ()
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 ());
790 [Test]
791 public void SerializeDCWithEnumJson ()
793 MemoryStream ms = new MemoryStream ();
794 SerializeDCWithEnum (JsonReaderWriterFactory.CreateJsonWriter (ms));
795 Assert.AreEqual (
796 @"{""_colors"":0}",
797 Encoding.UTF8.GetString (ms.ToArray ()));
800 void SerializeDCWithEnum (XmlWriter writer)
802 DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEnum));
803 using (XmlWriter w = writer) {
804 ser.WriteObject (w, new DCWithEnum ());
808 [Test]
809 public void SerializerDCArrayXml ()
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 ());
818 [Test]
819 public void SerializerDCArrayJson ()
821 MemoryStream ms = new MemoryStream ();
822 SerializerDCArray (JsonReaderWriterFactory.CreateJsonWriter (ms));
823 Assert.AreEqual (
824 @"[{""_colors"":0},{""_colors"":1}]",
825 Encoding.UTF8.GetString (ms.ToArray ()));
828 void SerializerDCArray (XmlWriter writer)
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);
839 [Test]
840 public void SerializerDCArray2Xml ()
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 ());
849 [Test]
850 public void SerializerDCArray2Json ()
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 ()));
859 void SerializerDCArray2 (XmlWriter writer)
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";
869 using (XmlWriter w = writer) {
870 ser.WriteObject (w, arr);
874 [Test]
875 public void SerializerDCArray3Xml ()
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;
882 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
883 ser.WriteObject (w, arr);
886 Assert.AreEqual (
887 @"<root type=""array""><item type=""number"">1</item><item type=""number"">2</item></root>",
888 sw.ToString ());
891 [Test]
892 public void SerializerDCArray3Json ()
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;
899 using (XmlWriter w = JsonReaderWriterFactory.CreateJsonWriter (ms)) {
900 ser.WriteObject (w, arr);
903 Assert.AreEqual (
904 @"[1,2]",
905 Encoding.UTF8.GetString (ms.ToArray ()));
908 [Test]
909 // ... so, non-JSON XmlWriter is still accepted.
910 public void SerializeNonDCArrayXml ()
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 ());
917 Assert.AreEqual (@"<root type=""object""><IPAddresses type=""array"" /></root>",
918 sw.ToString ());
921 [Test]
922 public void SerializeNonDCArrayJson ()
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 ());
929 Assert.AreEqual (@"{""IPAddresses"":[]}",
930 Encoding.UTF8.GetString (ms.ToArray ()));
933 [Test]
934 public void SerializeNonDCArrayItems ()
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);
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");
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");
957 [Test]
958 public void MaxItemsInObjectGraph1 ()
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 ());
965 [Test]
966 [ExpectedException (typeof (SerializationException))]
967 public void MaxItemsInObjectGraph2 ()
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 ());
974 [Test]
975 public void DeserializeString ()
977 Assert.AreEqual ("ABC", Deserialize ("\"ABC\"", typeof (string)));
980 [Test]
981 public void DeserializeInt ()
983 Assert.AreEqual (5, Deserialize ("5", typeof (int)));
986 [Test]
987 public void DeserializeArray ()
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");
995 [Test]
996 public void DeserializeArrayUntyped ()
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");
1004 [Test]
1005 public void DeserializeMixedArray ()
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");
1013 [Test]
1014 [ExpectedException (typeof (SerializationException))]
1015 public void DeserializeEmptyAsString ()
1017 // it somehow expects "root" which should have been already consumed.
1018 Deserialize ("", typeof (string));
1021 [Test]
1022 [ExpectedException (typeof (SerializationException))]
1023 public void DeserializeEmptyAsInt ()
1025 // it somehow expects "root" which should have been already consumed.
1026 Deserialize ("", typeof (int));
1029 [Test]
1030 [ExpectedException (typeof (SerializationException))]
1031 public void DeserializeEmptyAsDBNull ()
1033 // it somehow expects "root" which should have been already consumed.
1034 Deserialize ("", typeof (DBNull));
1037 [Test]
1038 public void DeserializeEmptyObjectAsString ()
1040 // looks like it is converted to ""
1041 Assert.AreEqual (String.Empty, Deserialize ("{}", typeof (string)));
1044 [Test]
1045 [ExpectedException (typeof (SerializationException))]
1046 public void DeserializeEmptyObjectAsInt ()
1048 Deserialize ("{}", typeof (int));
1051 [Test]
1052 public void DeserializeEmptyObjectAsDBNull ()
1054 Assert.AreEqual (DBNull.Value, Deserialize ("{}", typeof (DBNull)));
1057 [Test]
1058 [ExpectedException (typeof (SerializationException))]
1059 public void DeserializeEnumByName ()
1061 // enum is parsed into long
1062 Deserialize (@"""Red""", typeof (Colors));
1065 [Test]
1066 public void DeserializeEnum2 ()
1068 object o = Deserialize ("0", typeof (Colors));
1070 Assert.AreEqual (typeof (Colors), o.GetType (), "#de3");
1071 Colors c = (Colors) o;
1072 Assert.AreEqual (Colors.Red, c, "#de4");
1075 [Test]
1076 [ExpectedException (typeof (SerializationException))]
1077 public void DeserializeEnumInvalid ()
1079 Deserialize ("", typeof (Colors));
1082 [Test]
1083 [ExpectedException (typeof (SerializationException))]
1084 [Category ("NotDotNet")] // 0.0 is an invalid Colors value.
1085 public void DeserializeEnumInvalid3 ()
1087 //"0.0" instead of "0"
1088 Deserialize (
1089 "0.0",
1090 typeof (Colors));
1093 [Test]
1094 public void DeserializeEnumWithDC ()
1096 object o = Deserialize ("0", typeof (ColorsWithDC));
1098 Assert.AreEqual (typeof (ColorsWithDC), o.GetType (), "#de5");
1099 ColorsWithDC cdc = (ColorsWithDC) o;
1100 Assert.AreEqual (ColorsWithDC.Red, o, "#de6");
1103 [Test]
1104 [ExpectedException (typeof (SerializationException))]
1105 [Category ("NotDotNet")] // 4 is an invalid Colors value.
1106 [Category ("NotWorking")]
1107 public void DeserializeEnumWithDCInvalid ()
1109 Deserialize (
1110 "4",
1111 typeof (ColorsWithDC));
1114 [Test]
1115 public void DeserializeDCWithEnum ()
1117 object o = Deserialize (
1118 "{\"_colors\":0}",
1119 typeof (DCWithEnum));
1121 Assert.AreEqual (typeof (DCWithEnum), o.GetType (), "#de7");
1122 DCWithEnum dc = (DCWithEnum) o;
1123 Assert.AreEqual (Colors.Red, dc.colors, "#de8");
1126 [Test]
1127 public void ReadObjectVerifyObjectNameFalse ()
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");
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");
1140 [Test]
1141 [ExpectedException (typeof (SerializationException))]
1142 public void ReadObjectVerifyObjectNameTrue ()
1144 string xml = @"<any><Member1>bar</Member1></any>";
1145 new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
1146 .ReadObject (XmlReader.Create (new StringReader (xml)), true);
1149 [Test] // member name is out of scope
1150 public void ReadObjectVerifyObjectNameTrue2 ()
1152 string xml = @"<root><Member2>bar</Member2></root>";
1153 new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
1154 .ReadObject (XmlReader.Create (new StringReader (xml)), true);
1157 [Test]
1158 public void ReadTypedObjectJson ()
1160 object o = Deserialize (@"{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0}", typeof (DCWithEnum));
1161 Assert.AreEqual (typeof (DCWithEnum), o.GetType ());
1164 [Test]
1165 public void ReadObjectDCArrayJson ()
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");
1174 [Test]
1175 public void ReadObjectDCArray2Json ()
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");
1185 private object Deserialize (string xml, Type type, params Type [] knownTypes)
1187 DataContractJsonSerializer ser = new DataContractJsonSerializer (type, knownTypes);
1188 XmlReader xr = JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (xml), new XmlDictionaryReaderQuotas ());
1189 return ser.ReadObject (xr);
1192 [Test]
1193 public void IsStartObject ()
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");
1202 [Test]
1203 public void SerializeNonDC2 ()
1205 var ser = new DataContractJsonSerializer (typeof (TestData));
1206 StringWriter sw = new StringWriter ();
1207 var obj = new TestData () { Foo = "foo", Bar = "bar", Baz = "baz" };
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");
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");
1229 [Test]
1230 public void AlwaysEmitTypeInformation ()
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");
1239 [Test]
1240 public void AlwaysEmitTypeInformation2 ()
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");
1249 [Test]
1250 public void AlwaysEmitTypeInformation3 ()
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");
1259 [Test]
1260 public void TestNonpublicDeserialization ()
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 ()))));
1265 Assert.AreEqual (null, o1.Baz, "#1");
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 ()))));
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");
1275 // [Test] use this case if you want to check lame silverlight parser behavior. Seealso #549756
1276 public void QuotelessDeserialization ()
1278 string s1 = @"{FooMember:""value""}";
1279 var ds = new DataContractJsonSerializer (typeof (DCWithName));
1280 ds.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (s1)));
1282 string s2 = @"{FooMember:"" \""{dummy:string}\""""}";
1283 ds.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (s2)));
1286 [Test]
1287 [Category ("NotWorking")]
1288 public void TypeIsNotPartsOfKnownTypes ()
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");
1296 [Test]
1297 public void ReadWriteNullObject ()
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)");
1307 ms.Position = 0;
1308 Assert.IsNull (dcjs.ReadObject (ms), "ReadObject(stream)");
1313 object ReadWriteObject (Type type, object obj, string expected)
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");
1322 ms.Position = 0;
1323 return dcjs.ReadObject (ms);
1328 [Test]
1329 [Ignore ("Wrong test case. See bug #573691")]
1330 public void ReadWriteObject_Single_SpecialCases ()
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")));
1337 [Test]
1338 [Ignore ("Wrong test case. See bug #573691")]
1339 public void ReadWriteObject_Double_SpecialCases ()
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")));
1346 [Test]
1347 public void ReadWriteDateTime ()
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)
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");
1365 [Test]
1366 public void DeserializeNullMember ()
1368 var ds = new DataContractJsonSerializer (typeof (ClassA));
1369 var stream = new MemoryStream ();
1370 var a = new ClassA ();
1371 ds.WriteObject (stream, a);
1372 stream.Position = 0;
1373 a = (ClassA) ds.ReadObject (stream);
1374 Assert.IsNull (a.B, "#1");
1377 [Test]
1378 public void OnDeserializationMethods ()
1380 var ds = new DataContractJsonSerializer (typeof (GSPlayerListErg));
1381 var obj = new GSPlayerListErg ();
1382 var ms = new MemoryStream ();
1383 ds.WriteObject (ms, obj);
1384 ms.Position = 0;
1385 ds.ReadObject (ms);
1386 Assert.IsTrue (GSPlayerListErg.A, "A");
1387 Assert.IsTrue (GSPlayerListErg.B, "B");
1388 Assert.IsTrue (GSPlayerListErg.C, "C");
1391 [Test]
1392 public void WriteChar ()
1394 DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof (CharTest));
1395 using (MemoryStream ms = new MemoryStream()) {
1396 serializer.WriteObject(ms, new CharTest ());
1397 ms.Position = 0L;
1398 using (StreamReader reader = new StreamReader(ms)) {
1399 reader.ReadToEnd();
1405 public class CharTest
1407 public char Foo;
1410 public class TestData
1412 public string Foo { get; set; }
1413 public string Bar { get; set; }
1414 internal string Baz { get; set; }
1417 public enum Colors {
1418 Red, Green, Blue
1421 [DataContract (Name = "_ColorsWithDC")]
1422 public enum ColorsWithDC {
1424 [EnumMember (Value = "_Red")]
1425 Red,
1426 [EnumMember]
1427 Green,
1428 Blue
1432 public enum ColorsEnumMemberNoDC {
1433 [EnumMember (Value = "_Red")]
1434 Red,
1435 [EnumMember]
1436 Green,
1437 Blue
1440 [DataContract]
1441 public class DCWithEnum {
1442 [DataMember (Name = "_colors")]
1443 public Colors colors;
1446 [DataContract]
1447 public class DCEmpty
1449 // serializer doesn't touch it.
1450 public string Foo = "TEST";
1453 [DataContract]
1454 public class DCSimple1
1456 [DataMember]
1457 public string Foo = "TEST";
1460 [DataContract]
1461 public class DCHasNonDC
1463 [DataMember]
1464 public NonDC Hoge= new NonDC ();
1467 public class NonDC
1469 public string Whee = "whee!";
1472 [DataContract]
1473 public class DCHasSerializable
1475 [DataMember]
1476 public SimpleSer1 Ser = new SimpleSer1 ();
1479 [DataContract (Name = "Foo")]
1480 public class DCWithName
1482 [DataMember (Name = "FooMember")]
1483 public string DMWithName = "value";
1486 [DataContract (Name = "")]
1487 public class DCWithEmptyName
1489 [DataMember]
1490 public string Foo;
1493 [DataContract (Name = null)]
1494 public class DCWithNullName
1496 [DataMember]
1497 public string Foo;
1500 [DataContract (Namespace = "")]
1501 public class DCWithEmptyNamespace
1503 [DataMember]
1504 public string Foo;
1507 [Serializable]
1508 public class SimpleSer1
1510 public string Doh = "doh!";
1513 public class Wrapper
1515 [DataContract]
1516 public class DCWrapped
1521 [DataContract]
1522 public class CollectionContainer
1524 Collection<string> items = new Collection<string> ();
1526 [DataMember]
1527 public Collection<string> Items {
1528 get { return items; }
1532 [CollectionDataContract]
1533 public class DataCollection<T> : Collection<T>
1537 [DataContract]
1538 public class DataCollectionContainer
1540 DataCollection<string> items = new DataCollection<string> ();
1542 [DataMember]
1543 public DataCollection<string> Items {
1544 get { return items; }
1548 [DataContract]
1549 class SerializeNonDCArrayType
1551 [DataMember]
1552 public NonDCItem [] IPAddresses = new NonDCItem [0];
1555 public class NonDCItem
1557 public byte [] Data { get; set; }
1560 [DataContract]
1561 public class VerifyObjectNameTestData
1563 [DataMember]
1564 string Member1 = "foo";
1567 [Serializable]
1568 public class KeyValueTestData {
1569 public List<KeyValuePair<string,string>> TestData = new List<KeyValuePair<string,string>>();
1572 [DataContract] // bug #586169
1573 public class Query
1575 [DataMember (Order=1)]
1576 public DateTime StartDate { get; set; }
1577 [DataMember (Order=2)]
1578 public DateTime EndDate { get; set; }
1581 public class ClassA {
1582 public ClassB B { get; set; }
1585 public class ClassB
1589 public class GSPlayerListErg
1591 public GSPlayerListErg ()
1593 Init ();
1596 void Init ()
1598 C = true;
1601 [OnDeserializing]
1602 public void OnDeserializing (StreamingContext c)
1604 A = true;
1605 Init ();
1608 [OnDeserialized]
1609 void OnDeserialized (StreamingContext c)
1611 B = true;
1614 public static bool A, B, C;
1616 [DataMember (Name = "T")]
1617 public long CodedServerTimeUTC { get; set; }
1618 public DateTime ServerTimeUTC { get; set; }
1622 [DataContract]
1623 class GlobalSample1