| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541 |
- //
- // 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)).WriteObject (new MemoryStream (), new 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 NonDCItem [] {new NonDCItem () { Data = 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");
- XmlElement el = doc.SelectSingleNode ("/root/IPAddresses/item/Data", nsmgr) as XmlElement;
- Assert.IsNotNull (el, "#3");
- Assert.AreEqual (4, 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");
- }
- [Test]
- public void AlwaysEmitTypeInformation ()
- {
- var ms = new MemoryStream ();
- var ds = new DataContractJsonSerializer (typeof (string), "root", null, 10, false, null, true);
- ds.WriteObject (ms, "foobar");
- var s = Encoding.UTF8.GetString (ms.ToArray ());
- Assert.AreEqual ("\"foobar\"", s, "#1");
- }
- [Test]
- public void AlwaysEmitTypeInformation2 ()
- {
- var ms = new MemoryStream ();
- var ds = new DataContractJsonSerializer (typeof (TestData), "root", null, 10, false, null, true);
- ds.WriteObject (ms, new TestData () { Foo = "foo"});
- var s = Encoding.UTF8.GetString (ms.ToArray ());
- Assert.AreEqual (@"{""__type"":""TestData:#MonoTests.System.Runtime.Serialization.Json"",""Bar"":null,""Foo"":""foo""}", s, "#1");
- }
- [Test]
- public void AlwaysEmitTypeInformation3 ()
- {
- var ms = new MemoryStream ();
- var ds = new DataContractJsonSerializer (typeof (TestData), "root", null, 10, false, null, false);
- ds.WriteObject (ms, new TestData () { Foo = "foo"});
- var s = Encoding.UTF8.GetString (ms.ToArray ());
- Assert.AreEqual (@"{""Bar"":null,""Foo"":""foo""}", s, "#1");
- }
- [Test]
- public void TestNonpublicDeserialization ()
- {
- string s1= @"{""Bar"":""bar"", ""Foo"":""foo"", ""Baz"":""baz""}";
- TestData o1 = ((TestData)(new DataContractJsonSerializer (typeof (TestData)).ReadObject (JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (s1), new XmlDictionaryReaderQuotas ()))));
- Assert.AreEqual (null, o1.Baz, "#1");
- string s2 = @"{""TestData"":[{""key"":""key1"",""value"":""value1""}]}";
- KeyValueTestData o2 = ((KeyValueTestData)(new DataContractJsonSerializer (typeof (KeyValueTestData)).ReadObject (JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (s2), new XmlDictionaryReaderQuotas ()))));
- Assert.AreEqual (1, o2.TestData.Count, "#2");
- Assert.AreEqual ("key1", o2.TestData[0].Key, "#3");
- Assert.AreEqual ("value1", o2.TestData[0].Value, "#4");
- }
- // [Test] use this case if you want to check lame silverlight parser behavior. Seealso #549756
- public void QuotelessDeserialization ()
- {
- string s1 = @"{FooMember:""value""}";
- var ds = new DataContractJsonSerializer (typeof (DCWithName));
- ds.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (s1)));
- string s2 = @"{FooMember:"" \""{dummy:string}\""""}";
- ds.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (s2)));
- }
- [Test]
- [Category ("NotWorking")]
- public void TypeIsNotPartsOfKnownTypes ()
- {
- var dcs = new DataContractSerializer (typeof (string));
- Assert.AreEqual (0, dcs.KnownTypes.Count, "KnownTypes #1");
- var dcjs = new DataContractJsonSerializer (typeof (string));
- Assert.AreEqual (0, dcjs.KnownTypes.Count, "KnownTypes #2");
- }
- [Test]
- public void ReadWriteNullObject ()
- {
- DataContractJsonSerializer dcjs = new DataContractJsonSerializer (typeof (string));
- using (MemoryStream ms = new MemoryStream ()) {
- dcjs.WriteObject (ms, null);
- ms.Position = 0;
- using (StreamReader sr = new StreamReader (ms)) {
- string data = sr.ReadToEnd ();
- Assert.AreEqual ("null", data, "WriteObject(stream,null)");
- ms.Position = 0;
- Assert.IsNull (dcjs.ReadObject (ms), "ReadObject(stream)");
- }
- };
- }
- object ReadWriteObject (Type type, object obj, string expected)
- {
- using (MemoryStream ms = new MemoryStream ()) {
- DataContractJsonSerializer dcjs = new DataContractJsonSerializer (type);
- dcjs.WriteObject (ms, obj);
- ms.Position = 0;
- using (StreamReader sr = new StreamReader (ms)) {
- Assert.AreEqual (expected, sr.ReadToEnd (), "WriteObject");
- ms.Position = 0;
- return dcjs.ReadObject (ms);
- }
- }
- }
- [Test]
- [Ignore ("Wrong test case. See bug #573691")]
- public void ReadWriteObject_Single_SpecialCases ()
- {
- Assert.IsTrue (Single.IsNaN ((float) ReadWriteObject (typeof (float), Single.NaN, "NaN")));
- Assert.IsTrue (Single.IsNegativeInfinity ((float) ReadWriteObject (typeof (float), Single.NegativeInfinity, "-INF")));
- Assert.IsTrue (Single.IsPositiveInfinity ((float) ReadWriteObject (typeof (float), Single.PositiveInfinity, "INF")));
- }
- [Test]
- [Ignore ("Wrong test case. See bug #573691")]
- public void ReadWriteObject_Double_SpecialCases ()
- {
- Assert.IsTrue (Double.IsNaN ((double) ReadWriteObject (typeof (double), Double.NaN, "NaN")));
- Assert.IsTrue (Double.IsNegativeInfinity ((double) ReadWriteObject (typeof (double), Double.NegativeInfinity, "-INF")));
- Assert.IsTrue (Double.IsPositiveInfinity ((double) ReadWriteObject (typeof (double), Double.PositiveInfinity, "INF")));
- }
- [Test]
- public void ReadWriteDateTime ()
- {
- var ms = new MemoryStream ();
- DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (Query));
- Query query = new Query () {
- StartDate = new DateTime (2010, 3, 4, 5, 6, 7),
- EndDate = new DateTime (2010, 4, 5, 6, 7, 8)
- };
- serializer.WriteObject (ms, query);
- Assert.AreEqual ("{\"StartDate\":\"\\/Date(1267679167000)\\/\",\"EndDate\":\"\\/Date(1270447628000)\\/\"}", Encoding.UTF8.GetString (ms.ToArray ()), "#1");
- ms.Position = 0;
- Console.WriteLine (new StreamReader (ms).ReadToEnd ());
- ms.Position = 0;
- var q = (Query) serializer.ReadObject(ms);
- Assert.AreEqual (query.StartDate, q.StartDate, "#2");
- Assert.AreEqual (query.EndDate, q.EndDate, "#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
- {
- [DataMember]
- public string Foo;
- }
- [DataContract (Name = null)]
- public class DCWithNullName
- {
- [DataMember]
- public string Foo;
- }
- [DataContract (Namespace = "")]
- public class DCWithEmptyNamespace
- {
- [DataMember]
- public string Foo;
- }
- [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 NonDCItem [] IPAddresses = new NonDCItem [0];
- }
- public class NonDCItem
- {
- public byte [] Data { get; set; }
- }
- [DataContract]
- public class VerifyObjectNameTestData
- {
- [DataMember]
- string Member1 = "foo";
- }
- [Serializable]
- public class KeyValueTestData {
- public List<KeyValuePair<string,string>> TestData = new List<KeyValuePair<string,string>>();
- }
- [DataContract] // bug #586169
- public class Query
- {
- [DataMember (Order=1)]
- public DateTime StartDate { get; set; }
- [DataMember (Order=2)]
- public DateTime EndDate { get; set; }
- }
- }
- [DataContract]
- class GlobalSample1
- {
- }
|