コード例 #1
0
ファイル: CallbackTests.cs プロジェクト: CXuesong/XSerializer
 public void CallbackTest()
 {
     var s = new XSerializer(typeof(MyClass));
     var obj = new MyClass("Callback Test");
     var doc = s.GetSerializedDocument(obj);
     Trace.WriteLine(doc);
     var obj1 = (MyClass)s.Deserialize(doc, null);
     Assert.AreEqual(obj.myString, obj1.myString);
 }
コード例 #2
0
 public void SerializeAPoint()
 {
     var s = new XSerializer(typeof(Point));
     var obj = new Point(123.45, 678.9012);
     var doc = s.GetSerializedDocument(obj);
     Trace.WriteLine(doc);
     var obj1 = (Point)s.Deserialize(doc, null);
     Assert.AreEqual(obj, obj1);
 }
コード例 #3
0
ファイル: B001.cs プロジェクト: CXuesong/XSerializer
 public void B001TestMethod1()
 {
     var s = new XSerializer(typeof(MyList));
     var list = new MyList { 1, 2, 3 };
     var doc = s.GetSerializedDocument(list);
     Trace.WriteLine(doc);
     var list1 = (MyList)s.Deserialize(doc, null);
     Assert.AreEqual(list1.Count, list.Count);
 }
コード例 #4
0
ファイル: B002.cs プロジェクト: CXuesong/XSerializer
 public void B002TestMethod1()
 {
     var s = new XSerializer(typeof(MyObject));
     var obj = new MyObject();
     obj.List.AddRange(new[] { 10, 20, 30 });
     var doc = s.GetSerializedDocument(obj);
     Trace.WriteLine(doc);
     var obj1 = (MyObject)s.Deserialize(doc, null);
     Assert.AreEqual(obj1.List.Count, obj.List.Count);
 }
コード例 #5
0
 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);
 }
コード例 #6
0
ファイル: UnitTest1.cs プロジェクト: CXuesong/XSerializer
 public void B003TestMethod1()
 {
     var s = new XSerializer(typeof(MyClass));
     var obj = new MyClass();
     obj.List.AddRange(new[] { 10, 20, 30 });
     var doc = s.GetSerializedDocument(obj);
     Trace.WriteLine(doc);
     Assert.IsFalse(doc.Elements().DescendantsAndSelf().Any(xe => xe.Name.Namespace != XNamespace.None));
     var obj1 = (MyClass)s.Deserialize(doc, null);
     Assert.AreEqual(obj1.List.Count, obj.List.Count);
 }
コード例 #7
0
 public void ObjectWithReadonlyFieldTest()
 {
     const string containerName = "This Container";
     var s = new XSerializer(typeof(MyObject2));
     var container = new Container("container");
     var obj = new MyObject2();
     obj.ReadonlyContainer.Name = containerName;
     var doc = s.GetSerializedDocument(obj);
     var obj1 = (MyObject2)s.Deserialize(doc, null, new MyObject2());
     Assert.AreEqual(obj1.ReadonlyContainer.Name, containerName);
 }
コード例 #8
0
 //[TestMethod]
 public void IEnumerableTest()
 {
     var s = new XSerializer(typeof(ClassWithEnumerable));
     var obj = new ClassWithEnumerable
     {
         Collection = new object[] {"abc", 123.45},
         StringCollection = new string[] {"abc", "def"}
     };
     var doc = s.GetSerializedDocument(obj);
     Trace.WriteLine(doc);
     var obj1 = (ClassWithEnumerable)s.Deserialize(doc, null);
     Assert.IsTrue(obj.Collection.Cast<object>().SequenceEqual(obj1.Collection.Cast<object>()));
     Assert.IsTrue(obj.StringCollection.SequenceEqual(obj1.StringCollection));
 }
