Пример #1
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);
        }
Пример #2
0
        public static void GenerateData(int _personCount)
        {
            var rng = new Random(12345);

            People = new CBigPerson[_personCount];

            for (var i = 0; i < _personCount; i++)
            {
                var p = new CBigPerson
                {
                    Name     = "Person " + i,
                    Age      = rng.Next(10, 100),
                    Height   = rng.NextDouble(),
                    IsParent = rng.Next(2) == 0
                };

                var kidCount = rng.Next(0, 4) + rng.Next(0, 4);
                p.KidsAges  = new int[kidCount];
                p.KidsNames = new string[kidCount];

                for (var j = 0; j < kidCount; j++)
                {
                    p.KidsNames[j] = "Kidname " + j;
                    p.KidsAges[j]  = rng.Next(3, 18);
                }

                p.Numbers = new byte[rng.Next(1, 50)];
                rng.NextBytes(p.Numbers);

                People[i] = p;
            }

            for (var i = 0; i < _personCount; i++)
            {
                var numFriends = rng.Next(1, 12) + rng.Next(1, 12);
                People[i].Friends = new CFriend[numFriends];

                for (var j = 0; j < numFriends; j++)
                {
                    var f = new CFriend
                    {
                        IsBestFriend = rng.Next(2) == 0,
                        Rating       = rng.Next(1000000),
                        FriendPerson = People[rng.Next(_personCount)]
                    };

                    People[i].Friends[j] = f;
                }
            }
        }
Пример #3
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");
        }
Пример #4
0
        public void TestPeople()
        {
            CBigPerson.GenerateData(100);
            var c = new CSerializationContext();

            c.SetConcise();
            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));

            var d = new CDeserializer(c);

            var x2 = d.Deserialize <CBigPerson[]>(doc);
            var x1 = CBigPerson.People;

            AssertEqualBigPeopleArray(x1, x2);
        }