コード例 #1
0
        public void TestArraySurrogate()
        {
            var x = new int[] { 1, 3, 5, 7, 9 };

            var c = new CSerializationContext();

            c.SetConcise();
            var helper = new CIntArraySurrogate();

            c.RegisterExternalSurrogate(typeof(int[]), helper);

            var s   = new CSerializer(c);
            var doc = s.Serialize(x);

            Console.WriteLine("Depth of resulting XML: " + XmlExtensions.Depth(doc));
            Console.WriteLine("Length of resulting XML String: " + doc.OuterXml.Length);
            Console.WriteLine("Number of resulting XmlElements: " + XmlExtensions.ElementCount(doc));
            Print(doc);

            var d = new CDeserializer(c);
            var y = (int[])d.Deserialize(doc);

            Assert.AreEqual(x.Length, y.Length - 1, "Length of resulting array is wrong");
            for (var i = 0; i < x.Length; i++)
            {
                Assert.AreEqual(x[i], y[i], "Invalid element at: " + i);
            }
        }
コード例 #2
0
        public void TestGenericDerived()
        {
            var src = new CPersonWithObject <CAddress>()
            {
                SomeObject = new CAddress()
                {
                    m_city   = "Spring",
                    m_street = "Halifax",
                    m_zip    = 37174
                }
            };

            var s   = new CSerializer();
            var xml = s.Serialize(src);

            Print(xml);



            var d    = new CDeserializer();
            var dest = d.Deserialize(xml) as CPersonWithObject <CAddress>;

            Assert.AreEqual(src.m_name, dest.m_name, "Name");
            Assert.AreEqual(src.SomeObject.m_street, dest.SomeObject.m_street, "Generic Street");
            Assert.AreEqual(src.m_address.m_zip, dest.m_address.m_zip, "Zip");
            Assert.AreNotEqual(src.SomeObject, dest.SomeObject);
        }
コード例 #3
0
        public void TestExternalSurrogate()
        {
            CBigPerson.GenerateData(100);
            var c = new CSerializationContext();

            c.SetConcise();
            var helper = new CFriendSerializer();

            c.RegisterExternalSurrogate(typeof(CFriend), helper);

            var s   = new CSerializer(c);
            var doc = s.Serialize(CBigPerson.People);

            Console.WriteLine("Depth of resulting XML: " + XmlExtensions.Depth(doc));
            Console.WriteLine("Length of resulting XML String: " + doc.OuterXml.Length);
            Console.WriteLine("Number of resulting XmlElements: " + XmlExtensions.ElementCount(doc));
            Print(doc);

            var d  = new CDeserializer(c);
            var x2 = d.Deserialize <CBigPerson[]>(doc);

            helper.FinishDeserializing(x2);

            CDeserializeTest.AssertEqualBigPeopleArray(CBigPerson.People, x2);
        }
コード例 #4
0
        public void TestFieldRenamer()
        {
            var c = new CSerializationContext
            {
                FieldRenamer = new CFieldRenamer()
            };

            var s   = new CSerializer(c);
            var add = new CAddress();

            var doc = s.Serialize(add);

            Print(doc);

            var root = doc.DocumentElement;

            TestSingleRenamedField(add.m_zip, root["INT_Zip"]);
            TestSingleRenamedField(add.m_city, root["STRING_City"]);
            TestSingleRenamedField(add.m_street, root["STRING_Street"]);


            var d   = new CDeserializer(c);
            var ad2 = d.Deserialize <CAddress>(doc);

            Assert.AreEqual(add.m_city, ad2.m_city, "City");
            Assert.AreEqual(add.m_street, ad2.m_street, "Street");
            Assert.AreEqual(add.m_zip, ad2.m_zip, "Zip");
        }