コード例 #9
0
ファイル: Simple1.cs プロジェクト: CXuesong/XSerializer
 public void CircularReferenceTest()
 {
     var s = new XSerializer(typeof(SimpleObject1));
     var obj = new SimpleObject1 { Title = "Simple Object Test" };
     obj.CompositeArray.Add(obj);
     try
     {
         s.GetSerializedDocument(obj);
     }
     catch (Exception ex)
     {
         Trace.WriteLine(ex);
         throw;
     }
 }
コード例 #10
0
 public void AnyElementTest()
 {
     var s = new XSerializer(typeof(MyClass));
     var obj = new MyClass("AnyElement / AnyAttribute Test")
     {
         attr = new[] {new XAttribute("a1", "value1"), new XAttribute("a2", DateTime.UtcNow)}
     };
     obj.Elem.Add(new XElement("customElement", "content1"));
     obj.Elem.Add(new XElement("customElement", 12345));
     obj.Elem.Add(new XElement("customElement", Guid.NewGuid()));
     var doc = s.GetSerializedDocument(obj);
     Trace.WriteLine(doc);
     var obj1 = (MyClass)s.Deserialize(doc, null);
     Assert.AreEqual(obj.myString, obj1.myString);
     Assert.AreEqual(obj.attr.Count, obj1.attr.Count);
     Assert.AreEqual(obj.Elem.Count, obj1.Elem.Count);
 }
コード例 #11
0
ファイル: Simple1.cs プロジェクト: CXuesong/XSerializer
 public void SerializationTest()
 {
     var s = new XSerializer(typeof(SimpleObject1));
     var obj = new SimpleObject1 {Title = "Simple Object Test", ExtraContent = "Extra content here."};
     obj.CompositeArray.Add("Hello, world!");
     obj.CompositeArray.Add(12345.67e89);
     obj.CompositeArray.Add(new SimpleObject1 { Title = "Child Object" });
     var doc = s.GetSerializedDocument(obj);
     Trace.WriteLine(doc);
     var obj1 = (SimpleObject1)s.Deserialize(doc, null);
     Assert.AreEqual(obj.Title, obj1.Title);
     Assert.AreEqual(obj.ExtraContent, obj1.ExtraContent);
     Assert.AreEqual(obj.Time, obj1.Time);
     Assert.AreEqual(obj.CompositeArray.Count, obj1.CompositeArray.Count);
     for (var i = 0; i < obj.CompositeArray.Count; i++)
     {
         if (!(obj.CompositeArray[i] is SimpleObject1))
             Assert.AreEqual(obj.CompositeArray[i], obj1.CompositeArray[i]);
     }
 }
コード例 #12
0
 public void SerializeARectangle()
 {
     var surrogates = new XSerializableSurrogateCollection
     {
         new ColorXSSSurrogate()
     };
     var s = new XSerializer(typeof(Rectangle), null, surrogates);
     var obj = new Rectangle
     {
         Point1 = new Point(123.45, 678.9012),
         Point2 = new Point(350, 850),
         BorderColor = Color.FromArgb(127, 100, 50, 100),
         FillColor = Color.CadetBlue,
         FillMode = FillMode.Alternate
     };
     var doc = s.GetSerializedDocument(obj);
     Trace.WriteLine(doc);
     var obj1 = (Rectangle)s.Deserialize(doc, null);
     Assert.AreEqual(obj.BorderColor, obj1.BorderColor);
     Assert.AreEqual(obj.FillColor, obj1.FillColor);
     Assert.AreEqual(obj.Point1, obj1.Point1);
     Assert.AreEqual(obj.Point2, obj1.Point2);
     Assert.AreEqual(obj.FillMode, obj1.FillMode);
 }
コード例 #13
0
ファイル: UnitTest1.cs プロジェクト: CXuesong/XSerializer
 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;
     }
 }
コード例 #14
0
ファイル: UnitTest1.cs プロジェクト: CXuesong/XSerializer
 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);
 }
コード例 #15
0
ファイル: UnitTest1.cs プロジェクト: CXuesong/XSerializer
 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);
 }