예제 #1
0
        private object DeserializePof(byte[] data)
        {
            MemoryStream buffer = new MemoryStream(data);
            DataReader   reader = new DataReader(buffer);

            return(pofSerializer.Deserialize(reader));
        }
        public void TestSerializationWithSkippingReadingType()
        {
            SimplePofContext ctx = new SimplePofContext();

            ctx.RegisterUserType(1, typeof(SkippingPersonLite), new PortableObjectSerializer(1));

            SkippingPersonLite ana = new SkippingPersonLite("Ana Maria Seovic", new DateTime(2006, 8, 14));

            Stream stream = new MemoryStream();

            ctx.Serialize(new DataWriter(stream), ana);

            stream.Position = 0;
            SkippingPersonLite ana2 = (SkippingPersonLite)ctx.Deserialize(new DataReader(stream));
        }
        public void TestEvolvableObjectSerialization()
        {
            SimplePofContext ctx_v1 = new SimplePofContext();

            ctx_v1.RegisterUserType(1, typeof(EvolvablePortablePerson), new PortableObjectSerializer(1));
            ctx_v1.RegisterUserType(2, typeof(Address), new PortableObjectSerializer(2));

            SimplePofContext ctx_v2 = new SimplePofContext();

            ctx_v2.RegisterUserType(1, typeof(EvolvablePortablePerson2), new PortableObjectSerializer(1));
            ctx_v2.RegisterUserType(2, typeof(Address), new PortableObjectSerializer(2));

            EvolvablePortablePerson2 aleks = new EvolvablePortablePerson2("Aleksandar Seovic", new DateTime(1974, 8, 24));
            EvolvablePortablePerson2 ana   = new EvolvablePortablePerson2("Ana Maria Seovic", new DateTime(2004, 8, 14, 7, 43, 0));

            aleks.Address      = ana.Address = new Address("208 Myrtle Ridge Rd", "Lutz", "FL", "33549");
            aleks.Nationality  = ana.Nationality = "Serbian";
            aleks.PlaceOfBirth = new Address(null, "Belgrade", "Serbia", "11000");
            ana.PlaceOfBirth   = new Address("128 Asbury Ave, #401", "Evanston", "IL", "60202");
            aleks.Children     = new EvolvablePortablePerson2[] { ana };

            Stream stream_v2 = new MemoryStream();

            ctx_v2.Serialize(new DataWriter(stream_v2), aleks);

            stream_v2.Position = 0;
            EvolvablePortablePerson aleks_v1 = (EvolvablePortablePerson)ctx_v1.Deserialize(new DataReader(stream_v2));

            EvolvablePortablePerson marija_v1 = new EvolvablePortablePerson("Marija Seovic", new DateTime(1978, 2, 20));

            marija_v1.Address  = aleks_v1.Address;
            marija_v1.Children = aleks_v1.Children;
            aleks_v1.Spouse    = marija_v1;

            Stream stream_v1 = new MemoryStream();

            ctx_v1.Serialize(new DataWriter(stream_v1), aleks_v1);

            stream_v1.Position = 0;
            EvolvablePortablePerson2 aleks_v2 = (EvolvablePortablePerson2)ctx_v2.Deserialize(new DataReader(stream_v1));

            Assert.AreEqual(aleks.Name, aleks_v2.Name);
            Assert.AreEqual(aleks.Nationality, aleks_v2.Nationality);
            Assert.AreEqual(aleks.DOB, aleks_v2.DOB);
            Assert.AreEqual(aleks_v1.Spouse.Name, aleks_v2.Spouse.Name);
            Assert.AreEqual(aleks.Address.City, aleks_v2.Address.City);
            Assert.AreEqual(aleks.PlaceOfBirth.City, aleks_v2.PlaceOfBirth.City);
        }
        public void TestSerialization()
        {
            SimplePofContext ctx = new SimplePofContext();

            ctx.RegisterUserType(1, typeof(PortablePerson), new PortableObjectSerializer(1));
            ctx.RegisterUserType(2, typeof(Address), new PortableObjectSerializer(2));

            PortablePerson aleks  = new PortablePerson("Aleksandar Seovic", new DateTime(1974, 8, 24));
            PortablePerson marija = new PortablePerson("Marija Seovic", new DateTime(1978, 2, 20));
            PortablePerson ana    = new PortablePerson("Ana Maria Seovic", new DateTime(2004, 8, 14, 7, 43, 0));

            aleks.Spouse   = marija;
            aleks.Address  = marija.Address = ana.Address = new Address("208 Myrtle Ridge Rd", "Lutz", "FL", "33549");
            aleks.Children = marija.Children = new PortablePerson[] { ana };

            Stream stream = new MemoryStream();

            ctx.Serialize(new DataWriter(stream), aleks);

            stream.Position = 0;
            PortablePerson aleks2 = (PortablePerson)ctx.Deserialize(new DataReader(stream));

            Assert.AreEqual(aleks.Name, aleks2.Name);
            Assert.AreEqual(aleks.DOB, aleks2.DOB);
            Assert.AreEqual(aleks.Spouse.Name, aleks2.Spouse.Name);
            Assert.AreEqual(aleks.Address.City, aleks2.Address.City);
            Assert.AreEqual(1, aleks2.Children.Length);
            Assert.AreEqual(ana.Name, aleks2.Children[0].Name);
            Assert.AreEqual(ana.Address.Street, aleks2.Children[0].Address.Street);

            SimplePofContext liteCtx = new SimplePofContext();

            liteCtx.RegisterUserType(1, typeof(PortablePersonLite), new PortableObjectSerializer(1));
            liteCtx.RegisterUserType(2, typeof(Address), new PortableObjectSerializer(2));

            stream.Position = 0;
            PortablePersonLite aleks3 = (PortablePersonLite)liteCtx.Deserialize(new DataReader(stream));

            Assert.AreEqual(aleks.Name, aleks3.Name);
            Assert.AreEqual(aleks.DOB, aleks3.DOB);
        }