コード例 #5
0
        public void TestList()
        {
            var names = new List <string>
            {
                "Homer",
                "Marge",
                "Bart",
                "Lisa",
                "Maggie"
            };

            var c = new CSerializationContext();

            c.SetVerbose();
            var s   = new CSerializer(c);
            var doc = s.Serialize(names);

            Print(doc);

            var d      = new CDeserializer(c);
            var names2 = d.Deserialize <List <string> >(doc);

            Assert.AreEqual(names.Count, names2.Count, "The number of list elements is wrong");
            for (var i = 0; i < names.Count; i++)
            {
                Assert.AreEqual(names[i], names2[i], "The name is wrong at index " + i);
            }
        }
コード例 #6
0
        public void TestLinkedList()
        {
            var list = new LinkedList <int>();

            for (var i = 1; i < 1000000; i = (int)((i + 1) * 1.5))
            {
                list.AddLast(i);
            }

            var s   = new CSerializer();
            var doc = s.Serialize(list);

            Print(doc);

            var d     = new CDeserializer();
            var list2 = d.Deserialize(doc) as LinkedList <int>;

            Assert.AreEqual(list.Count, list2.Count, "Number of resulting elements is wrong.");

            var ptr = list2.First;

            foreach (var x in list)
            {
                Assert.AreEqual(x, ptr.Value, "The deserialized value is wrong for initial value: " + x);
                ptr = ptr.Next;
            }
        }
コード例 #7
0
        public void TestSortedDictionary()
        {
            var names = GenerateRandomNames();
            var dict  = new SortedDictionary <string, CAddress>();

            for (var i = 0; i < 26; i++)
            {
                dict[names[i]] = CAddress.Get();
            }

            var s   = new CSerializer();
            var doc = s.Serialize(dict);

            Print(doc);

            var d  = new CDeserializer();
            var d2 = (SortedDictionary <string, CAddress>)d.Deserialize(doc);

            Assert.AreEqual(dict.Count, d2.Count, "Size of resulting dictionary is wrong");
            Assert.AreEqual(dict.Count, doc.DocumentElement.ChildNodes.Count, "The number of XmlNodes for the collection is wrong");

            foreach (var key in dict.Keys)
            {
                CompareCAddresses(dict[key], d2[key]);
            }
        }
コード例 #8
0
        public void TestDictionary()
        {
            var x = new Dictionary <int, string>
            {
                [4]     = "hello",
                [55]    = "Katie",
                [15834] = "=)",
                [324]   = "Homer",
                [-87]   = "Simpson"
            };

            var c = new CSerializationContext();

            c.SetConcise();
            var s   = new CSerializer(c);
            var doc = s.Serialize(x);

            Print(doc);

            var d = new CDeserializer(c);
            var y = (Dictionary <int, string>)d.Deserialize(doc);


            Assert.AreEqual(x.Count, y.Count, "Size of resulting hashtable is wrong");
            foreach (var key in x.Keys)
            {
                Assert.AreEqual(x[key], y[key], "Entry at key " + key + " was wrong.");
            }
        }
コード例 #9
0
        public void TestStack()
        {
            var q = new Stack <CAddress>();

            for (var i = 1; i < 11; i++)
            {
                q.Push(CAddress.Get());
            }

            var s   = new CSerializer();
            var doc = s.Serialize(q);

            Print(doc);

            var d  = new CDeserializer();
            var q2 = (Stack <CAddress>)d.Deserialize(doc);

            Assert.AreEqual(q.Count, q2.Count, "Number of resulting elements is wrong.");
            Assert.AreEqual(q.Count,
                            doc.DocumentElement.ChildNodes.Count,
                            "The number of child nodes does not equal the number of elements in the Collection.");

            while (q.Count > 0)
            {
                var a1 = q.Pop();
                var a2 = q2.Pop();
                CompareCAddresses(a1, a2);
            }
        }
