| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381 |
- //
- // DataContractJsonSerializerTest.cs
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- // Ankit Jain <[email protected]>
- //
- // Copyright (C) 2005-2007 Novell, Inc. http://www.novell.com
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- //
- // This test code contains tests for DataContractJsonSerializer, which is
- // imported from DataContractSerializerTest.cs.
- //
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Net;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Json;
- using System.Text;
- using System.Xml;
- using NUnit.Framework;
- namespace MonoTests.System.Runtime.Serialization.Json
- {
- [TestFixture]
- public class DataContractJsonSerializerTest
- {
- static readonly XmlWriterSettings settings;
- static DataContractJsonSerializerTest ()
- {
- settings = new XmlWriterSettings ();
- settings.OmitXmlDeclaration = true;
- }
- [DataContract]
- class Sample1
- {
- [DataMember]
- public string Member1;
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ConstructorTypeNull ()
- {
- new DataContractJsonSerializer (null);
- }
- [Test]
- public void ConstructorKnownTypesNull ()
- {
- // null knownTypes is allowed.
- new DataContractJsonSerializer (typeof (Sample1), (IEnumerable<Type>) null);
- new DataContractJsonSerializer (typeof (Sample1), "Foo", null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ConstructorNameNull ()
- {
- new DataContractJsonSerializer (typeof (Sample1), (string) null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentOutOfRangeException))]
- public void ConstructorNegativeMaxObjects ()
- {
- new DataContractJsonSerializer (typeof (Sample1), "Sample1",
- null, -1, false, null, false);
- }
- [Test]
- public void ConstructorMisc ()
- {
- new DataContractJsonSerializer (typeof (GlobalSample1));
- }
- [Test]
- public void WriteObjectContent ()
- {
- StringWriter sw = new StringWriter ();
- using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
- DataContractJsonSerializer ser =
- new DataContractJsonSerializer (typeof (string));
- xw.WriteStartElement ("my-element");
- ser.WriteObjectContent (xw, "TEST STRING");
- xw.WriteEndElement ();
- }
- Assert.AreEqual ("<my-element>TEST STRING</my-element>",
- sw.ToString ());
- }
- // int
- [Test]
- public void SerializeIntXml ()
- {
- StringWriter sw = new StringWriter ();
- SerializeInt (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root type=""number"">1</root>",
- sw.ToString ());
- }
- [Test]
- public void SerializeIntJson ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeInt (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- "1",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializeInt (XmlWriter writer)
- {
- DataContractJsonSerializer ser =
- new DataContractJsonSerializer (typeof (int));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, 1);
- }
- }
- // int, with rootName
- [Test]
- public void SerializeIntXmlWithRootName ()
- {
- StringWriter sw = new StringWriter ();
- SerializeIntWithRootName (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<myroot type=""number"">1</myroot>",
- sw.ToString ());
- }
- [Test]
- // since JsonWriter supports only "root" as the root name, using
- // XmlWriter from JsonReaderWriterFactory will always fail with
- // an explicit rootName.
- [ExpectedException (typeof (SerializationException))]
- public void SerializeIntJsonWithRootName ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeIntWithRootName (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- "1",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializeIntWithRootName (XmlWriter writer)
- {
- DataContractJsonSerializer ser =
- new DataContractJsonSerializer (typeof (int), "myroot");
- using (XmlWriter w = writer) {
- ser.WriteObject (w, 1);
- }
- }
- // pass typeof(DCEmpty), serialize int
- [Test]
- public void SerializeIntForDCEmptyXml ()
- {
- StringWriter sw = new StringWriter ();
- SerializeIntForDCEmpty (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root type=""number"">1</root>",
- sw.ToString ());
- }
- [Test]
- public void SerializeIntForDCEmptyJson ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeIntForDCEmpty (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- "1",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializeIntForDCEmpty (XmlWriter writer)
- {
- DataContractJsonSerializer ser =
- new DataContractJsonSerializer (typeof (DCEmpty));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, 1);
- }
- }
- // DCEmpty
- [Test]
- public void SerializeEmptyClassXml ()
- {
- StringWriter sw = new StringWriter ();
- SerializeEmptyClass (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root type=""object"" />",
- sw.ToString ());
- }
- [Test]
- public void SerializeEmptyClassJson ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeEmptyClass (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- "{}",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializeEmptyClass (XmlWriter writer)
- {
- DataContractJsonSerializer ser =
- new DataContractJsonSerializer (typeof (DCEmpty));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, new DCEmpty ());
- }
- }
- // string (primitive)
- [Test]
- public void SerializePrimitiveStringXml ()
- {
- StringWriter sw = new StringWriter ();
- SerializePrimitiveString (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- "<root>TEST</root>",
- sw.ToString ());
- }
- [Test]
- public void SerializePrimitiveStringJson ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializePrimitiveString (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- @"""TEST""",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializePrimitiveString (XmlWriter writer)
- {
- XmlObjectSerializer ser =
- new DataContractJsonSerializer (typeof (string));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, "TEST");
- }
- }
- // QName (primitive but ...)
- [Test]
- public void SerializePrimitiveQNameXml ()
- {
- StringWriter sw = new StringWriter ();
- SerializePrimitiveQName (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- "<root>foo:urn:foo</root>",
- sw.ToString ());
- }
- [Test]
- public void SerializePrimitiveQNameJson ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializePrimitiveQName (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- @"""foo:urn:foo""",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializePrimitiveQName (XmlWriter writer)
- {
- XmlObjectSerializer ser =
- new DataContractJsonSerializer (typeof (XmlQualifiedName));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, new XmlQualifiedName ("foo", "urn:foo"));
- }
- }
- // DBNull (primitive)
- [Test]
- public void SerializeDBNullXml ()
- {
- StringWriter sw = new StringWriter ();
- SerializeDBNull (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root type=""object"" />",
- sw.ToString ());
- }
- [Test]
- public void SerializeDBNullJson ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeDBNull (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- "{}",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializeDBNull (XmlWriter writer)
- {
- DataContractJsonSerializer ser =
- new DataContractJsonSerializer (typeof (DBNull));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, DBNull.Value);
- }
- }
- // DCSimple1
- [Test]
- public void SerializeSimpleClass1Xml ()
- {
- StringWriter sw = new StringWriter ();
- SerializeSimpleClass1 (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root type=""object""><Foo>TEST</Foo></root>",
- sw.ToString ());
- }
- [Test]
- public void SerializeSimpleClass1Json ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeSimpleClass1 (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- @"{""Foo"":""TEST""}",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializeSimpleClass1 (XmlWriter writer)
- {
- DataContractJsonSerializer ser =
- new DataContractJsonSerializer (typeof (DCSimple1));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, new DCSimple1 ());
- }
- }
- // NonDC
- [Test]
- // NonDC is not a DataContract type.
- public void SerializeNonDCOnlyCtor ()
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (NonDC));
- }
- [Test]
- //[ExpectedException (typeof (InvalidDataContractException))]
- // NonDC is not a DataContract type.
- // UPDATE: non-DataContract types are became valid in RTM.
- public void SerializeNonDC ()
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (NonDC));
- using (XmlWriter w = XmlWriter.Create (TextWriter.Null, settings)) {
- ser.WriteObject (w, new NonDC ());
- }
- }
- // DCHasNonDC
- [Test]
- //[ExpectedException (typeof (InvalidDataContractException))]
- // DCHasNonDC itself is a DataContract type whose field is
- // marked as DataMember but its type is not DataContract.
- // UPDATE: non-DataContract types are became valid in RTM.
- public void SerializeDCHasNonDC ()
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCHasNonDC));
- using (XmlWriter w = XmlWriter.Create (TextWriter.Null, settings)) {
- ser.WriteObject (w, new DCHasNonDC ());
- }
- }
- // DCHasSerializable
- [Test]
- public void SerializeSimpleSerializable1Xml ()
- {
- StringWriter sw = new StringWriter ();
- SerializeSimpleSerializable1 (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root type=""object""><Ser type=""object""><Doh>doh!</Doh></Ser></root>",
- sw.ToString ());
- }
- [Test]
- public void SerializeSimpleSerializable1Json ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeSimpleSerializable1 (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- @"{""Ser"":{""Doh"":""doh!""}}",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- // DCHasSerializable itself is DataContract and has a field
- // whose type is not contract but serializable.
- void SerializeSimpleSerializable1 (XmlWriter writer)
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCHasSerializable));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, new DCHasSerializable ());
- }
- }
- [Test]
- public void SerializeDCWithNameXml ()
- {
- StringWriter sw = new StringWriter ();
- SerializeDCWithName (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root type=""object""><FooMember>value</FooMember></root>",
- sw.ToString ());
- }
- [Test]
- public void SerializeDCWithNameJson ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeDCWithName (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- @"{""FooMember"":""value""}",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializeDCWithName (XmlWriter writer)
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithName));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, new DCWithName ());
- }
- }
- [Test]
- public void SerializeDCWithEmptyName1 ()
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEmptyName));
- StringWriter sw = new StringWriter ();
- DCWithEmptyName dc = new DCWithEmptyName ();
- using (XmlWriter w = XmlWriter.Create (sw, settings)) {
- try {
- ser.WriteObject (w, dc);
- } catch (InvalidDataContractException) {
- return;
- }
- }
- Assert.Fail ("Expected InvalidDataContractException");
- }
- [Test]
- public void SerializeDCWithEmptyName2 ()
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithName));
- StringWriter sw = new StringWriter ();
- /* DataContractAttribute.Name == "", not valid */
- DCWithEmptyName dc = new DCWithEmptyName ();
- using (XmlWriter w = XmlWriter.Create (sw, settings)) {
- try {
- ser.WriteObject (w, dc);
- } catch (InvalidDataContractException) {
- return;
- }
- }
- Assert.Fail ("Expected InvalidDataContractException");
- }
- [Test]
- [Category("NotWorking")]
- public void SerializeDCWithNullName ()
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithNullName));
- StringWriter sw = new StringWriter ();
- using (XmlWriter w = XmlWriter.Create (sw, settings)) {
- try {
- /* DataContractAttribute.Name == "", not valid */
- ser.WriteObject (w, new DCWithNullName ());
- } catch (InvalidDataContractException) {
- return;
- }
- }
- Assert.Fail ("Expected InvalidDataContractException");
- }
- [Test]
- public void SerializeDCWithEmptyNamespace1 ()
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEmptyNamespace));
- StringWriter sw = new StringWriter ();
- using (XmlWriter w = XmlWriter.Create (sw, settings)) {
- ser.WriteObject (w, new DCWithEmptyNamespace ());
- }
- }
- [Test]
- public void SerializeWrappedClassXml ()
- {
- StringWriter sw = new StringWriter ();
- SerializeWrappedClass (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root type=""object"" />",
- sw.ToString ());
- }
- [Test]
- public void SerializeWrappedClassJson ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeWrappedClass (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- "{}",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializeWrappedClass (XmlWriter writer)
- {
- DataContractJsonSerializer ser =
- new DataContractJsonSerializer (typeof (Wrapper.DCWrapped));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, new Wrapper.DCWrapped ());
- }
- }
- // CollectionContainer : Items must have a setter. (but became valid in RTM).
- [Test]
- public void SerializeReadOnlyCollectionMember ()
- {
- DataContractJsonSerializer ser =
- new DataContractJsonSerializer (typeof (CollectionContainer));
- StringWriter sw = new StringWriter ();
- using (XmlWriter w = XmlWriter.Create (sw, settings)) {
- ser.WriteObject (w, null);
- }
- }
- // DataCollectionContainer : Items must have a setter. (but became valid in RTM).
- [Test]
- public void SerializeReadOnlyDataCollectionMember ()
- {
- DataContractJsonSerializer ser =
- new DataContractJsonSerializer (typeof (DataCollectionContainer));
- StringWriter sw = new StringWriter ();
- using (XmlWriter w = XmlWriter.Create (sw, settings)) {
- ser.WriteObject (w, null);
- }
- }
- [Test]
- [Ignore ("https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=409970")]
- [ExpectedException (typeof (SerializationException))]
- public void DeserializeReadOnlyDataCollection_NullCollection ()
- {
- DataContractJsonSerializer ser =
- new DataContractJsonSerializer (typeof (CollectionContainer));
- StringWriter sw = new StringWriter ();
- var c = new CollectionContainer ();
- c.Items.Add ("foo");
- c.Items.Add ("bar");
- using (XmlWriter w = XmlWriter.Create (sw, settings))
- ser.WriteObject (w, c);
- // CollectionContainer.Items is null, so it cannot deserialize non-null collection.
- using (XmlReader r = XmlReader.Create (new StringReader (sw.ToString ())))
- c = (CollectionContainer) ser.ReadObject (r);
- }
- [Test]
- public void SerializeGuidXml ()
- {
- StringWriter sw = new StringWriter ();
- SerializeGuid (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root>00000000-0000-0000-0000-000000000000</root>",
- sw.ToString ());
- }
- [Test]
- public void SerializeGuidJson ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeGuid (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- @"""00000000-0000-0000-0000-000000000000""",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializeGuid (XmlWriter writer)
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Guid));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, Guid.Empty);
- }
- }
- [Test]
- public void SerializeEnumXml ()
- {
- StringWriter sw = new StringWriter ();
- SerializeEnum (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root type=""number"">0</root>",
- sw.ToString ());
- }
- [Test]
- public void SerializeEnumJson ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeEnum (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- "0",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializeEnum (XmlWriter writer)
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Colors));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, new Colors ());
- }
- }
- [Test]
- public void SerializeEnum2Xml ()
- {
- StringWriter sw = new StringWriter ();
- SerializeEnum2 (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root type=""number"">0</root>",
- sw.ToString ());
- }
- [Test]
- public void SerializeEnum2Json ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeEnum2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- "0",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializeEnum2 (XmlWriter writer)
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Colors));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, 0);
- }
- }
- [Test] // so, DataContract does not affect here.
- public void SerializeEnumWithDCXml ()
- {
- StringWriter sw = new StringWriter ();
- SerializeEnumWithDC (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root type=""number"">0</root>",
- sw.ToString ());
- }
- [Test]
- public void SerializeEnumWithDCJson ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeEnumWithDC (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- "0",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializeEnumWithDC (XmlWriter writer)
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, new ColorsWithDC ());
- }
- }
- [Test]
- public void SerializeEnumWithNoDCXml ()
- {
- StringWriter sw = new StringWriter ();
- SerializeEnumWithNoDC (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root type=""number"">0</root>",
- sw.ToString ());
- }
- [Test]
- public void SerializeEnumWithNoDCJson ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeEnumWithNoDC (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- "0",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializeEnumWithNoDC (XmlWriter writer)
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsEnumMemberNoDC));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, new ColorsEnumMemberNoDC ());
- }
- }
- [Test]
- public void SerializeEnumWithDC2Xml ()
- {
- StringWriter sw = new StringWriter ();
- SerializeEnumWithDC2 (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root type=""number"">3</root>",
- sw.ToString ());
- }
- [Test]
- public void SerializeEnumWithDC2Json ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeEnumWithDC2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- "3",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializeEnumWithDC2 (XmlWriter writer)
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, 3);
- }
- }
- /*
- [Test]
- [ExpectedException (typeof (SerializationException))]
- public void SerializeEnumWithDCInvalid ()
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
- StringWriter sw = new StringWriter ();
- ColorsWithDC cdc = ColorsWithDC.Blue;
- using (XmlWriter w = XmlWriter.Create (sw, settings)) {
- ser.WriteObject (w, cdc);
- }
- }
- */
- [Test]
- public void SerializeDCWithEnumXml ()
- {
- StringWriter sw = new StringWriter ();
- SerializeDCWithEnum (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root type=""object""><_colors type=""number"">0</_colors></root>",
- sw.ToString ());
- }
- [Test]
- public void SerializeDCWithEnumJson ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializeDCWithEnum (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- @"{""_colors"":0}",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializeDCWithEnum (XmlWriter writer)
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEnum));
- using (XmlWriter w = writer) {
- ser.WriteObject (w, new DCWithEnum ());
- }
- }
- [Test]
- public void SerializerDCArrayXml ()
- {
- StringWriter sw = new StringWriter ();
- SerializerDCArray (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<root type=""array""><item type=""object""><_colors type=""number"">0</_colors></item><item type=""object""><_colors type=""number"">1</_colors></item></root>",
- sw.ToString ());
- }
- [Test]
- public void SerializerDCArrayJson ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializerDCArray (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- @"[{""_colors"":0},{""_colors"":1}]",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializerDCArray (XmlWriter writer)
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEnum []));
- DCWithEnum [] arr = new DCWithEnum [2];
- arr [0] = new DCWithEnum (); arr [0].colors = Colors.Red;
- arr [1] = new DCWithEnum (); arr [1].colors = Colors.Green;
- using (XmlWriter w = writer) {
- ser.WriteObject (w, arr);
- }
- }
- [Test]
- public void SerializerDCArray2Xml ()
- {
- StringWriter sw = new StringWriter ();
- SerializerDCArray2 (XmlWriter.Create (sw, settings));
- Assert.AreEqual (
- @"<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>",
- sw.ToString ());
- }
- [Test]
- public void SerializerDCArray2Json ()
- {
- MemoryStream ms = new MemoryStream ();
- SerializerDCArray2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
- Assert.AreEqual (
- @"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0},{""__type"":""DCSimple1:#MonoTests.System.Runtime.Serialization.Json"",""Foo"":""hello""}]",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- void SerializerDCArray2 (XmlWriter writer)
- {
- List<Type> known = new List<Type> ();
- known.Add (typeof (DCWithEnum));
- known.Add (typeof (DCSimple1));
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (object []), known);
- object [] arr = new object [2];
- arr [0] = new DCWithEnum (); ((DCWithEnum)arr [0]).colors = Colors.Red;
- arr [1] = new DCSimple1 (); ((DCSimple1) arr [1]).Foo = "hello";
- using (XmlWriter w = writer) {
- ser.WriteObject (w, arr);
- }
- }
- [Test]
- public void SerializerDCArray3Xml ()
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (int []));
- StringWriter sw = new StringWriter ();
- int [] arr = new int [2];
- arr [0] = 1; arr [1] = 2;
- using (XmlWriter w = XmlWriter.Create (sw, settings)) {
- ser.WriteObject (w, arr);
- }
- Assert.AreEqual (
- @"<root type=""array""><item type=""number"">1</item><item type=""number"">2</item></root>",
- sw.ToString ());
- }
- [Test]
- public void SerializerDCArray3Json ()
- {
- MemoryStream ms = new MemoryStream ();
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (int []));
- int [] arr = new int [2];
- arr [0] = 1; arr [1] = 2;
- using (XmlWriter w = JsonReaderWriterFactory.CreateJsonWriter (ms)) {
- ser.WriteObject (w, arr);
- }
- Assert.AreEqual (
- @"[1,2]",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- [Test]
- // ... so, non-JSON XmlWriter is still accepted.
- public void SerializeNonDCArrayXml ()
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (SerializeNonDCArrayType));
- StringWriter sw = new StringWriter ();
- using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
- ser.WriteObject (xw, new SerializeNonDCArrayType ());
- }
- Assert.AreEqual (@"<root type=""object""><IPAddresses type=""array"" /></root>",
- sw.ToString ());
- }
- [Test]
- public void SerializeNonDCArrayJson ()
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (SerializeNonDCArrayType));
- MemoryStream ms = new MemoryStream ();
- using (XmlWriter xw = JsonReaderWriterFactory.CreateJsonWriter (ms)) {
- ser.WriteObject (xw, new SerializeNonDCArrayType ());
- }
- Assert.AreEqual (@"{""IPAddresses"":[]}",
- Encoding.UTF8.GetString (ms.ToArray ()));
- }
- [Test]
- public void SerializeNonDCArrayItems ()
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (SerializeNonDCArrayType));
- StringWriter sw = new StringWriter ();
- using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
- SerializeNonDCArrayType obj = new SerializeNonDCArrayType ();
- obj.IPAddresses = new IPAddress [] {new IPAddress (new byte [] {1, 2, 3, 4})};
- ser.WriteObject (xw, obj);
- }
- XmlDocument doc = new XmlDocument ();
- doc.LoadXml (sw.ToString ());
- XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
- nsmgr.AddNamespace ("s", "http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization");
- nsmgr.AddNamespace ("n", "http://schemas.datacontract.org/2004/07/System.Net");
- nsmgr.AddNamespace ("a", "http://schemas.microsoft.com/2003/10/Serialization/Arrays");
- Assert.AreEqual (1, doc.SelectNodes ("/root/IPAddresses/item", nsmgr).Count, "#1");
- Assert.AreEqual ("67305985", doc.SelectSingleNode ("/root/IPAddresses/item/m_Address", nsmgr).InnerText, "#2");
- XmlElement el = doc.SelectSingleNode ("/root/IPAddresses/item/m_Numbers", nsmgr) as XmlElement;
- Assert.IsNotNull (el, "#3");
- Assert.AreEqual (8, el.SelectNodes ("item", nsmgr).Count, "#4");
- }
- [Test]
- public void MaxItemsInObjectGraph1 ()
- {
- // object count == maximum
- DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCEmpty), null, 1, false, null, false);
- s.WriteObject (XmlWriter.Create (TextWriter.Null), new DCEmpty ());
- }
- [Test]
- [ExpectedException (typeof (SerializationException))]
- public void MaxItemsInObjectGraph2 ()
- {
- // object count > maximum
- DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCSimple1), null, 1, false, null, false);
- s.WriteObject (XmlWriter.Create (TextWriter.Null), new DCSimple1 ());
- }
- [Test]
- public void DeserializeString ()
- {
- Assert.AreEqual ("ABC", Deserialize ("\"ABC\"", typeof (string)));
- }
- [Test]
- public void DeserializeInt ()
- {
- Assert.AreEqual (5, Deserialize ("5", typeof (int)));
- }
- [Test]
- public void DeserializeArray ()
- {
- int [] ret = (int []) Deserialize ("[5,6,7]", typeof (int []));
- Assert.AreEqual (5, ret [0], "#1");
- Assert.AreEqual (6, ret [1], "#2");
- Assert.AreEqual (7, ret [2], "#3");
- }
- [Test]
- public void DeserializeArrayUntyped ()
- {
- object [] ret = (object []) Deserialize ("[5,6,7]", typeof (object []));
- Assert.AreEqual (5, ret [0], "#1");
- Assert.AreEqual (6, ret [1], "#2");
- Assert.AreEqual (7, ret [2], "#3");
- }
- [Test]
- public void DeserializeMixedArray ()
- {
- object [] ret = (object []) Deserialize ("[5,\"6\",false]", typeof (object []));
- Assert.AreEqual (5, ret [0], "#1");
- Assert.AreEqual ("6", ret [1], "#2");
- Assert.AreEqual (false, ret [2], "#3");
- }
- [Test]
- [ExpectedException (typeof (SerializationException))]
- public void DeserializeEmptyAsString ()
- {
- // it somehow expects "root" which should have been already consumed.
- Deserialize ("", typeof (string));
- }
- [Test]
- [ExpectedException (typeof (SerializationException))]
- public void DeserializeEmptyAsInt ()
- {
- // it somehow expects "root" which should have been already consumed.
- Deserialize ("", typeof (int));
- }
- [Test]
- [ExpectedException (typeof (SerializationException))]
- public void DeserializeEmptyAsDBNull ()
- {
- // it somehow expects "root" which should have been already consumed.
- Deserialize ("", typeof (DBNull));
- }
- [Test]
- public void DeserializeEmptyObjectAsString ()
- {
- // looks like it is converted to ""
- Assert.AreEqual (String.Empty, Deserialize ("{}", typeof (string)));
- }
- [Test]
- [ExpectedException (typeof (SerializationException))]
- public void DeserializeEmptyObjectAsInt ()
- {
- Deserialize ("{}", typeof (int));
- }
- [Test]
- public void DeserializeEmptyObjectAsDBNull ()
- {
- Assert.AreEqual (DBNull.Value, Deserialize ("{}", typeof (DBNull)));
- }
- [Test]
- [ExpectedException (typeof (SerializationException))]
- public void DeserializeEnumByName ()
- {
- // enum is parsed into long
- Deserialize (@"""Red""", typeof (Colors));
- }
- [Test]
- public void DeserializeEnum2 ()
- {
- object o = Deserialize ("0", typeof (Colors));
- Assert.AreEqual (typeof (Colors), o.GetType (), "#de3");
- Colors c = (Colors) o;
- Assert.AreEqual (Colors.Red, c, "#de4");
- }
-
- [Test]
- [ExpectedException (typeof (SerializationException))]
- public void DeserializeEnumInvalid ()
- {
- Deserialize ("", typeof (Colors));
- }
- [Test]
- [ExpectedException (typeof (SerializationException))]
- [Category ("NotDotNet")] // 0.0 is an invalid Colors value.
- public void DeserializeEnumInvalid3 ()
- {
- //"0.0" instead of "0"
- Deserialize (
- "0.0",
- typeof (Colors));
- }
- [Test]
- public void DeserializeEnumWithDC ()
- {
- object o = Deserialize ("0", typeof (ColorsWithDC));
-
- Assert.AreEqual (typeof (ColorsWithDC), o.GetType (), "#de5");
- ColorsWithDC cdc = (ColorsWithDC) o;
- Assert.AreEqual (ColorsWithDC.Red, o, "#de6");
- }
- [Test]
- [ExpectedException (typeof (SerializationException))]
- [Category ("NotDotNet")] // 4 is an invalid Colors value.
- [Category ("NotWorking")]
- public void DeserializeEnumWithDCInvalid ()
- {
- Deserialize (
- "4",
- typeof (ColorsWithDC));
- }
- [Test]
- public void DeserializeDCWithEnum ()
- {
- object o = Deserialize (
- "{\"_colors\":0}",
- typeof (DCWithEnum));
- Assert.AreEqual (typeof (DCWithEnum), o.GetType (), "#de7");
- DCWithEnum dc = (DCWithEnum) o;
- Assert.AreEqual (Colors.Red, dc.colors, "#de8");
- }
- [Test]
- public void ReadObjectVerifyObjectNameFalse ()
- {
- string xml = @"<any><Member1>bar</Member1></any>";
- object o = new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
- .ReadObject (XmlReader.Create (new StringReader (xml)), false);
- Assert.IsTrue (o is VerifyObjectNameTestData, "#1");
- string xml2 = @"<any><x:Member1 xmlns:x=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">bar</x:Member1></any>";
- o = new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
- .ReadObject (XmlReader.Create (new StringReader (xml2)), false);
- Assert.IsTrue (o is VerifyObjectNameTestData, "#2");
- }
- [Test]
- [ExpectedException (typeof (SerializationException))]
- public void ReadObjectVerifyObjectNameTrue ()
- {
- string xml = @"<any><Member1>bar</Member1></any>";
- new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
- .ReadObject (XmlReader.Create (new StringReader (xml)), true);
- }
- [Test] // member name is out of scope
- public void ReadObjectVerifyObjectNameTrue2 ()
- {
- string xml = @"<root><Member2>bar</Member2></root>";
- new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
- .ReadObject (XmlReader.Create (new StringReader (xml)), true);
- }
- [Test]
- public void ReadTypedObjectJson ()
- {
- object o = Deserialize (@"{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0}", typeof (DCWithEnum));
- Assert.AreEqual (typeof (DCWithEnum), o.GetType ());
- }
- [Test]
- public void ReadObjectDCArrayJson ()
- {
- object o = Deserialize (@"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0}]",
- typeof (object []), typeof (DCWithEnum));
- Assert.AreEqual (typeof (object []), o.GetType (), "#1");
- object [] arr = (object []) o;
- Assert.AreEqual (typeof (DCWithEnum), arr [0].GetType (), "#2");
- }
- [Test]
- public void ReadObjectDCArray2Json ()
- {
- object o = Deserialize (@"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0},{""__type"":""DCSimple1:#MonoTests.System.Runtime.Serialization.Json"",""Foo"":""hello""}]",
- typeof (object []), typeof (DCWithEnum), typeof (DCSimple1));
- Assert.AreEqual (typeof (object []), o.GetType (), "#1");
- object [] arr = (object []) o;
- Assert.AreEqual (typeof (DCWithEnum), arr [0].GetType (), "#2");
- Assert.AreEqual (typeof (DCSimple1), arr [1].GetType (), "#3");
- }
- private object Deserialize (string xml, Type type, params Type [] knownTypes)
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer (type, knownTypes);
- XmlReader xr = JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (xml), new XmlDictionaryReaderQuotas ());
- return ser.ReadObject (xr);
- }
- [Test]
- public void IsStartObject ()
- {
- DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCSimple1));
- Assert.IsTrue (s.IsStartObject (XmlReader.Create (new StringReader ("<root></root>"))), "#1");
- Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<dummy></dummy>"))), "#2");
- Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<Foo></Foo>"))), "#3");
- Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<root xmlns='urn:foo'></root>"))), "#4");
- }
- [Test]
- public void SerializeNonDC2 ()
- {
- var ser = new DataContractJsonSerializer (typeof (TestData));
- StringWriter sw = new StringWriter ();
- var obj = new TestData () { Foo = "foo", Bar = "bar", Baz = "baz" };
- // XML
- using (var xw = XmlWriter.Create (sw))
- ser.WriteObject (xw, obj);
- var s = sw.ToString ();
- // since the order is not preserved, we compare only contents.
- Assert.IsTrue (s.IndexOf ("<Foo>foo</Foo>") > 0, "#1-1");
- Assert.IsTrue (s.IndexOf ("<Bar>bar</Bar>") > 0, "#1-2");
- Assert.IsFalse (s.IndexOf ("<Baz>baz</Baz>") > 0, "#1-3");
- // JSON
- MemoryStream ms = new MemoryStream ();
- using (var xw = JsonReaderWriterFactory.CreateJsonWriter (ms))
- ser.WriteObject (ms, obj);
- s = new StreamReader (new MemoryStream (ms.ToArray ())).ReadToEnd ().Replace ('"', '/');
- // since the order is not preserved, we compare only contents.
- Assert.IsTrue (s.IndexOf ("/Foo/:/foo/") > 0, "#2-1");
- Assert.IsTrue (s.IndexOf ("/Bar/:/bar/") > 0, "#2-2");
- Assert.IsFalse (s.IndexOf ("/Baz/:/baz/") > 0, "#2-3");
- }
- }
- public class TestData
- {
- public string Foo { get; set; }
- public string Bar { get; set; }
- internal string Baz { get; set; }
- }
- public enum Colors {
- Red, Green, Blue
- }
- [DataContract (Name = "_ColorsWithDC")]
- public enum ColorsWithDC {
- [EnumMember (Value = "_Red")]
- Red,
- [EnumMember]
- Green,
- Blue
- }
- public enum ColorsEnumMemberNoDC {
- [EnumMember (Value = "_Red")]
- Red,
- [EnumMember]
- Green,
- Blue
- }
- [DataContract]
- public class DCWithEnum {
- [DataMember (Name = "_colors")]
- public Colors colors;
- }
- [DataContract]
- public class DCEmpty
- {
- // serializer doesn't touch it.
- public string Foo = "TEST";
- }
- [DataContract]
- public class DCSimple1
- {
- [DataMember]
- public string Foo = "TEST";
- }
- [DataContract]
- public class DCHasNonDC
- {
- [DataMember]
- public NonDC Hoge= new NonDC ();
- }
- public class NonDC
- {
- public string Whee = "whee!";
- }
- [DataContract]
- public class DCHasSerializable
- {
- [DataMember]
- public SimpleSer1 Ser = new SimpleSer1 ();
- }
- [DataContract (Name = "Foo")]
- public class DCWithName
- {
- [DataMember (Name = "FooMember")]
- public string DMWithName = "value";
- }
- [DataContract (Name = "")]
- public class DCWithEmptyName
- {
- }
- [DataContract (Name = null)]
- public class DCWithNullName
- {
- }
- [DataContract (Namespace = "")]
- public class DCWithEmptyNamespace
- {
- }
- [Serializable]
- public class SimpleSer1
- {
- public string Doh = "doh!";
- }
- public class Wrapper
- {
- [DataContract]
- public class DCWrapped
- {
- }
- }
- [DataContract]
- public class CollectionContainer
- {
- Collection<string> items = new Collection<string> ();
- [DataMember]
- public Collection<string> Items {
- get { return items; }
- }
- }
- [CollectionDataContract]
- public class DataCollection<T> : Collection<T>
- {
- }
- [DataContract]
- public class DataCollectionContainer
- {
- DataCollection<string> items = new DataCollection<string> ();
- [DataMember]
- public DataCollection<string> Items {
- get { return items; }
- }
- }
- [DataContract]
- class SerializeNonDCArrayType
- {
- [DataMember]
- public IPAddress [] IPAddresses = new IPAddress [0];
- }
- [DataContract]
- public class VerifyObjectNameTestData
- {
- [DataMember]
- string Member1 = "foo";
- }
- }
- [DataContract]
- class GlobalSample1
- {
- }
|