public void ObjectWithoutEmptyConstructorTest() { var s = new XSerializer(typeof (MyObject1)); var container = new Container("container"); var obj = new MyObject1(container) {Name = "My Object 1"}; var doc = s.GetSerializedDocument(obj); Trace.WriteLine(doc); var obj1 = (MyObject1) s.Deserialize(doc, null, new MyObject1(container)); Trace.WriteLine(obj1); }
public void CircularSerializationTest() { var s = new XSerializer(typeof(MyObject1)); var obj = new MyObject1(); obj.AnotherObject = obj; // circular reference try { s.GetSerializedDocument(obj); } catch (Exception ex) { Trace.WriteLine(ex); throw; } }
public void SerializationTest() { var s = new XSerializer(typeof(MyObject1), new[] { typeof(MyObject2) }); var ns = new XSerializerNamespaceCollection { {"n1", MyObject1.MyUri1}, {"n2", MyObject1.MyUri2}, PrefixUriPair.Xsi }; var p = new XSerializerParameters(ns); var obj = new MyObject1 { Property1 = 123e45d, Array1 = new object[] { "abc", 123.4567, "def", new MyObject1(), new MyObject2() }, AnotherObject = new MyObject1(), myObject = new MyObject2() }; //There are intentional spaces left in the strings. obj.List1.Add("越过长城,走向世界。 "); obj.List1.Add("\t\tAcross the Great Wall we can reach every corner in the world."); var doc = s.GetSerializedDocument(obj, p); Trace.WriteLine(doc); var obj1 = (MyObject1)s.Deserialize(doc, null); //Debug.Print("Deserialize Obj : {0}", obj1.GetHashCode()); Assert.AreEqual(obj.Property1, obj1.Property1); Assert.AreEqual(obj.Property1, obj1.Property1); Assert.AreEqual(obj.List1.Count, obj1.List1.Count); Assert.AreEqual(obj.List1[0], obj1.List1[0]); Assert.AreEqual(obj.List1[1], obj1.List1[1]); Assert.AreEqual(obj.Array1.Length, obj1.Array1.Length); }
public void SerializationProfiling() { const int repetitions = 1000; var s = new XSerializer(typeof(MyObject1)); var ns = new XSerializerNamespaceCollection { {"n1", MyObject1.MyUri1}, {"n2", MyObject1.MyUri2}, PrefixUriPair.Xsi }; var p = new XSerializerParameters(ns); var obj = new MyObject1 { Property1 = 123e45d, Array1 = new object[] { "abc", 123.4567, "def", new MyObject1() }, AnotherObject = new MyObject1() }; //There are intentional spaces left in the strings. obj.List1.Add("越过长城,走向世界。 "); obj.List1.Add("\t\tAcross the Great Wall we can reach every corner in the world."); var doc = s.GetSerializedDocument(obj, p); Trace.WriteLine(doc); var obj1 = (MyObject1)s.Deserialize(doc, null); // Profiling for (var i = 0; i < obj.Array1.Length; i++) { if (obj.Array1[i].GetType().IsValueType) Assert.AreEqual(obj.Array1[i], obj1.Array1[i]); } var sw = Stopwatch.StartNew(); for (int i = 0; i < repetitions; i++) { // ReSharper disable once ReturnValueOfPureMethodIsNotUsed s.GetSerializedDocument(obj, p).ToString(); } Trace.Write("Serialization elapsed ms : "); Trace.WriteLine(sw.Elapsed.TotalMilliseconds / repetitions); sw.Restart(); for (int i = 0; i < repetitions; i++) { s.Deserialize(doc, null); } Trace.Write("Deserialization elapsed ms : "); Trace.WriteLine(sw.Elapsed.TotalMilliseconds / repetitions); }