コード例 #10
0
        public void TestStringCounter()
        {
            var x = new CTypeCounter();

            var c = new CSerializationContext();

            c.SetConcise();
            c.RegisterExternalSurrogate(typeof(string), x);

            var s   = new CSerializer(c);
            var doc = s.Serialize(x);

            Assert.AreEqual(3, x.Count, "There should have been 3 strings counted.");

            Console.WriteLine("Depth of resulting XML: " + XmlExtensions.Depth(doc));
            Console.WriteLine("Length of resulting XML String: " + doc.OuterXml.Length);
            Console.WriteLine("Number of resulting XmlElements: " + XmlExtensions.ElementCount(doc));
            Print(doc);

            var d = new CDeserializer(c);
            var y = d.Deserialize <CTypeCounter>(doc);

            Assert.AreEqual(0, y.Count, "The new object should have no counted strings");
            Assert.AreEqual(6, x.Count, "The initial object should have strings counted for 2 actions");
        }
コード例 #11
0
        public void TestAllArrayLists()
        {
            CClassWithArrayLists ca = new CClassWithArrayLists();

            ca.SimpleArrayList   = MakeSampleArrayList();
            ca.ReadOnlyArrayList = ArrayList.ReadOnly(ca.SimpleArrayList);
            // This is really bad because we shouldn't really hold the ref to the "old" object
            // after its been wrapped
            ca.SyncArrayList = ArrayList.Synchronized(ca.SimpleArrayList);
            // This is really bad because we shouldn't really hold the ref to the "old" object
            // after its been wrapped

            Assert.AreNotEqual(ca.SimpleArrayList.GetType(),
                               ca.ReadOnlyArrayList.GetType(),
                               "The Type of the ArrayList is the SAME as the ReadOnly ArrayList.");
            Assert.AreNotEqual(ca.SimpleArrayList.GetType(),
                               ca.SyncArrayList.GetType(),
                               "The Type of the ArrayList is the SAME as the SyncArrayList.");
            Assert.AreNotEqual(ca.SyncArrayList.GetType(),
                               ca.ReadOnlyArrayList.GetType(),
                               "The Type of the SyncArrayList is the SAME as the ReadOnly ArrayList.");

            Console.WriteLine(ca.SimpleArrayList.GetType().AssemblyQualifiedName);
            Console.WriteLine(ca.ReadOnlyArrayList.GetType().AssemblyQualifiedName);
            Console.WriteLine(ca.SyncArrayList.GetType().AssemblyQualifiedName);

            CSerializationContext.Global.UseFullUtcDateTimeStrings = true;
            CSerializer s   = new CSerializer();
            XmlDocument doc = s.Serialize(ca);

            Print(doc);

            CDeserializer        d   = new CDeserializer();
            CClassWithArrayLists ca2 = d.Deserialize <CClassWithArrayLists>(doc);

            Assert.AreEqual(ca.SimpleArrayList.GetType(),
                            ca2.SimpleArrayList.GetType(),
                            "The Type of the resulting ArrayList is different.");
            Assert.AreEqual(ca.ReadOnlyArrayList.GetType(),
                            ca2.ReadOnlyArrayList.GetType(),
                            "The Type of the ReadOnly array is different");
            Assert.AreEqual(ca.SyncArrayList.GetType(),
                            ca2.SyncArrayList.GetType(),
                            "The Type of the Sync array is different");

            Assert.AreNotEqual(ca2.SimpleArrayList.GetType(),
                               ca2.ReadOnlyArrayList.GetType(),
                               "The Type of the ArrayList is the SAME as the ReadOnly ArrayList.");
            Assert.AreNotEqual(ca2.SimpleArrayList.GetType(),
                               ca2.SyncArrayList.GetType(),
                               "The Type of the ArrayList is the SAME as the SyncArrayList.");
            Assert.AreNotEqual(ca2.SyncArrayList.GetType(),
                               ca2.ReadOnlyArrayList.GetType(),
                               "The Type of the SyncArrayList is the SAME as the ReadOnly ArrayList.");

            VerifyArrayListContents(ca.SimpleArrayList, ca2.SimpleArrayList);
            VerifyArrayListContents(ca.ReadOnlyArrayList, ca2.ReadOnlyArrayList);
            VerifyArrayListContents(ca.SyncArrayList, ca2.SyncArrayList);
        }