예제 #5
0
        public void TestSynchronizedDictionarySerialization()
        {
            SynchronizedDictionary ht = new SynchronizedDictionary();

            SimplePofContext    ctx        = new SimplePofContext();
            BinaryPofSerializer serializer = new BinaryPofSerializer(1);

            ctx.RegisterUserType(1, ht.GetType(), serializer);

            Assert.AreEqual(1, ctx.GetUserTypeIdentifier(ht));
            Assert.AreEqual(1, ctx.GetUserTypeIdentifier(ht.GetType()));

            Assert.AreEqual(ht.GetType(), ctx.GetType(1));
            Assert.AreEqual(ht.GetType().FullName, ctx.GetTypeName(1));
            Assert.AreEqual(serializer, ctx.GetPofSerializer(1));

            ht.Add(1, 1);
            ht.Add(2, 2);
            ht.Add(3, 3);
            ht.Add(4, 4);
            ht.Add(5, 5);
            ht.Add(6, 6);
            ht.Add(7, 7);

            Stream stream = new MemoryStream();

            ctx.Serialize(new DataWriter(stream), ht);

            stream.Position = 0;
            SynchronizedDictionary ht2 = (SynchronizedDictionary)ctx.Deserialize(new DataReader(stream));

            Assert.AreEqual(ht[1], ht2[1]);
            Assert.AreEqual(ht[2], ht2[2]);
            Assert.AreEqual(ht[3], ht2[3]);
            Assert.AreEqual(ht[4], ht2[4]);
            Assert.AreEqual(ht[5], ht2[5]);
            Assert.AreEqual(ht[6], ht2[6]);
            Assert.AreEqual(ht[7], ht2[7]);
        }
        public void TestDeserializationException()
        {
            SimplePofContext ctx = new SimplePofContext();

            ctx.RegisterUserType(1, typeof(PortablePerson), new PortableObjectSerializer(1));
            ctx.RegisterUserType(2, typeof(Address), new PortableObjectSerializer(2));

            PortablePerson aleks  = new PortablePerson("Aleksandar Seovic", new DateTime(1974, 8, 24));
            PortablePerson marija = new PortablePerson("Marija Seovic", new DateTime(1978, 2, 20));
            PortablePerson ana    = new PortablePerson("Ana Maria Seovic", new DateTime(2004, 8, 14, 7, 43, 0));

            aleks.Spouse   = marija;
            aleks.Address  = marija.Address = ana.Address = new Address("208 Myrtle Ridge Rd", "Lutz", "FL", "33549");
            aleks.Children = marija.Children = new PortablePerson[] { ana };

            Stream stream = new MemoryStream();

            ctx.Serialize(new DataWriter(stream), aleks);

            stream.Position = 0;
            stream.Close();
            PortablePerson aleks2 = (PortablePerson)ctx.Deserialize(new DataReader(stream));
        }
        public void TestXmlPofSerializer()
        {
            MemoryStream     stream        = new MemoryStream();
            DataWriter       writer        = new DataWriter(stream);
            XmlPofSerializer xmlSerializer = new XmlPofSerializer(1);
            SimplePofContext context       = new SimplePofContext();

            context.RegisterUserType(1, typeof(DataSet), xmlSerializer);

            // create test DataSet
            DataSet set = CreateTestDataSet();

            // serialize DataSet
            context.Serialize(writer, set);

            // deserialize DataSet
            stream.Seek(0, SeekOrigin.Begin);
            DataReader reader   = new DataReader(stream);
            object     deserObj = context.Deserialize(reader);

            Assert.IsInstanceOf(typeof(DataSet), deserObj);
            DataSet deserDS = (DataSet)deserObj;

            // Assert tables
            Assert.AreEqual(set.Tables.Count, deserDS.Tables.Count);
            for (int i = 0; i < set.Tables.Count; i++)
            {
                DataTable table      = set.Tables[i];
                DataTable deserTable = deserDS.Tables[i];
                Assert.AreEqual(table.TableName, deserTable.TableName);
                Assert.AreEqual(table.Columns.Count, deserTable.Columns.Count);

                for (int j = 0, c = table.Columns.Count; j < c; j++)
                {
                    DataColumn column1 = table.Columns[j];
                    DataColumn column2 = deserTable.Columns[j];
                    Assert.AreEqual(column1.ColumnName, column2.ColumnName);
                    Assert.AreEqual(column1.DataType, column2.DataType);
                    Assert.AreEqual(column1.Unique, column2.Unique);
                }

                Assert.AreEqual(table.Rows.Count, deserTable.Rows.Count);
                for (int k = 0, r = table.Rows.Count; k < r; k++)
                {
                    DataRow row1 = table.Rows[k];
                    DataRow row2 = deserTable.Rows[k];
                    for (int m = 0, c = table.Columns.Count; m < c; m++)
                    {
                        Assert.AreEqual(row1[m], row2[m]);
                    }
                }
            }

            // Assert relations
            Assert.AreEqual(set.Relations.Count, deserDS.Relations.Count);
            for (int i = 0; i < set.Relations.Count; i++)
            {
                DataColumn[] parentColumns1 = set.Relations[i].ParentColumns;
                DataColumn[] parentColumns2 = deserDS.Relations[i].ParentColumns;
                Assert.AreEqual(parentColumns1.Length, parentColumns2.Length);
                for (int j = 0; j < parentColumns1.Length; j++)
                {
                    Assert.AreEqual(parentColumns1[j].ColumnName, parentColumns2[j].ColumnName);
                }

                DataColumn[] childColumns1 = set.Relations[i].ChildColumns;
                DataColumn[] childColumns2 = deserDS.Relations[i].ChildColumns;
                Assert.AreEqual(childColumns1.Length, childColumns2.Length);
                for (int j = 0; j < childColumns1.Length; j++)
                {
                    Assert.AreEqual(childColumns1[j].ColumnName, childColumns2[j].ColumnName);
                }
            }
        }
