2 // DataContractJsonSerializerTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Ankit Jain <JAnkit@novell.com>
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
37 using System
.Collections
.Generic
;
38 using System
.Collections
.ObjectModel
;
41 using System
.Runtime
.Serialization
;
42 using System
.Runtime
.Serialization
.Json
;
45 using NUnit
.Framework
;
47 namespace MonoTests
.System
.Runtime
.Serialization
.Json
50 public class DataContractJsonSerializerTest
52 static readonly XmlWriterSettings settings
;
54 static DataContractJsonSerializerTest ()
56 settings
= new XmlWriterSettings ();
57 settings
.OmitXmlDeclaration
= true;
64 public string Member1
;
68 [ExpectedException (typeof (ArgumentNullException
))]
69 public void ConstructorTypeNull ()
71 new DataContractJsonSerializer (null);
75 public void ConstructorKnownTypesNull ()
77 // null knownTypes is allowed.
78 new DataContractJsonSerializer (typeof (Sample1
), (IEnumerable
<Type
>) null);
79 new DataContractJsonSerializer (typeof (Sample1
), "Foo", null);
83 [ExpectedException (typeof (ArgumentNullException
))]
84 public void ConstructorNameNull ()
86 new DataContractJsonSerializer (typeof (Sample1
), (string) null);
90 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
91 public void ConstructorNegativeMaxObjects ()
93 new DataContractJsonSerializer (typeof (Sample1
), "Sample1",
94 null, -1, false, null, false);
98 public void ConstructorMisc ()
100 new DataContractJsonSerializer (typeof (GlobalSample1
)).WriteObject (new MemoryStream (), new GlobalSample1 ());
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>",
121 public void SerializeIntXml ()
123 StringWriter sw
= new StringWriter ();
124 SerializeInt (XmlWriter
.Create (sw
, settings
));
126 @"<root type=""number"">1</root>",
131 public void SerializeIntJson ()
133 MemoryStream ms
= new MemoryStream ();
134 SerializeInt (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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
152 public void SerializeIntXmlWithRootName ()
154 StringWriter sw
= new StringWriter ();
155 SerializeIntWithRootName (XmlWriter
.Create (sw
, settings
));
157 @"<myroot type=""number"">1</myroot>",
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
));
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
187 public void SerializeIntForDCEmptyXml ()
189 StringWriter sw
= new StringWriter ();
190 SerializeIntForDCEmpty (XmlWriter
.Create (sw
, settings
));
192 @"<root type=""number"">1</root>",
197 public void SerializeIntForDCEmptyJson ()
199 MemoryStream ms
= new MemoryStream ();
200 SerializeIntForDCEmpty (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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);
218 public void SerializeEmptyClassXml ()
220 StringWriter sw
= new StringWriter ();
221 SerializeEmptyClass (XmlWriter
.Create (sw
, settings
));
223 @"<root type=""object"" />",
228 public void SerializeEmptyClassJson ()
230 MemoryStream ms
= new MemoryStream ();
231 SerializeEmptyClass (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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)
249 public void SerializePrimitiveStringXml ()
251 StringWriter sw
= new StringWriter ();
252 SerializePrimitiveString (XmlWriter
.Create (sw
, settings
));
259 public void SerializePrimitiveStringJson ()
261 MemoryStream ms
= new MemoryStream ();
262 SerializePrimitiveString (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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 ...)
280 public void SerializePrimitiveQNameXml ()
282 StringWriter sw
= new StringWriter ();
283 SerializePrimitiveQName (XmlWriter
.Create (sw
, settings
));
285 "<root>foo:urn:foo</root>",
290 public void SerializePrimitiveQNameJson ()
292 MemoryStream ms
= new MemoryStream ();
293 SerializePrimitiveQName (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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)
311 public void SerializeDBNullXml ()
313 StringWriter sw
= new StringWriter ();
314 SerializeDBNull (XmlWriter
.Create (sw
, settings
));
316 @"<root type=""object"" />",
321 public void SerializeDBNullJson ()
323 MemoryStream ms
= new MemoryStream ();
324 SerializeDBNull (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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
);
342 public void SerializeSimpleClass1Xml ()
344 StringWriter sw
= new StringWriter ();
345 SerializeSimpleClass1 (XmlWriter
.Create (sw
, settings
));
347 @"<root type=""object""><Foo>TEST</Foo></root>",
352 public void SerializeSimpleClass1Json ()
354 MemoryStream ms
= new MemoryStream ();
355 SerializeSimpleClass1 (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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 ());
373 // NonDC is not a DataContract type.
374 public void SerializeNonDCOnlyCtor ()
376 DataContractJsonSerializer ser
= new DataContractJsonSerializer (typeof (NonDC
));
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 ());
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 ());
409 public void SerializeSimpleSerializable1Xml ()
411 StringWriter sw
= new StringWriter ();
412 SerializeSimpleSerializable1 (XmlWriter
.Create (sw
, settings
));
414 @"<root type=""object""><Ser type=""object""><Doh>doh!</Doh></Ser></root>",
419 public void SerializeSimpleSerializable1Json ()
421 MemoryStream ms
= new MemoryStream ();
422 SerializeSimpleSerializable1 (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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 ());
439 public void SerializeDCWithNameXml ()
441 StringWriter sw
= new StringWriter ();
442 SerializeDCWithName (XmlWriter
.Create (sw
, settings
));
444 @"<root type=""object""><FooMember>value</FooMember></root>",
449 public void SerializeDCWithNameJson ()
451 MemoryStream ms
= new MemoryStream ();
452 SerializeDCWithName (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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 ());
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
)) {
474 ser
.WriteObject (w
, dc
);
475 } catch (InvalidDataContractException
) {
479 Assert
.Fail ("Expected InvalidDataContractException");
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
)) {
492 ser
.WriteObject (w
, dc
);
493 } catch (InvalidDataContractException
) {
497 Assert
.Fail ("Expected InvalidDataContractException");
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
)) {
508 /* DataContractAttribute.Name == "", not valid */
509 ser
.WriteObject (w
, new DCWithNullName ());
510 } catch (InvalidDataContractException
) {
514 Assert
.Fail ("Expected InvalidDataContractException");
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 ());
528 public void SerializeWrappedClassXml ()
530 StringWriter sw
= new StringWriter ();
531 SerializeWrappedClass (XmlWriter
.Create (sw
, settings
));
533 @"<root type=""object"" />",
538 public void SerializeWrappedClassJson ()
540 MemoryStream ms
= new MemoryStream ();
541 SerializeWrappedClass (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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).
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).
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);
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 ();
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
);
599 public void SerializeGuidXml ()
601 StringWriter sw
= new StringWriter ();
602 SerializeGuid (XmlWriter
.Create (sw
, settings
));
604 @"<root>00000000-0000-0000-0000-000000000000</root>",
609 public void SerializeGuidJson ()
611 MemoryStream ms
= new MemoryStream ();
612 SerializeGuid (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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
);
627 public void SerializeEnumXml ()
629 StringWriter sw
= new StringWriter ();
630 SerializeEnum (XmlWriter
.Create (sw
, settings
));
632 @"<root type=""number"">0</root>",
637 public void SerializeEnumJson ()
639 MemoryStream ms
= new MemoryStream ();
640 SerializeEnum (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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 ());
655 public void SerializeEnum2Xml ()
657 StringWriter sw
= new StringWriter ();
658 SerializeEnum2 (XmlWriter
.Create (sw
, settings
));
660 @"<root type=""number"">0</root>",
665 public void SerializeEnum2Json ()
667 MemoryStream ms
= new MemoryStream ();
668 SerializeEnum2 (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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
));
688 @"<root type=""number"">0</root>",
693 public void SerializeEnumWithDCJson ()
695 MemoryStream ms
= new MemoryStream ();
696 SerializeEnumWithDC (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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 ());
711 public void SerializeEnumWithNoDCXml ()
713 StringWriter sw
= new StringWriter ();
714 SerializeEnumWithNoDC (XmlWriter
.Create (sw
, settings
));
716 @"<root type=""number"">0</root>",
721 public void SerializeEnumWithNoDCJson ()
723 MemoryStream ms
= new MemoryStream ();
724 SerializeEnumWithNoDC (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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 ());
739 public void SerializeEnumWithDC2Xml ()
741 StringWriter sw
= new StringWriter ();
742 SerializeEnumWithDC2 (XmlWriter
.Create (sw
, settings
));
744 @"<root type=""number"">3</root>",
749 public void SerializeEnumWithDC2Json ()
751 MemoryStream ms
= new MemoryStream ();
752 SerializeEnumWithDC2 (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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);
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);
781 public void SerializeDCWithEnumXml ()
783 StringWriter sw
= new StringWriter ();
784 SerializeDCWithEnum (XmlWriter
.Create (sw
, settings
));
786 @"<root type=""object""><_colors type=""number"">0</_colors></root>",
791 public void SerializeDCWithEnumJson ()
793 MemoryStream ms
= new MemoryStream ();
794 SerializeDCWithEnum (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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 ());
809 public void SerializerDCArrayXml ()
811 StringWriter sw
= new StringWriter ();
812 SerializerDCArray (XmlWriter
.Create (sw
, settings
));
814 @"<root type=""array""><item type=""object""><_colors type=""number"">0</_colors></item><item type=""object""><_colors type=""number"">1</_colors></item></root>",
819 public void SerializerDCArrayJson ()
821 MemoryStream ms
= new MemoryStream ();
822 SerializerDCArray (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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
);
840 public void SerializerDCArray2Xml ()
842 StringWriter sw
= new StringWriter ();
843 SerializerDCArray2 (XmlWriter
.Create (sw
, settings
));
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>",
850 public void SerializerDCArray2Json ()
852 MemoryStream ms
= new MemoryStream ();
853 SerializerDCArray2 (JsonReaderWriterFactory
.CreateJsonWriter (ms
));
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
);
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
);
887 @"<root type=""array""><item type=""number"">1</item><item type=""number"">2</item></root>",
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
);
905 Encoding
.UTF8
.GetString (ms
.ToArray ()));
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>",
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 ()));
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");
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 ());
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 ());
975 public void DeserializeString ()
977 Assert
.AreEqual ("ABC", Deserialize ("\"ABC\"", typeof (string)));
981 public void DeserializeInt ()
983 Assert
.AreEqual (5, Deserialize ("5", typeof (int)));
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");
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");
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");
1014 [ExpectedException (typeof (SerializationException
))]
1015 public void DeserializeEmptyAsString ()
1017 // it somehow expects "root" which should have been already consumed.
1018 Deserialize ("", typeof (string));
1022 [ExpectedException (typeof (SerializationException
))]
1023 public void DeserializeEmptyAsInt ()
1025 // it somehow expects "root" which should have been already consumed.
1026 Deserialize ("", typeof (int));
1030 [ExpectedException (typeof (SerializationException
))]
1031 public void DeserializeEmptyAsDBNull ()
1033 // it somehow expects "root" which should have been already consumed.
1034 Deserialize ("", typeof (DBNull
));
1038 public void DeserializeEmptyObjectAsString ()
1040 // looks like it is converted to ""
1041 Assert
.AreEqual (String
.Empty
, Deserialize ("{}", typeof (string)));
1045 [ExpectedException (typeof (SerializationException
))]
1046 public void DeserializeEmptyObjectAsInt ()
1048 Deserialize ("{}", typeof (int));
1052 public void DeserializeEmptyObjectAsDBNull ()
1054 Assert
.AreEqual (DBNull
.Value
, Deserialize ("{}", typeof (DBNull
)));
1058 [ExpectedException (typeof (SerializationException
))]
1059 public void DeserializeEnumByName ()
1061 // enum is parsed into long
1062 Deserialize (@"""Red""", typeof (Colors
));
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");
1076 [ExpectedException (typeof (SerializationException
))]
1077 public void DeserializeEnumInvalid ()
1079 Deserialize ("", typeof (Colors
));
1083 [ExpectedException (typeof (SerializationException
))]
1084 [Category ("NotDotNet")] // 0.0 is an invalid Colors value.
1085 public void DeserializeEnumInvalid3 ()
1087 //"0.0" instead of "0"
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");
1104 [ExpectedException (typeof (SerializationException
))]
1105 [Category ("NotDotNet")] // 4 is an invalid Colors value.
1106 [Category ("NotWorking")]
1107 public void DeserializeEnumWithDCInvalid ()
1111 typeof (ColorsWithDC
));
1115 public void DeserializeDCWithEnum ()
1117 object o
= Deserialize (
1119 typeof (DCWithEnum
));
1121 Assert
.AreEqual (typeof (DCWithEnum
), o
.GetType (), "#de7");
1122 DCWithEnum dc
= (DCWithEnum
) o
;
1123 Assert
.AreEqual (Colors
.Red
, dc
.colors
, "#de8");
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");
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);
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 ());
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");
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
);
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");
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" }
;
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");
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");
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");
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");
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");
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
)));
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");
1297 public void ReadWriteNullObject ()
1299 DataContractJsonSerializer dcjs
= new DataContractJsonSerializer (typeof (string));
1300 using (MemoryStream ms
= new MemoryStream ()) {
1301 dcjs
.WriteObject (ms
, null);
1303 using (StreamReader sr
= new StreamReader (ms
)) {
1304 string data
= sr
.ReadToEnd ();
1305 Assert
.AreEqual ("null", data
, "WriteObject(stream,null)");
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
);
1319 using (StreamReader sr
= new StreamReader (ms
)) {
1320 Assert
.AreEqual (expected
, sr
.ReadToEnd (), "WriteObject");
1323 return dcjs
.ReadObject (ms
);
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")));
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")));
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");
1358 Console
.WriteLine (new StreamReader (ms
).ReadToEnd ());
1360 var q
= (Query
) serializer
.ReadObject(ms
);
1361 Assert
.AreEqual (query
.StartDate
, q
.StartDate
, "#2");
1362 Assert
.AreEqual (query
.EndDate
, q
.EndDate
, "#3");
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");
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
);
1386 Assert
.IsTrue (GSPlayerListErg
.A
, "A");
1387 Assert
.IsTrue (GSPlayerListErg
.B
, "B");
1388 Assert
.IsTrue (GSPlayerListErg
.C
, "C");
1392 public void WriteChar ()
1394 DataContractJsonSerializer serializer
= new DataContractJsonSerializer(typeof (CharTest
));
1395 using (MemoryStream ms
= new MemoryStream()) {
1396 serializer
.WriteObject(ms
, new CharTest ());
1398 using (StreamReader reader
= new StreamReader(ms
)) {
1405 public class CharTest
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
{
1421 [DataContract (Name
= "_ColorsWithDC")]
1422 public enum ColorsWithDC
{
1424 [EnumMember (Value
= "_Red")]
1432 public enum ColorsEnumMemberNoDC
{
1433 [EnumMember (Value
= "_Red")]
1441 public class DCWithEnum
{
1442 [DataMember (Name
= "_colors")]
1443 public Colors colors
;
1447 public class DCEmpty
1449 // serializer doesn't touch it.
1450 public string Foo
= "TEST";
1454 public class DCSimple1
1457 public string Foo
= "TEST";
1461 public class DCHasNonDC
1464 public NonDC Hoge
= new NonDC ();
1469 public string Whee
= "whee!";
1473 public class DCHasSerializable
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
1493 [DataContract (Name
= null)]
1494 public class DCWithNullName
1500 [DataContract (Namespace
= "")]
1501 public class DCWithEmptyNamespace
1508 public class SimpleSer1
1510 public string Doh
= "doh!";
1513 public class Wrapper
1516 public class DCWrapped
1522 public class CollectionContainer
1524 Collection
<string> items
= new Collection
<string> ();
1527 public Collection
<string> Items
{
1528 get { return items; }
1532 [CollectionDataContract
]
1533 public class DataCollection
<T
> : Collection
<T
>
1538 public class DataCollectionContainer
1540 DataCollection
<string> items
= new DataCollection
<string> ();
1543 public DataCollection
<string> Items
{
1544 get { return items; }
1549 class SerializeNonDCArrayType
1552 public NonDCItem
[] IPAddresses
= new NonDCItem
[0];
1555 public class NonDCItem
1557 public byte [] Data { get; set; }
1561 public class VerifyObjectNameTestData
1564 string Member1
= "foo";
1568 public class KeyValueTestData
{
1569 public List
<KeyValuePair
<string,string>> TestData
= new List
<KeyValuePair
<string,string>>();
1572 [DataContract
] // bug #586169
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; }
1589 public class GSPlayerListErg
1591 public GSPlayerListErg ()
1602 public void OnDeserializing (StreamingContext c
)
1609 void OnDeserialized (StreamingContext c
)
1614 public static bool A
, B
, C
;
1616 [DataMember (Name
= "T")]
1617 public long CodedServerTimeUTC { get; set; }
1618 public DateTime ServerTimeUTC { get; set; }