コード例 #12
0
        public void TestNullDeserialize()
        {
            var s   = new CDeserializer();
            var doc = new XmlDocument();
            var o   = s.Deserialize(doc);

            Assert.IsNull(o, "An XmlDocument with nothing in it should return nothing");
        }
コード例 #13
0
        public void TestCondensedArrayWithNonPrimitive()
        {
            var s   = new CDeserializer();
            var doc = new XmlDocument();

            doc.LoadXml("<root _T='Morpheus.Standard.UnitTests.Serialization.CPerson[]'>1,2,3,4,5</root>");

            var x = (CPerson[])s.Deserialize(doc);
        }
コード例 #14
0
        public void TestDeserializeNoType()
        {
            var s   = new CDeserializer();
            var doc = new XmlDocument();

            doc.LoadXml("<root>45</root>");

            s.Deserialize(doc);
        }
コード例 #15
0
        public void TestNullableValue()
        {
            int?        x, y;
            var         s = new CSerializer();
            var         d = new CDeserializer();
            XmlDocument doc;

            x   = 5;
            doc = s.Serialize(x);
            Print(doc);
            y = d.Deserialize <int?>(doc);
            Console.WriteLine("Deserialized: {0}", (y == null) ? "<null>" : y.ToString());

            x   = null;
            doc = s.Serialize(x);
            Print(doc);
            y = d.Deserialize <int?>(doc);
            Console.WriteLine("Deserialized: {0}", (y == null) ? "<null>" : y.ToString());
        }
コード例 #16
0
        public void TestHandwrittenList()
        {
            var xml =
                @"
<_>
    <Name>Homer</Name>
    <Addresses>
        <_><Street>Schroeder Way</Street><City>Sparks</City><Zip>89431</Zip></_>
        <_><Street>Shadow Lane</Street><City>Sparks</City><Zip>89434</Zip></_>
        <_><Street>Lynnfield Court</Street><City>Reno</City><Zip>89509</Zip></_>
        <_ type='Morpheus.Standard.UnitTests.Serialization.CSuperAddress'><Country>Australia</Country><Street>Coast Ave</Street><City>Cronulla</City><Zip>2020</Zip></_>
        <_><Street>Plateau Road</Street><City>Reno</City><Zip>89519</Zip></_>
    </Addresses>
</_>";
            var doc = new XmlDocument();

            doc.LoadXml(xml);
            Print(doc);

            var c = new CSerializationContext
            {
                TypeAttributeName = "type",
                FixM_             = true,
                ArrayElementName  = "_"
            };
            var d = new CDeserializer(c);

            var cwl = d.Deserialize <CClassWithIList>(doc);

            Assert.AreEqual("Homer", cwl.Name, "Name is wrong");
            Assert.AreEqual(5, cwl.Addresses.Count, "Number of addresses is wrong");

            Assert.AreEqual("Schroeder Way", cwl.Addresses[0].m_street, "[0]- Street");
            Assert.AreEqual("Sparks", cwl.Addresses[0].m_city, "[0]- City");
            Assert.AreEqual(89431, cwl.Addresses[0].m_zip, "[0]- Zip");

            Assert.AreEqual("Shadow Lane", cwl.Addresses[1].m_street, "[1]- Street");
            Assert.AreEqual("Sparks", cwl.Addresses[1].m_city, "[1]- City");
            Assert.AreEqual(89434, cwl.Addresses[1].m_zip, "[1]- Zip");

            Assert.AreEqual("Lynnfield Court", cwl.Addresses[2].m_street, "[2]- Street");
            Assert.AreEqual("Reno", cwl.Addresses[2].m_city, "[2]- City");
            Assert.AreEqual(89509, cwl.Addresses[2].m_zip, "[2]- Zip");

            Assert.AreEqual("Coast Ave", cwl.Addresses[3].m_street, "[3]- Street");
            Assert.AreEqual("Cronulla", cwl.Addresses[3].m_city, "[3]- City");
            Assert.AreEqual(2020, cwl.Addresses[3].m_zip, "[3]- Zip");
            var sa = (CSuperAddress)cwl.Addresses[3];

            Assert.AreEqual("Australia", sa.m_country, "[3]- Country");

            Assert.AreEqual("Plateau Road", cwl.Addresses[4].m_street, "[4]- Street");
            Assert.AreEqual("Reno", cwl.Addresses[4].m_city, "[4]- City");
            Assert.AreEqual(89519, cwl.Addresses[4].m_zip, "[4]- Zip");
        }
