コード例 #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
        /// <summary>
        /// Deserialization helper to deserialize XML nodes into a "special" collection.
        /// </summary>
        /// <remarks>
        /// A "special" collection is one of the "hidden" collections that implement Synchronized or ReadOnly
        /// behavior for the System.Collections collections.
        /// </remarks>
        /// <param name="_object">The working object to receive the deserialized values</param>
        /// <param name="_node">The XmlNode containing the data for the collection</param>
        /// <param name="_deserializer">The deserialization framework performing the overall deserialization</param>
        /// <returns>TRUE always</returns>
        protected bool SpecialDeserialize(CWorkingObject _object, XmlElement _node, CDeserializer _deserializer)
        {
            if (!ATreatAsInterface.TreatAsInterface(_deserializer))
            {
                return(false);
            }

            // First, get an object to deserialize into
            var tmp = new CWorkingObject();

            if (_object.WorkingObject != null)
            {
                tmp.Set(_object.WorkingObject);
            }

            // Deserialize into the temporary WorkingObject
            BasicDeserialize(tmp, _node, _deserializer);

            // At this point, IF there was an object set in _object prior to this routine, its contents
            //  should have been set by the standard collection deserialization and we should be done.
            if (_object.IsSet)
            {
                return(true);
            }

            // Get the collection object populated so far with information from the Xml
            var tmpCollection = tmp.GetWorkingObject <TCollectionType>();

            // Using that object, create a new, syncronized version of that ArrayList.
            var specialCollection = MakeSpecialCollection(tmpCollection);

            // Set this sync'ed version to the _object, and we're done
            _object.Set(specialCollection);
            return(true);
        }
コード例 #3
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");
        }
コード例 #4
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);
        }
コード例 #5
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);
        }
コード例 #6
0
        public void TestConstructor()
        {
            var other = new CSerializationContext();
            var s     = new CDeserializer(other);

            ReferenceEquals(other, s.Context);
        }
コード例 #7
0
 public bool Deserialize(CWorkingObject _workingObject,
                         XmlElement _parentNode,
                         CDeserializer _deserializer)
 {
     Count++;
     return(false);
 }
コード例 #8
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;
            }
        }
コード例 #9
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.");
            }
        }
コード例 #10
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);
            }
        }
コード例 #11
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]);
            }
        }
コード例 #12
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);
            }
        }
コード例 #13
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");
        }
コード例 #14
0
        /// <summary>
        /// Add an Xml ChildNode to the collection being deserialized
        /// </summary>
        /// <param name="_collection">The collection that's being deserialized</param>
        /// <param name="_xmlElement">The XmlElement containing the data for this element</param>
        /// <param name="_expectedTypes">The expected Type(s) for this element</param>
        /// <param name="_deserializer">The deserialization framework controlling this process</param>
        protected override void AddElementFromXml(object _collection,
                                                  XmlElement _xmlElement,
                                                  Type[] _expectedTypes,
                                                  CDeserializer _deserializer)
        {
            var keyElem = _xmlElement[KEY_ELEMENT_NAME];

            if (keyElem == null)
            {
                throw new XDeserializationError("Could not find the KEY element for the Dictionary<,> entry.");
            }

            var valueElem = _xmlElement[VALUE_ELEMENT_NAME];

            if (valueElem == null)
            {
                throw new XDeserializationError("Could not find the VALUE element for the Dictionary<,> entry.");
            }

            var key   = _deserializer.FrameworkDeserialize(keyElem, _expectedTypes[0]);
            var value = _deserializer.FrameworkDeserialize(valueElem, _expectedTypes[1]);

            var collection = (IDictionary)_collection;

            collection.Add(key, value);
        }
コード例 #15
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);
        }
コード例 #16
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");
        }
コード例 #17
0
        public void TestConstructorNull()
        {
            var s = new CDeserializer(null);

            Assert.AreEqual(CSerializationContext.Global,
                            s.Context,
                            "Null constructor parameter should point to Global context");
        }
コード例 #18
0
        public void TestDeserializeNoType()
        {
            var s   = new CDeserializer();
            var doc = new XmlDocument();

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

            s.Deserialize(doc);
        }
コード例 #19
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);
        }
コード例 #20
0
        private static bool CreateFromXml(XmlElement _node, CWorkingObject _object, CDeserializer _framework)
        {
            var x = _object.GetExistingOrCreateNew <CStdImplicitSurrogate>();

            x.Name = XmlExtensions.GetAttributeValue(_node, "NAME");
            x.Age  = int.Parse(XmlExtensions.GetAttributeValue(_node, "AGE"));

            STATUS = ETestStatus.IMPLICIT_DESERIALIZER;
            return(true);
        }
コード例 #21
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");
        }
コード例 #22
0
        /// <summary>
        /// Add a single XmlElement to a collection, where that XmlElement is a child of the parent Collection element.
        /// </summary>
        /// <param name="_collection"></param>
        /// <param name="_xmlData"></param>
        /// <param name="_expectedTypes"></param>
        /// <param name="_deserializer"></param>
        protected override void AddElementFromXml(object _collection,
                                                  XmlElement _xmlData,
                                                  Type[] _expectedTypes,
                                                  CDeserializer _deserializer)
        {
            var o = _deserializer.FrameworkDeserialize(_xmlData, _expectedTypes[0]);

            var collection = (IList)_collection;

            collection.Add(o);
        }
コード例 #23
0
        private static void CreateFromXml(XmlElement _node, CWorkingObject _object, CDeserializer _framework)
        {
            var x = new CVoidImplicitSurrogate();

            _object.Set(x);

            x.Name = XmlExtensions.GetAttributeValue(_node, "NAME");
            x.Age  = int.Parse(XmlExtensions.GetAttributeValue(_node, "AGE"));

            STATUS = ETestStatus.IMPLICIT_DESERIALIZER_VOID;
        }
コード例 #24
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");
        }
コード例 #25
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");
        }
コード例 #26
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!");
        }
コード例 #27
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");
        }
コード例 #28
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");
        }
コード例 #29
0
        /// <summary>
        /// Add a single XmlElement to a collection, where that XmlElement is a child of the
        /// parent Collection element.
        /// </summary>
        /// <param name="_collection"></param>
        /// <param name="_xmlData"></param>
        /// <param name="_expectedTypes"></param>
        /// <param name="_deserializer"></param>
        protected override void AddElementFromXml(object _collection,
                                                  XmlElement _xmlData,
                                                  Type[] _expectedTypes,
                                                  CDeserializer _deserializer)
        {
            var o = _deserializer.FrameworkDeserialize(_xmlData, _expectedTypes[0]);

            var ct = _collection.GetType();
            var mi = ct.GetMethod("Add", _expectedTypes);

            mi.Invoke(_collection, new object[] { o });
        }
コード例 #30
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");
        }