//
using System;
+using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
+using System.Data;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
-using NUnit.Framework;
-using System.Xml.Serialization;
using System.Xml.Schema;
+using System.Xml.Serialization;
+using NUnit.Framework;
+
+[assembly: ContractNamespace ("http://www.u2u.be/samples/wcf/2009", ClrNamespace = "U2U.DataContracts")] // bug #599889
namespace MonoTests.System.Runtime.Serialization
{
[Test]
public void ConstructorKnownTypesNull ()
{
- // null knownTypes is allowed.
- new DataContractSerializer (typeof (Sample1), null);
- new DataContractSerializer (typeof (Sample1), "Foo", String.Empty, null);
- new DataContractSerializer (typeof (Sample1), new XmlDictionary ().Add ("Foo"), XmlDictionaryString.Empty, null);
+ // null knownTypes is allowed. Though the property is filled.
+ Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), null).KnownTypes, "#1");
+ Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), "Foo", String.Empty, null).KnownTypes, "#2");
+ Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), new XmlDictionary ().Add ("Foo"), XmlDictionaryString.Empty, null).KnownTypes, "#3");
}
[Test]
Assert.AreEqual (expected, sw.ToString ());
}
+ // DCEmpty
+
+ [Test]
+ public void SerializeEmptyNoNSClass ()
+ {
+ var ser = new DataContractSerializer (typeof (DCEmptyNoNS));
+ SerializeEmptyNoNSClass (ser, "<DCEmptyNoNS xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" />");
+ }
+
+ void SerializeEmptyNoNSClass (XmlObjectSerializer ser, string expected)
+ {
+ var sw = new StringWriter ();
+ using (var w = XmlWriter.Create (sw, settings)) {
+ ser.WriteObject (w, new DCEmptyNoNS ());
+ }
+ Assert.AreEqual (expected, sw.ToString ());
+ }
// string (primitive)
[Test]
Assert.AreEqual (expected, sw.ToString ());
}
- // NonDC
-
- [Test]
- // NonDC is not a DataContract type.
- public void SerializeNonDCOnlyCtor ()
- {
- DataContractSerializer ser = new DataContractSerializer (typeof (NonDC));
- }
+ // NonDC (behavior changed in 3.5/SP1; not it's not rejected)
[Test]
- /* old code
- [ExpectedException (typeof (InvalidDataContractException))]
- // NonDC is not a DataContract type.
- */
- [Category ("NotWorking")] // behavior has changed in 3.5/SP1
public void SerializeNonDC ()
{
DataContractSerializer ser = new DataContractSerializer (typeof (NonDC));
- using (XmlWriter w = XmlWriter.Create (TextWriter.Null, settings)) {
+ var sw = new StringWriter ();
+ using (XmlWriter w = XmlWriter.Create (sw, settings)) {
ser.WriteObject (w, new NonDC ());
}
+ Assert.AreEqual ("<NonDC xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Whee>whee!</Whee></NonDC>".Replace ('\'', '"'), sw.ToString ());
}
// DCHasNonDC
[Test]
- /* old code
- [ExpectedException (typeof (InvalidDataContractException))]
- // DCHasNonDC itself is a DataContract type whose field is
- // marked as DataMember but its type is not DataContract.
- */
- [Category ("NotWorking")] // behavior has changed in 3.5/SP1
public void SerializeDCHasNonDC ()
{
DataContractSerializer ser = new DataContractSerializer (typeof (DCHasNonDC));
- using (XmlWriter w = XmlWriter.Create (TextWriter.Null, settings)) {
+ var sw = new StringWriter ();
+ using (XmlWriter w = XmlWriter.Create (sw, settings)) {
ser.WriteObject (w, new DCHasNonDC ());
}
+ Assert.AreEqual ("<DCHasNonDC xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Hoge><Whee>whee!</Whee></Hoge></DCHasNonDC>".Replace ('\'', '"'), sw.ToString ());
}
// DCHasSerializable
// DataCollectionContainer : Items must have a setter.
[Test]
//[ExpectedException (typeof (InvalidDataContractException))]
- [Category ("NotWorking")]
public void SerializeReadOnlyDataCollectionMember ()
{
DataContractSerializer ser =
using (XmlWriter w = XmlWriter.Create (sw, settings)) {
ser.WriteObject (w, c);
}
+ // LAMESPEC: this is bogus behavior. .NET serializes
+ // System.String as "string" without overriding its
+ // element namespace, but then it must be regarded as
+ // in parent's namespace. What if there already is an
+ // element definition for "string" with the same
+ // namespace?
Assert.AreEqual ("<DataCollectionContainer xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Items><string>foo</string><string>bar</string></Items></DataCollectionContainer>".Replace ('\'', '"'), sw.ToString (), "#2");
}
using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
ser.WriteObject (xw, new SerializeNonDCArrayType ());
}
- Assert.AreEqual (@"<SerializeNonDCArrayType xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><IPAddresses xmlns:d2p1=""http://schemas.datacontract.org/2004/07/System.Net"" /></SerializeNonDCArrayType>",
+ Assert.AreEqual (@"<SerializeNonDCArrayType xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><IPAddresses /></SerializeNonDCArrayType>",
sw.ToString ());
}
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})};
+ obj.IPAddresses = new NonDCItem [] {new NonDCItem () { Data = new int [] {1, 2, 3, 4} } };
ser.WriteObject (xw, obj);
}
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 ("/s:SerializeNonDCArrayType/s:IPAddresses/n:IPAddress", nsmgr).Count, "#1");
- Assert.AreEqual ("67305985", doc.SelectSingleNode ("/s:SerializeNonDCArrayType/s:IPAddresses/n:IPAddress/n:m_Address", nsmgr).InnerText, "#2");
- XmlElement el = doc.SelectSingleNode ("/s:SerializeNonDCArrayType/s:IPAddresses/n:IPAddress/n:m_Numbers", nsmgr) as XmlElement;
+ Assert.AreEqual (1, doc.SelectNodes ("/s:SerializeNonDCArrayType/s:IPAddresses/s:NonDCItem", nsmgr).Count, "#1");
+ XmlElement el = doc.SelectSingleNode ("/s:SerializeNonDCArrayType/s:IPAddresses/s:NonDCItem/s:Data", nsmgr) as XmlElement;
Assert.IsNotNull (el, "#3");
- Assert.AreEqual (8, el.SelectNodes ("a:unsignedShort", nsmgr).Count, "#4");
+ Assert.AreEqual (4, el.SelectNodes ("a:int", nsmgr).Count, "#4");
}
[Test]
@"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">red</Colors>");
}
+ [Test]
+ public void DeserializeEnumFlags ()
+ {
+ Deserialize<Colors2> (
+ @"<Colors2 xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""/>");
+ }
+
[Test]
public void DeserializeEnumWithDC ()
{
Assert.AreEqual (w.T, w.T2, "#2");
}
+ [Test]
+ public void GenericSerialization ()
+ {
+ var sw = new StringWriter ();
+ var ser = new DataContractSerializer (typeof (Foo<string,int,int>));
+ using (var xw = XmlWriter.Create (sw))
+ ser.WriteObject (xw, new Foo<string,int,int> () {Field = "f"
+ });
+ var s = sw.ToString ();
+
+ var ret = (Foo<string,int,int>) ser.ReadObject (XmlReader.Create (new StringReader (s)));
+ Assert.AreEqual ("f", ret.Field);
+ }
+
+ [Test]
+ public void GenericCollectionSerialization ()
+ {
+ var l = new MyList ();
+ l.Add ("foo");
+ l.Add ("bar");
+ var ds = new DataContractSerializer (typeof (MyList));
+ var sw = new StringWriter ();
+ using (var xw = XmlWriter.Create (sw))
+ ds.WriteObject (xw, l);
+ l = (MyList) ds.ReadObject (XmlReader.Create (new StringReader (sw.ToString ())));
+ Assert.AreEqual (2, l.Count);
+ }
+
+ [Test]
+ public void GenericListOfKeyValuePairSerialization ()
+ {
+ string xml = @"<?xml version='1.0' encoding='utf-16'?><ArrayOfKeyValuePairOfstringstring xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/System.Collections.Generic'><KeyValuePairOfstringstring><key>foo</key><value>bar</value></KeyValuePairOfstringstring></ArrayOfKeyValuePairOfstringstring>".Replace ('\'', '"');
+
+ var ds = new DataContractSerializer (typeof (List<KeyValuePair<string,string>>));
+ var d = new List<KeyValuePair<string,string>> ();
+ d.Add (new KeyValuePair<string,string> ("foo", "bar"));
+ var sw = new StringWriter ();
+ using (var xw = XmlWriter.Create (sw))
+ ds.WriteObject (xw, d);
+ Assert.AreEqual (xml, sw.ToString (), "#1");
+ d = (List<KeyValuePair<string,string>>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
+ Assert.AreEqual (1, d.Count, "#2");
+ Assert.AreEqual ("bar", d [0].Value, "#3");
+ }
+
+ [Test]
+ public void GenericListOfDictionaryEntrySerialization ()
+ {
+ string xml = @"<?xml version='1.0' encoding='utf-16'?><ArrayOfDictionaryEntry xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/System.Collections'><DictionaryEntry><_key xmlns:d3p1='http://www.w3.org/2001/XMLSchema' i:type='d3p1:string'>foo</_key><_value xmlns:d3p1='http://www.w3.org/2001/XMLSchema' i:type='d3p1:string'>bar</_value></DictionaryEntry></ArrayOfDictionaryEntry>".Replace ('\'', '"');
+
+ var ds = new DataContractSerializer (typeof (List<DictionaryEntry>));
+ var d = new List<DictionaryEntry> ();
+ d.Add (new DictionaryEntry ("foo", "bar"));
+ var sw = new StringWriter ();
+ using (var xw = XmlWriter.Create (sw))
+ ds.WriteObject (xw, d);
+ Assert.AreEqual (xml, sw.ToString (), "#1");
+ Assert.IsTrue (sw.ToString ().IndexOf ("i:type") >= 0);
+ d = (List<DictionaryEntry>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
+ Assert.AreEqual (1, d.Count, "#2");
+ Assert.AreEqual ("bar", d [0].Value, "#3");
+ }
+
+ [Test]
+ public void GenericDictionarySerialization ()
+ {
+ string xml = @"<?xml version='1.0' encoding='utf-16'?><ArrayOfKeyValueOfstringstring xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.microsoft.com/2003/10/Serialization/Arrays'><KeyValueOfstringstring><Key>foo</Key><Value>bar</Value></KeyValueOfstringstring></ArrayOfKeyValueOfstringstring>".Replace ('\'', '"');
+
+ var ds = new DataContractSerializer (typeof (Dictionary<string,string>));
+ var d = new Dictionary<string,string> ();
+ d ["foo"] = "bar";
+ var sw = new StringWriter ();
+ using (var xw = XmlWriter.Create (sw))
+ ds.WriteObject (xw, d);
+ Assert.AreEqual (xml, sw.ToString (), "#1");
+ d = (Dictionary<string,string>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
+ Assert.AreEqual (1, d.Count, "#2");
+ Assert.AreEqual ("bar", d ["foo"], "#3");
+ }
+
+ [Test]
+ public void HashtableSerialization ()
+ {
+ string xml = @"<?xml version='1.0' encoding='utf-16'?><ArrayOfKeyValueOfanyTypeanyType xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.microsoft.com/2003/10/Serialization/Arrays'><KeyValueOfanyTypeanyType><Key xmlns:d3p1='http://www.w3.org/2001/XMLSchema' i:type='d3p1:string'>foo</Key><Value xmlns:d3p1='http://www.w3.org/2001/XMLSchema' i:type='d3p1:string'>bar</Value></KeyValueOfanyTypeanyType></ArrayOfKeyValueOfanyTypeanyType>".Replace ('\'', '"');
+
+ var ds = new DataContractSerializer (typeof (Hashtable));
+ var d = new Hashtable ();
+ d ["foo"] = "bar";
+ var sw = new StringWriter ();
+ using (var xw = XmlWriter.Create (sw))
+ ds.WriteObject (xw, d);
+ Assert.AreEqual (xml, sw.ToString (), "#1");
+ d = (Hashtable) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
+ Assert.AreEqual (1, d.Count, "#2");
+ Assert.AreEqual ("bar", d ["foo"], "#3");
+ }
+
+ [Test]
+ public void CollectionContarctDictionarySerialization ()
+ {
+ string xml = @"<?xml version='1.0' encoding='utf-16'?><NAME xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='urn:foo'><ITEM><KEY>foo</KEY><VALUE>bar</VALUE></ITEM></NAME>".Replace ('\'', '"');
+
+ var ds = new DataContractSerializer (typeof (MyDictionary<string,string>));
+ var d = new MyDictionary<string,string> ();
+ d ["foo"] = "bar";
+ var sw = new StringWriter ();
+ using (var xw = XmlWriter.Create (sw))
+ ds.WriteObject (xw, d);
+ Assert.AreEqual (xml, sw.ToString (), "#1");
+ d = (MyDictionary<string,string>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
+ Assert.AreEqual (1, d.Count, "#2");
+ Assert.AreEqual ("bar", d ["foo"], "#3");
+ }
+
+ [Test]
+ public void SerializeInterfaceCollection ()
+ {
+ var ser = new DataContractSerializer (typeof (InterfaceCollectionType));
+ var sw = new StringWriter ();
+ var obj = new InterfaceCollectionType ();
+ using (var xw = XmlWriter.Create (sw))
+ ser.WriteObject (xw, obj);
+ using (var xr = XmlReader.Create (new StringReader (sw.ToString ()))) {
+ obj = (InterfaceCollectionType) ser.ReadObject (xr);
+ Assert.IsNull (obj.Array, "#1");
+ }
+
+ sw = new StringWriter ();
+ obj.Array = new List<int> ();
+ obj.Array.Add (5);
+ using (var xw = XmlWriter.Create (sw))
+ ser.WriteObject (xw, obj);
+ using (var xr = XmlReader.Create (new StringReader (sw.ToString ()))) {
+ obj = (InterfaceCollectionType) ser.ReadObject (xr);
+ Assert.AreEqual (5, obj.Array [0], "#2");
+ }
+ }
+
+ [Test]
+ public void EmptyChildren ()
+ {
+ string xml = @"
+<DummyPlaylist xmlns='http://example.com/schemas/asx'>
+ <Entries>
+ <DummyEntry>
+ <EntryInfo xmlns:i='http://www.w3.org/2001/XMLSchema-instance' i:type='PartDummyEntryInfo'/>
+ <Href>http://vmsservices.example.com:8080/VideoService.svc?crid=45541/part=1/guid=ae968b5d-e4a5-41fe-9b23-ed631b27cd21/</Href>
+ </DummyEntry>
+ </Entries>
+</DummyPlaylist>
+";
+ var reader = XmlReader.Create (new StringReader (xml));
+ DummyPlaylist playlist = (DummyPlaylist) new DataContractSerializer (typeof (DummyPlaylist)).ReadObject (reader);
+ Assert.AreEqual (1, playlist.entries.Count, "#1");
+ Assert.IsTrue (playlist.entries [0] is DummyEntry, "#2");
+ Assert.IsNotNull (playlist.entries [0].Href, "#3");
+ }
+
+ [Test]
+ public void BaseKnownTypeAttributes ()
+ {
+ // bug #524088
+ string xml = @"
+<DummyPlaylist xmlns='http://example.com/schemas/asx'>
+ <Entries>
+ <DummyEntry>
+ <EntryInfo xmlns:i='http://www.w3.org/2001/XMLSchema-instance' i:type='PartDummyEntryInfo'/>
+ </DummyEntry>
+ </Entries>
+</DummyPlaylist>";
+
+ using (XmlReader reader = XmlReader.Create (new StringReader (xml))) {
+ DummyPlaylist playlist = new DataContractSerializer(typeof(DummyPlaylist)).ReadObject(reader) as DummyPlaylist;
+ Assert.IsNotNull (playlist);
+ }
+ }
+
+ [Test]
+ public void Bug524083 ()
+ {
+ string xml = @"
+<AsxEntryInfo xmlns='http://example.com/schemas/asx'>
+ <AdvertPrompt/>
+</AsxEntryInfo>";
+
+ using (XmlReader reader = XmlReader.Create (new StringReader (xml)))
+ new DataContractSerializer(typeof (AsxEntryInfo)).ReadObject (reader);
+ }
+
+ [Test]
+ public void Bug539563 ()
+ {
+ new DataContractSerializer (typeof (NestedContractType));
+ }
+
+ [Test]
+ public void Bug560155 ()
+ {
+ var g = Guid.NewGuid ();
+ Person p1 = new Person ("UserName", g);
+ Assert.AreEqual ("name=UserName,id=" + g, p1.ToString (), "#1");
+ MemoryStream memStream = new MemoryStream ();
+ DataContractSerializer ser = new DataContractSerializer (typeof (Person));
+
+ ser.WriteObject (memStream, p1);
+ memStream.Seek (0, SeekOrigin.Begin);
+ Person p2 = (Person) ser.ReadObject (memStream);
+ Assert.AreEqual ("name=UserName,id=" + g, p2.ToString (), "#1");
+ }
+
private T Deserialize<T> (string xml)
{
return Deserialize<T> (xml, typeof (T));
Assert.AreEqual (runtimeType, o.GetType (), "#DS0");
return (T)o;
}
- }
-
+
+ public Dictionary<string, object> GenericDictionary (Dictionary<string, object> settings)
+ {
+ using (MemoryStream ms = new MemoryStream ()) {
+ DataContractSerializer save = new DataContractSerializer (settings.GetType ());
+ save.WriteObject (ms, settings);
+
+ ms.Position = 0;
+
+ DataContractSerializer load = new DataContractSerializer (typeof (Dictionary<string, object>));
+ return (Dictionary<string, object>) load.ReadObject (ms);
+ }
+ }
+
+ [Test]
+ public void GenericDictionaryEmpty ()
+ {
+ Dictionary<string, object> in_settings = new Dictionary<string, object> ();
+ Dictionary<string, object> out_settings = GenericDictionary (in_settings);
+ out_settings.Clear ();
+ }
+
+ [Test]
+ public void GenericDictionaryOneElement ()
+ {
+ Dictionary<string, object> in_settings = new Dictionary<string, object> ();
+ in_settings.Add ("one", "ONE");
+ Dictionary<string, object> out_settings = GenericDictionary (in_settings);
+ Assert.AreEqual ("ONE", out_settings ["one"], "out");
+ out_settings.Clear ();
+ }
+
+ [Test]
+ public void IgnoreDataMember ()
+ {
+ var ser = new DataContractSerializer (typeof (MemberIgnored));
+ var sw = new StringWriter ();
+ using (var w = XmlWriter.Create (sw, settings)) {
+ ser.WriteObject (w, new MemberIgnored ());
+ }
+ Assert.AreEqual (@"<MemberIgnored xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><body><Bar>bar</Bar></body></MemberIgnored>", sw.ToString (), "#1");
+ }
+
+ [Test]
+ public void DeserializeEmptyArray ()
+ {
+ var ds = new DataContractSerializer (typeof (string []));
+ var sw = new StringWriter ();
+ var xw = XmlWriter.Create (sw);
+ ds.WriteObject (xw, new string [] {});
+ xw.Close ();
+ Console.WriteLine (sw.ToString ());
+ var sr = new StringReader (sw.ToString ());
+ var xr = XmlReader.Create (sr);
+ var ret = ds.ReadObject (xr);
+ Assert.AreEqual (typeof (string []), ret.GetType (), "#1");
+ }
+
+ [Test]
+ public void ContractNamespaceAttribute ()
+ {
+ var ds = new DataContractSerializer (typeof (U2U.DataContracts.Person));
+ string xml = "<?xml version='1.0' encoding='utf-16'?><Person xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://www.u2u.be/samples/wcf/2009'><Name>Rupert</Name><Occupation><Description>Monkey</Description></Occupation></Person>";
+ var person = new U2U.DataContracts.Person () {
+ Name = "Rupert",
+ Occupation = new U2U.DataContracts.Job () { Description = "Monkey" }
+ };
+ var sw = new StringWriter ();
+ using (var xw = XmlWriter.Create (sw))
+ ds.WriteObject (xw, person);
+ Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
+ }
+
+ [Test]
+ public void Bug610036 ()
+ {
+ var ms = new MemoryStream ();
+ Type [] knownTypes = new Type [] { typeof (ParentClass), typeof (Foo), typeof (Bar) };
+
+ var ds = new DataContractSerializer (typeof (Root), "Root", "Company.Foo", knownTypes, 1000, false, true, null);
+
+ var root = new Root ("root");
+ var bar1 = new Bar ("bar1");
+ var bar2 = new Bar ("bar2");
+ var bar3 = new Bar ("bar3");
+
+ var foo1 = new Foo ("foo1");
+ var foo2 = new Foo ("foo2");
+
+ foo1.FDict.Add (bar1);
+ foo1.FDict.Add (bar2);
+
+ foo2.FDict.Add (bar1);
+ foo2.FDict.Add (bar3);
+
+ root.FDict.Add (foo1);
+ root.FDict.Add (foo2);
+
+ ds.WriteObject (ms, root);
+ string result = Encoding.UTF8.GetString (ms.ToArray ());
+ ms.Position = 0;
+
+ root = (Root) ds.ReadObject (ms);
+
+ Assert.AreEqual (2, root.FDict.Count, "#1");
+ int idx = result.IndexOf ("foo1");
+ Assert.IsTrue (idx >= 0, "#2");
+ // since "foo1" is stored as z:Ref for string, it must not occur twice.
+ int idx2 = result.IndexOf ("foo1", idx + 1);
+ Assert.IsTrue (idx2 < 0, "idx2 should not occur at " + idx2);
+ }
+
+ [Test]
+ public void AncestralReference ()
+ {
+ // Reference to Parent comes inside the Parent itself.
+ // In this case, adding reference after complete deserialization won't work (but it should).
+ var ms = new MemoryStream ();
+ Type [] knownTypes = new Type [] { typeof (ParentClass), typeof (Foo), typeof (Bar) };
+
+ var ds = new DataContractSerializer (typeof (Parent));
+
+ var org = new Parent ();
+ ds.WriteObject (ms, org);
+ string result = Encoding.UTF8.GetString (ms.ToArray ());
+ ms.Position = 0;
+
+ var parent = (Parent) ds.ReadObject (ms);
+
+ Assert.IsNotNull (parent.Child, "#1");
+ Assert.AreEqual (parent, parent.Child.Parent, "#2");
+ }
+
+ [Test]
+ public void IXmlSerializableCallConstructor ()
+ {
+ IXmlSerializableCallConstructor (false);
+ IXmlSerializableCallConstructor (true);
+ }
+
+ void IXmlSerializableCallConstructor (bool binary)
+ {
+ Stream s = IXmlSerializableCallConstructorSerialize (binary);
+ var a = new byte [s.Length];
+ s.Position = 0;
+ s.Read (a, 0, a.Length);
+ s.Position = 0;
+ IXmlSerializableCallConstructorDeserialize (s, binary);
+ }
+
+ public Stream IXmlSerializableCallConstructorSerialize (bool binary)
+ {
+ var ds = new DataSet ("ds");
+ var dt = new DataTable ("dt");
+ ds.Tables.Add (dt);
+ dt.Columns.Add ("n", typeof (int));
+ dt.Columns.Add ("s", typeof (string));
+
+ dt.Rows.Add (5, "five");
+ dt.Rows.Add (10, "ten");
+
+ ds.AcceptChanges ();
+
+ var s = new MemoryStream ();
+
+ var w = binary ? XmlDictionaryWriter.CreateBinaryWriter (s) : XmlDictionaryWriter.CreateTextWriter (s);
+
+ var x = new DataContractSerializer (typeof (DataSet));
+ x.WriteObject (w, ds);
+ w.Flush ();
+
+ return s;
+ }
+
+ public void IXmlSerializableCallConstructorDeserialize (Stream s, bool binary)
+ {
+ var r = binary ? XmlDictionaryReader.CreateBinaryReader (s, XmlDictionaryReaderQuotas.Max)
+ : XmlDictionaryReader.CreateTextReader (s, XmlDictionaryReaderQuotas.Max);
+
+ var x = new DataContractSerializer (typeof (DataSet));
+
+ var ds = (DataSet) x.ReadObject (r);
+ }
+
+ [Test]
+ [ExpectedException (typeof (InvalidDataContractException))] // BaseConstraintType1 is neither DataContract nor Serializable.
+ public void BaseConstraint1 ()
+ {
+ new DataContractSerializer (typeof (BaseConstraintType3)).WriteObject (XmlWriter.Create (TextWriter.Null), new BaseConstraintType3 ());
+ }
+
+ [Test]
+ public void BaseConstraint2 ()
+ {
+ new DataContractSerializer (typeof (BaseConstraintType4)).WriteObject (XmlWriter.Create (TextWriter.Null), new BaseConstraintType4 ());
+ }
+ }
+
+ [DataContract]
+ public class MemberIgnored
+ {
+ [DataMember]
+ MemberIgnoredBody body = new MemberIgnoredBody ();
+ }
+
+ public class MemberIgnoredBody
+ {
+ [IgnoreDataMember]
+ public string Foo = "foo";
+
+ public string Bar = "bar";
+ }
+
public enum Colors {
Red, Green, Blue
}
+ [Flags]
+ public enum Colors2 {
+ Red, Green, Blue
+ }
+
[DataContract (Name = "_ColorsWithDC")]
public enum ColorsWithDC {
public string Foo = "TEST";
}
+ [DataContract (Namespace = "")]
+ public class DCEmptyNoNS
+ {
+ }
+
[DataContract]
public class DCSimple1
{
public class SimpleSer1
{
public string Doh = "doh!";
+ [NonSerialized]
+ public string Bah = "bah!";
}
public class Wrapper
class SerializeNonDCArrayType
{
[DataMember]
- public IPAddress [] IPAddresses = new IPAddress [0];
+ public NonDCItem [] IPAddresses = new NonDCItem [0];
+ }
+
+ public class NonDCItem
+ {
+ public int [] Data { get; set; }
}
[DataContract]
[DataMember]
public string F = "x";
}
+
+ public class MyList : IList<string>
+ {
+ List<string> l = new List<string> ();
+ public void Clear () { l.Clear (); }
+ public void Add(string s) { l.Add (s);}
+ public void Insert(int idx, string s) { l.Insert(idx,s);}
+ public bool Contains(string s) { return l.Contains(s); }
+ public IEnumerator<string> GetEnumerator () { return l.GetEnumerator (); }
+ IEnumerator IEnumerable.GetEnumerator () { return l.GetEnumerator (); }
+ public bool Remove(string s) { return l.Remove(s); }
+ public void RemoveAt(int i) { l.RemoveAt (i);}
+ public void CopyTo (string [] arr, int index) { l.CopyTo (arr, index);}
+ public int IndexOf (string s) { return l.IndexOf (s); }
+
+ public int Count { get { return l.Count; } }
+ public bool IsReadOnly { get { return ((IList<string>) l).IsReadOnly; } }
+ public string this [int index] { get { return l [index]; } set { l [index] = value; } }
+ }
+
+ [DataContract]
+ internal class InterfaceCollectionType
+ {
+ [DataMember]
+ public IList<int> Array { get; set; }
+ }
+
+ [DataContract]
+ public class NestedContractType
+ {
+ [DataMember]
+ public NestedContractType Nested;
+ [DataMember]
+ public string X = "x";
+ }
+
+ class BaseConstraintType1 // non-serializable
+ {
+ }
+
+ [Serializable]
+ class BaseConstraintType2
+ {
+ }
+
+ [DataContract]
+ class BaseConstraintType3 : BaseConstraintType1
+ {
+ }
+
+ [DataContract]
+ class BaseConstraintType4 : BaseConstraintType2
+ {
+ }
}
[DataContract]
class GlobalSample1
{
}
+
+[DataContract]
+class Foo<X,Y,Z>
+{
+ [DataMember]
+ public X Field;
+}
+
+[CollectionDataContract (Name = "NAME", Namespace = "urn:foo", ItemName = "ITEM", KeyName = "KEY", ValueName = "VALUE")]
+public class MyDictionary<K,V> : Dictionary<K,V>
+{
+}
+
+// bug #524086
+[DataContract(Namespace="http://example.com/schemas/asx")]
+public class DummyEntry
+{
+ [DataMember]
+ public DummyEntryInfo EntryInfo { get; set; }
+ [DataMember]
+ public string Href { get; set; }
+}
+
+[DataContract(Namespace="http://example.com/schemas/asx"),
+KnownType(typeof(PartDummyEntryInfo))]
+public abstract class DummyEntryInfo
+{
+}
+
+[DataContract(Namespace="http://example.com/schemas/asx")]
+public class DummyPlaylist
+{
+ public IList<DummyEntry> entries = new List<DummyEntry> ();
+
+ [DataMember]
+ public IList<DummyEntry> Entries { get { return entries; } set {entries = value;} }
+}
+
+[DataContract(Namespace="http://example.com/schemas/asx")]
+public class PartDummyEntryInfo : DummyEntryInfo
+{
+ public PartDummyEntryInfo() {}
+}
+
+// bug #524088
+
+[DataContract(Namespace="http://example.com/schemas/asx")]
+public class AsxEntryInfo
+{
+ [DataMember]
+ public string AdvertPrompt { get; set; }
+}
+
+// bug #560155
+
+[DataContract]
+public class Person
+{
+ [DataMember]
+ readonly public string name;
+ [DataMember]
+ readonly public Guid Id = Guid.Empty;
+
+ public Person (string nameIn, Guid idIn)
+ {
+ name = nameIn;
+ Id = idIn;
+ }
+
+ public override string ToString()
+ {
+ return string.Format ("name={0},id={1}", name, Id);
+ }
+}
+
+// bug #599889
+namespace U2U.DataContracts
+{
+ [DataContract]
+ public class Person
+ {
+ [DataMember]
+ public string Name { get; set; }
+
+ [DataMember]
+ public Job Occupation { get; set; }
+ }
+
+ [DataContract]
+ public class Job
+ {
+ [DataMember]
+ public string Description { get; set; }
+ }
+}
+
+#region bug #610036
+//parent class with a name property
+[DataContract (Namespace = "Company.Foo")]
+public abstract class ParentClass
+{
+
+ //constructor
+ public ParentClass (string name)
+ {
+ Name = name;
+ }
+
+ //the name
+ [DataMember]
+ public string Name{ get; set; }
+
+}
+
+//root object
+[DataContract (Namespace = "Company.Foo")]
+public class Root : ParentClass
+{
+ //dict
+ [DataMember]
+ public Dict<Foo> FDict;
+
+ //constructor
+ public Root (string name)
+ : base (name)
+ {
+ FDict = new Dict<Foo> ();
+ }
+}
+
+
+//subclass
+[DataContract (Namespace = "Company.Foo")]
+public class Foo : ParentClass
+{
+ //here is one dict
+ [DataMember]
+ public Dict<Bar> FDict;
+
+ //constructor
+ public Foo (string name)
+ : base (name)
+ {
+ FDict = new Dict<Bar> ();
+ }
+
+}
+
+//another sublass
+[DataContract (Namespace = "Company.Foo")]
+public class Bar : ParentClass
+{
+ //constructor
+ public Bar (string name)
+ : base (name)
+ {
+ }
+
+}
+//the custom dictionary
+[CollectionDataContract (ItemName = "DictItem", Namespace = "Company.Foo")]
+public class Dict<T> : Dictionary<string, T> where T : ParentClass
+{
+ public void Add (T item)
+ {
+ Add (item.Name, item);
+ }
+
+}
+
+[DataContract (IsReference = true)]
+public class Parent
+{
+ //constructor
+ public Parent ()
+ {
+ Child = new Child (this);
+ }
+
+ [DataMember]
+ public Child Child;
+}
+
+[DataContract]
+public class Child
+{
+ public Child ()
+ {
+ }
+
+ public Child (Parent parent)
+ {
+ this.Parent = parent;
+ }
+
+ [DataMember]
+ public Parent Parent;
+}
+
+#endregion