コード例 #17
0
        public void TestGenericDeserialize()
        {
            var s   = new CDeserializer();
            var doc = new XmlDocument();

            doc.LoadXml("<root>45</root>");

            var x = s.Deserialize <int>(doc);

            Assert.AreEqual(45, x, "Deserialization failed for GenericFunction call");
        }
コード例 #18
0
        public void TestDeserializeNullAttribute()
        {
            var s   = new CDeserializer();
            var doc = new XmlDocument();

            doc.LoadXml("<root _T='System.String' _N='1'>45</root>");

            var o = s.Deserialize(doc);

            Assert.IsNull(o, "The returned object should be null!");
        }
コード例 #19
0
        public void TestInvalidTypeDeserialization()
        {
            var s   = new CDeserializer();
            var doc = new XmlDocument();

            doc.LoadXml("<root _T='System.IntPtr'>0</root>");

            var x = s.Deserialize(doc);

            Assert.IsNull(x, "Invalid type should deserialize to null");
        }
コード例 #20
0
        public void TestNullableValueError()
        {
            int?x = null;

            var s   = new CSerializer();
            var doc = s.Serialize(x);

            Print(doc);

            var d = new CDeserializer();
            var y = (int)d.Deserialize(doc);    // should throw the error
        }
コード例 #21
0
        public void TestImplicitSurrogate()
        {
            var s   = new CDeserializer();
            var doc = new XmlDocument();

            doc.LoadXml("<root _T='Morpheus.Standard.UnitTests.Serialization.CStdImplicitSurrogate' NAME='Michael' AGE='22'/>");

            var x = s.Deserialize <CStdBaseObject>(doc);

            Assert.AreEqual("Michael", x.Name, "Name is wrong");
            Assert.AreEqual(22, x.Age, "Age is wrong");
        }
コード例 #22
0
        public void TestDeserializePrimitive()
        {
            var s   = new CDeserializer();
            var doc = new XmlDocument();

            doc.LoadXml("<root _T='System.Int32'>45</root>");

            var o = s.Deserialize(doc);

            Assert.AreEqual(typeof(int), o.GetType(), "The type of the object returned is wrong");
            Assert.AreEqual(45, (int)o, "The value of the object is wrong");
        }
コード例 #23
0
        public void TestDeserializeString()
        {
            var s   = new CDeserializer();
            var doc = new XmlDocument();

            doc.LoadXml("<root _T='System.String'>45</root>");

            var o = s.Deserialize(doc);

            Assert.AreEqual(typeof(string), o.GetType(), "The type of the object returned is wrong");
            Assert.AreEqual("45", o.ToString(), "The value of the object is wrong");
        }
コード例 #24
0
        public void TestCondensedPrimitiveArray()
        {
            var s   = new CDeserializer();
            var doc = new XmlDocument();

            doc.LoadXml("<root _T='System.Int32[]'>1,2,3,4,5</root>");

            var x = (int[])s.Deserialize(doc);

            Assert.AreEqual(5, x.Length, "Length of resulting array is wrong");
            for (var i = 0; i < x.Length; i++)
            {
                Assert.AreEqual(i + 1, x[i], "Element " + i + " is wrong");
            }
        }