예제 #8
0
        public void TestDataSetSerialization()
        {
            DataSet dataSet1 = new DataSet();

            SimplePofContext    ctx        = new SimplePofContext();
            BinaryPofSerializer serializer = new BinaryPofSerializer(1);

            ctx.RegisterUserType(1, dataSet1.GetType(), serializer);

            Assert.AreEqual(1, ctx.GetUserTypeIdentifier(dataSet1));
            Assert.AreEqual(1, ctx.GetUserTypeIdentifier(dataSet1.GetType()));

            Assert.AreEqual(dataSet1.GetType(), ctx.GetType(1));
            Assert.AreEqual(dataSet1.GetType().FullName, ctx.GetTypeName(1));
            Assert.AreEqual(serializer, ctx.GetPofSerializer(1));

            dataSet1.Tables.Add("Order");
            dataSet1.Tables["Order"].Columns.Add("Id", typeof(String));
            dataSet1.Tables["Order"].Columns.Add("Name", typeof(String));

            dataSet1.Tables.Add("OrderDetail");
            dataSet1.Tables["OrderDetail"].Columns.Add("Id", typeof(String));
            dataSet1.Tables["OrderDetail"].Columns.Add("Sum", typeof(Double));
            dataSet1.Tables["OrderDetail"].Columns.Add("OrderId", typeof(String));

            DataRelation relation = new DataRelation("Order_OrderDetail", dataSet1.Tables["Order"].Columns["Id"], dataSet1.Tables["OrderDetail"].Columns["OrderId"]);

            dataSet1.Relations.Add(relation);

            dataSet1.Tables["Order"].Rows.Add(new object[] { "01/2007", "Comedy books" });
            dataSet1.Tables["Order"].Rows.Add(new object[] { "02/2007", "Suite" });

            dataSet1.Tables["OrderDetail"].Rows.Add(new object[] { "01/2007-01", 1500, "01/2007" });
            dataSet1.Tables["OrderDetail"].Rows.Add(new object[] { "01/2007-02", 1350, "01/2007" });
            dataSet1.Tables["OrderDetail"].Rows.Add(new object[] { "02/2007-01", 5500.50, "02/2007" });

            Stream stream = new MemoryStream();

            ctx.Serialize(new DataWriter(stream), dataSet1);

            stream.Position = 0;
            DataSet dataSet2 = (DataSet)ctx.Deserialize(new DataReader(stream));

            Assert.AreEqual(dataSet1.Tables["Order"].Columns.Count, dataSet2.Tables["Order"].Columns.Count);
            Assert.AreEqual(dataSet1.Tables["Order"].Columns["Id"].DataType, dataSet2.Tables["Order"].Columns["Id"].DataType);
            Assert.AreEqual(dataSet1.Tables["Order"].Columns["Name"].DataType, dataSet2.Tables["Order"].Columns["Name"].DataType);

            Assert.AreEqual(dataSet1.Tables["OrderDetail"].Columns.Count, dataSet2.Tables["OrderDetail"].Columns.Count);
            Assert.AreEqual(dataSet1.Tables["OrderDetail"].Columns["Id"].DataType, dataSet2.Tables["OrderDetail"].Columns["Id"].DataType);
            Assert.AreEqual(dataSet1.Tables["OrderDetail"].Columns["Sum"].DataType, dataSet2.Tables["OrderDetail"].Columns["Sum"].DataType);
            Assert.AreEqual(dataSet1.Tables["OrderDetail"].Columns["OrderId"].DataType, dataSet2.Tables["OrderDetail"].Columns["OrderId"].DataType);

            Assert.AreEqual(dataSet1.Tables["Order"].Rows.Count, dataSet2.Tables["Order"].Rows.Count);
            Assert.AreEqual(dataSet1.Tables["Order"].Rows[0]["Id"], dataSet2.Tables["Order"].Rows[0]["Id"]);
            Assert.AreEqual(dataSet1.Tables["Order"].Rows[0]["Name"], dataSet2.Tables["Order"].Rows[0]["Name"]);
            Assert.AreEqual(dataSet1.Tables["Order"].Rows[1]["Id"], dataSet2.Tables["Order"].Rows[1]["Id"]);
            Assert.AreEqual(dataSet1.Tables["Order"].Rows[1]["Name"], dataSet2.Tables["Order"].Rows[1]["Name"]);

            Assert.AreEqual(dataSet1.Tables["OrderDetail"].Rows.Count, dataSet2.Tables["OrderDetail"].Rows.Count);
            Assert.AreEqual(dataSet1.Tables["OrderDetail"].Rows[0]["Id"], dataSet2.Tables["OrderDetail"].Rows[0]["Id"]);
            Assert.AreEqual(dataSet1.Tables["OrderDetail"].Rows[0]["Sum"], dataSet2.Tables["OrderDetail"].Rows[0]["Sum"]);
            Assert.AreEqual(dataSet1.Tables["OrderDetail"].Rows[0]["OrderId"], dataSet2.Tables["OrderDetail"].Rows[0]["OrderId"]);

            Assert.AreEqual(dataSet1.Tables["OrderDetail"].Rows[1]["Id"], dataSet2.Tables["OrderDetail"].Rows[1]["Id"]);
            Assert.AreEqual(dataSet1.Tables["OrderDetail"].Rows[1]["Sum"], dataSet2.Tables["OrderDetail"].Rows[1]["Sum"]);
            Assert.AreEqual(dataSet1.Tables["OrderDetail"].Rows[1]["OrderId"], dataSet2.Tables["OrderDetail"].Rows[1]["OrderId"]);

            Assert.AreEqual(dataSet1.Tables["OrderDetail"].Rows[2]["Id"], dataSet2.Tables["OrderDetail"].Rows[2]["Id"]);
            Assert.AreEqual(dataSet1.Tables["OrderDetail"].Rows[2]["Sum"], dataSet2.Tables["OrderDetail"].Rows[2]["Sum"]);
            Assert.AreEqual(dataSet1.Tables["OrderDetail"].Rows[2]["OrderId"], dataSet2.Tables["OrderDetail"].Rows[2]["OrderId"]);

            ctx.UnregisterUserType(1);
        }