コード例 #25
0
        public void TestCondensedStringArray()
        {
            var s   = new CDeserializer();
            var doc = new XmlDocument();

            doc.LoadXml(@"<root _T='System.String[]'>Hello,,to,\_,all</root>");

            var x = (string[])s.Deserialize(doc);

            Assert.AreEqual(5, x.Length, "Length of resulting array is wrong");
            Assert.AreEqual("Hello", x[0], "0");
            Assert.AreEqual(null, x[1], "1");
            Assert.AreEqual("to", x[2], "2");
            Assert.AreEqual("", x[3], "3");
            Assert.AreEqual("all", x[4], "4");
        }
コード例 #26
0
        public void TestExternalSurrogate()
        {
            var c = new CSerializationContext();

            c.RegisterExternalSurrogate(typeof(CStdBaseObject), new CStdExternalSurrogate());

            var s   = new CDeserializer(c);
            var doc = new XmlDocument();

            doc.LoadXml("<root NAME='Mike' AGE='11'/>");

            var x = s.Deserialize <CStdBaseObject>(doc);

            Assert.AreEqual("Mike", x.Name, "Name is wrong");
            Assert.AreEqual(11, x.Age, "Age is wrong");
        }
コード例 #27
0
        public void TestStandardDeserialization()
        {
            var s   = new CDeserializer();
            var doc = new XmlDocument();

            doc.LoadXml(
                "<Babe>                     " +
                "    <Name>Alyssa</Name>    " +
                "    <Age>24</Age>          " +
                "    <Sex>Lots</Sex>        " +
                "</Babe>                    ");

            var x = s.Deserialize <CMySuperStd>(doc);

            Assert.AreEqual("Alyssa", x.Name, "Name is wrong");
            Assert.AreEqual(24, x.Age, "Age is wrong");
            Assert.AreEqual("Lots", x.Sex, "Sex is wrong");
        }
コード例 #28
0
        public void TestStandardDeserializationBaseSurrogate()
        {
            var c = new CSerializationContext();

            c.RegisterExternalSurrogate(typeof(CStdBaseObject), new CStdExternalSurrogate());
            var s   = new CDeserializer(c);
            var doc = new XmlDocument();

            doc.LoadXml(
                "<Babe NAME='Alyssa' AGE='24'>  " +
                "    <Sex>Lots</Sex>            " +
                "</Babe>                        ");

            var x = s.Deserialize <CMySuperStd>(doc);

            Assert.AreEqual("Alyssa", x.Name, "Name is wrong");
            Assert.AreEqual(24, x.Age, "Age is wrong");
            Assert.AreEqual("Lots", x.Sex, "Sex is wrong");
        }
コード例 #29
0
        public void TestBigPerson()
        {
            var src = new CBigPerson()
            {
                Role = CBigPerson.ERole.Parent
            };

            var ser = new CSerializer();

            var doc = ser.Serialize(src);

            var deser = new CDeserializer();
            var dest  = deser.Deserialize <CBigPerson>(doc);

            Assert.AreEqual(src.Name, dest.Name, "Name");
            Assert.AreEqual(src.Height, dest.Height, "Height");
            Assert.AreEqual(src.IsParent, dest.IsParent, "IsParent");
            Assert.AreEqual(src.Role, dest.Role, "Role");
        }
コード例 #30
0
        public void TestCircularRef()
        {
            var s   = new CDeserializer();
            var doc = new XmlDocument();

            doc.LoadXml(
                "<root _ID='1'>             " +
                "    <Data>44</Data>        " +
                "    <Next>                 " +
                "        <Data>55</Data>    " +
                "        <Next _RID='1'/>   " +
                "    </Next>                " +
                "</root>                    ");

            var x = s.Deserialize <CLinkedList>(doc);

            Assert.AreEqual(44, x.Data, "Head node Data is wrong");
            Assert.AreEqual(55, x.Next.Data, "Head->Next node Data is wrong");
            ReferenceEquals(x, x.Next.Next);
        }