/// <summary>
        /// Serialize an object to a stream by writing its state using the
        /// specified <see cref="DataWriter"/> object.
        /// </summary>
        /// <param name="writer">
        /// The <b>DataWriter</b> with which to write the object's state.
        /// </param>
        /// <param name="o">
        /// The object to serialize.
        /// </param>
        /// <exception cref="IOException">
        /// If an I/O error occurs.
        /// </exception>
        public virtual void Serialize(DataWriter writer, object o)
        {
            PofStreamWriter pofWriter = new PofStreamWriter(writer, this);

            // COH-5065: due to the complexity of maintaining references
            // in future data, we won't support them for IEvolvable objects
            if (IsReferenceEnabled && !(o is IEvolvable))
            {
                pofWriter.EnableReference();
            }

            try
            {
                pofWriter.WriteObject(-1, o);
            }
            catch (ArgumentException e)
            {
                // Guarantee that exceptions from called methods are IOException
                throw new IOException(e.Message, e);
            }
            catch (NotSupportedException e)
            {
                // Guarantee that exceptions from called methods are IOException
                throw new IOException(e.Message, e);
            }
        }
Example #2
0
        public void TestWriterProperties()
        {
            PofStreamWriter psw = new PofStreamWriter(new DataWriter(new MemoryStream()), new SimplePofContext());

            psw.PofContext = new SimplePofContext();
            Console.WriteLine(psw.UserTypeId);
            psw.PofContext = null;
        }
Example #3
0
        public void TestReadInt32Array()
        {
            la      = new LongSortedList();
            la[1L]  = 0;
            la[3L]  = 1;
            la[30L] = Int32.MaxValue;
            la[31L] = Int32.MinValue;

            LongSortedList la1 = new LongSortedList();

            la1[1L]  = (Single)1.0;
            la1[10L] = (Single)10.0;
            la1[11L] = (Single)11.0;

            DataWriter      writer    = new DataWriter(stream);
            PofStreamWriter pofWriter = new PofStreamWriter(writer, ctx);

            // writting ILongArray
            pofWriter.WriteLongArray(0, la);
            pofWriter.WriteLongArray(0, new LongSortedList());
            // writting uniform ILongArray
            pofWriter.WriteLongArray(0, la, typeof(Int32));
            pofWriter.WriteLongArray(0, la1, typeof(Single));

            stream.Position = 0;
            DataReader      reader    = new DataReader(stream);
            PofStreamReader pofReader = new PofStreamReader(reader, ctx);

            // reading ILongArray
            Int32[] resInt32Array = pofReader.ReadInt32Array(0);
            Assert.AreEqual(la.LastIndex + 1, resInt32Array.Length);

            Assert.AreEqual(la[1L], resInt32Array[1]);
            Assert.AreEqual(la[3L], resInt32Array[3]);
            Assert.AreEqual(la[30L], resInt32Array[30]);
            Assert.AreEqual(la[31L], resInt32Array[31]);

            resInt32Array = pofReader.ReadInt32Array(0);
            Assert.AreEqual(0, resInt32Array.Length);

            // reading  uniform ILongArray
            resInt32Array = pofReader.ReadInt32Array(0);
            Assert.AreEqual(la.LastIndex + 1, resInt32Array.Length);

            Assert.AreEqual(la[1L], resInt32Array[1]);
            Assert.AreEqual(la[3L], resInt32Array[3]);
            Assert.AreEqual(la[30L], resInt32Array[30]);
            Assert.AreEqual(la[31L], resInt32Array[31]);

            resInt32Array = pofReader.ReadInt32Array(0);
            Assert.AreEqual(la1.LastIndex + 1, resInt32Array.Length);

            Assert.AreEqual(la1[1L], resInt32Array[1]);
            Assert.AreEqual(la1[10L], resInt32Array[10]);
            Assert.AreEqual(la1[11L], resInt32Array[11]);
        }
 private void initPOFWriter()
 {
     if (m_ctx == null)
     {
         m_ctx = new SimplePofContext();
     }
     m_stream    = new MemoryStream();
     m_writer    = new DataWriter(m_stream);
     m_pofWriter = new PofStreamWriter(m_writer, m_ctx);
 }
Example #5
0
        private void writeChar()
        {
            DataWriter        dw  = initPofWriter("Char");
            WritingPofHandler wfh = new WritingPofHandler(dw);
            PofStreamWriter   psw = new PofStreamWriter(wfh, new SimplePofContext());

            psw.WriteChar(0, 'f');
            psw.WriteChar(0, '0');

            dw.Flush();
            dw.Close();
        }
Example #6
0
        private void writeInt64()
        {
            DataWriter        dw  = initPofWriter("Int64");
            WritingPofHandler wfh = new WritingPofHandler(dw);
            PofStreamWriter   psw = new PofStreamWriter(wfh, new SimplePofContext());

            psw.WriteInt64(0, -1L);
            psw.WriteInt64(0, Int64.MaxValue);

            dw.Flush();
            dw.Close();
        }
Example #7
0
        public void TestReadLongArray()
        {
            la      = new LongSortedList();
            la[1L]  = 'a';
            la[3L]  = 'b';
            la[20L] = 'c';
            byte[] bytes = new byte[] { 1, 250, 3 };

            DataWriter      writer    = new DataWriter(stream);
            PofStreamWriter pofWriter = new PofStreamWriter(writer, ctx);

            pofWriter.WriteLongArray(0, la);
            pofWriter.WriteLongArray(0, la, typeof(char));
            pofWriter.WriteLongArray(0, new LongSortedList());
            pofWriter.WriteLongArray(0, null);
            pofWriter.WriteArray(0, bytes);
            pofWriter.WriteArray(0, bytes, typeof(byte));
            pofWriter.WriteArray(0, new byte[0]);

            stream.Position = 0;
            DataReader      reader    = new DataReader(stream);
            PofStreamReader pofReader = new PofStreamReader(reader, ctx);

            ILongArray resLongArray = pofReader.ReadLongArray(0, null);

            Assert.AreEqual(la.Count, resLongArray.Count);
            Assert.AreEqual(la[1], resLongArray[1]);
            Assert.AreEqual(la[3], resLongArray[3]);
            Assert.AreEqual(la[20], resLongArray[20]);

            //uniform sparse array
            resLongArray = pofReader.ReadLongArray(0, null);
            Assert.AreEqual(la.Count, resLongArray.Count);
            Assert.AreEqual(la[1], resLongArray[1]);
            Assert.AreEqual(la[3], resLongArray[3]);
            Assert.AreEqual(la[20], resLongArray[20]);

            resLongArray = pofReader.ReadLongArray(0, null);
            Assert.AreEqual(0, resLongArray.Count);

            resLongArray = pofReader.ReadLongArray(0, null);
            Assert.IsTrue(resLongArray == null);

            resLongArray = pofReader.ReadLongArray(0, null);
            Assert.AreEqual(3, resLongArray.Count);

            resLongArray = pofReader.ReadLongArray(0, null);
            Assert.AreEqual(3, resLongArray.Count);

            resLongArray = pofReader.ReadLongArray(0, new LongSortedList());
            Assert.AreEqual(0, resLongArray.Count);
        }
Example #8
0
        private void writeDecimal32()
        {
            DataWriter        dw  = initPofWriter("Dec32");
            WritingPofHandler wfh = new WritingPofHandler(dw);
            PofStreamWriter   psw = new PofStreamWriter(wfh, new SimplePofContext());

            psw.WriteDecimal(0, new Decimal(99999, 0, 0, false, 0));
            psw.WriteDecimal(0, new Decimal(9999999, 0, 0, false, 0));
            psw.WriteDecimal(0, new Decimal(9999999, 0, 0, false, 28));

            dw.Flush();
            dw.Close();
        }
Example #9
0
        /*---------Utility methods for writing POF data----------------------*/

        private void writeByte()
        {
            DataWriter        dw  = initPofWriter("Byte");
            WritingPofHandler wfh = new WritingPofHandler(dw);
            PofStreamWriter   psw = new PofStreamWriter(wfh, new SimplePofContext());

            psw.WriteByte(0, 1);
            psw.WriteByte(0, 0);
            psw.WriteByte(0, 200);
            psw.WriteByte(0, 255);

            dw.Flush();
            dw.Close();
        }
Example #10
0
        public void TestReadObject()
        {
            la     = new LongSortedList();
            la[1L] = "one";
            la[2L] = "two";
            la.Add("five");
            la.Add("three");
            la[200L] = "twohundred";


            DataWriter      writer    = new DataWriter(stream);
            PofStreamWriter pofWriter = new PofStreamWriter(writer, ctx);

            pofWriter.WriteLongArray(0, la);
            pofWriter.WriteLongArray(0, new LongSortedList());
            pofWriter.WriteLongArray(0, la, typeof(String));

            stream.Position = 0;
            DataReader      reader    = new DataReader(stream);
            PofStreamReader pofReader = new PofStreamReader(reader, ctx);

            LongSortedList resLA = (LongSortedList)pofReader.ReadObject(0);

            Assert.AreEqual(la.Count, resLA.Count);

            IEnumerator e  = resLA.GetEnumerator();
            IEnumerator de = la.GetEnumerator();

            for (; e.MoveNext() && de.MoveNext();)
            {
                Assert.AreEqual(((DictionaryEntry)e.Current).Key, ((DictionaryEntry)de.Current).Key);
                Assert.AreEqual(((DictionaryEntry)e.Current).Value, ((DictionaryEntry)de.Current).Value);
            }

            resLA = (LongSortedList)pofReader.ReadObject(0);
            Assert.AreEqual(0, resLA.Count);

            //uniform sparse array
            resLA = (LongSortedList)pofReader.ReadObject(0);
            Assert.AreEqual(la.Count, resLA.Count);

            e  = resLA.GetEnumerator();
            de = la.GetEnumerator();
            for (; e.MoveNext() && de.MoveNext();)
            {
                Assert.AreEqual(((DictionaryEntry)e.Current).Key, ((DictionaryEntry)de.Current).Key);
                Assert.AreEqual(((DictionaryEntry)e.Current).Value, ((DictionaryEntry)de.Current).Value);
            }
        }
        public void TestReferencesInUniformMap()
        {
            var localCtx = new SimplePofContext();

            localCtx.RegisterUserType(101, typeof(PortablePerson),
                                      new PortableObjectSerializer(101));
            localCtx.RegisterUserType(102, typeof(PortablePersonReference),
                                      new PortableObjectSerializer(102));
            localCtx.RegisterUserType(201, typeof(CompositeKey),
                                      new PortableObjectSerializer(201));

            IDictionary  mapPerson = new Dictionary <CompositeKey, IPortableObject>();
            const String lastName  = "Smith";
            CompositeKey key1      = new CompositeKey(lastName, "ivan"),
                         key2      = new CompositeKey(lastName, "goran");
            var ivan = new PortablePersonReference("Ivan", new DateTime(78, 4, 25));

            ivan.Children = null;
            mapPerson.Add(key1, ivan);
            mapPerson.Add(key2, ivan);

            initPOFWriter();
            m_pofWriter = new PofStreamWriter(m_writer, localCtx);
            m_pofWriter.EnableReference();
            m_pofWriter.WriteDictionary(0, mapPerson, typeof(CompositeKey),
                                        typeof(PortablePersonReference));

            var mapPersonR = new Dictionary <CompositeKey, IPortableObject>();

            initPOFReader();
            m_pofReader = new PofStreamReader(m_reader, localCtx);
            m_pofReader.ReadDictionary(0, (IDictionary)mapPersonR);

            // compare mapPerson with result
            Assert.IsTrue(mapPersonR[key1].Equals(mapPerson[key1]));
            Assert.IsTrue(mapPersonR[key2].Equals(mapPerson[key2]));

            ICollection colValR = mapPersonR.Values;
            IEnumerator iter    = colValR.GetEnumerator();

            iter.MoveNext();
            var val1 = (PortablePersonReference)iter.Current;

            iter.MoveNext();
            var val2 = (PortablePersonReference)iter.Current;

            Assert.IsTrue(val1 == val2);
        }
Example #12
0
        private void writeDecimal64()
        {
            DataWriter        dw  = initPofWriter("Dec64");
            WritingPofHandler wfh = new WritingPofHandler(dw);
            PofStreamWriter   psw = new PofStreamWriter(wfh, new SimplePofContext());

            psw.WriteDecimal(0, new Decimal(9999999999));

            Decimal d2 = new Decimal(9999999999999999);

            psw.WriteDecimal(0, d2);

            int[] value = Decimal.GetBits(d2);
            psw.WriteDecimal(0, new Decimal(value[0], value[1], value[3], false, 28));

            dw.Flush();
            dw.Close();
        }
Example #13
0
        private void writeDecimal128()
        {
            DataWriter        dw  = initPofWriter("Dec128");
            WritingPofHandler wfh = new WritingPofHandler(dw);
            PofStreamWriter   psw = new PofStreamWriter(wfh, new SimplePofContext());

            psw.WriteDecimal(0, Decimal.MaxValue);

            Decimal d1 = Decimal.Add(new Decimal(Int64.MaxValue), Decimal.One);

            psw.WriteDecimal(0, d1);

            // Scale the maximum integer plus one value
            int[] value = Decimal.GetBits(d1);
            psw.WriteDecimal(0, new Decimal(value[0], value[1], value[3], false, 28));

            dw.Flush();
            dw.Close();
        }
        public void testSerialization()
        {
            String sPath = "config/reference-pof-config.xml";
            var    ctx   = new ConfigurablePofContext(sPath);
            var    bal   = new Balance();
            var    p     = new Product(bal);
            var    c     = new Customer("Customer", p, bal);

            bal.setBalance(2.0);
            bal.setCustomer(c);

            initPOFWriter();
            m_pofwriter = new PofStreamWriter(m_writer, ctx);
            m_pofwriter.EnableReference();
            m_pofwriter.WriteObject(0, c);

            initPOFReader();
            m_pofreader = new PofStreamReader(m_reader, ctx);
            var cResult = (Customer)m_pofreader.ReadObject(0);

            Assert.IsTrue(cResult.getProduct().getBalance() == cResult.getBalance());
        }
        public void testCircularReferences()
        {
            var ctx = new SimplePofContext();

            ctx.RegisterUserType(101, typeof(PortablePerson),
                                 new PortableObjectSerializer(101));
            ctx.RegisterUserType(102, typeof(PortablePersonReference),
                                 new PortableObjectSerializer(102));

            var ivan = new PortablePersonReference("Ivan", new DateTime(78, 4, 25));

            ivan.Children    = new PortablePerson[1];
            ivan.Children[0] = new PortablePerson("Mary Jane", new DateTime(97, 8, 14));
            ivan.Spouse      = new PortablePerson("Eda", new DateTime(79, 6, 25));

            var goran = new PortablePersonReference("Goran", new DateTime(82, 3, 3));

            goran.Children    = new PortablePerson[2];
            goran.Children[0] = new PortablePerson("Tom", new DateTime(103, 7, 5));
            goran.Children[1] = new PortablePerson("Ellen", new DateTime(105, 3, 15));
            goran.Spouse      = new PortablePerson("Tiffany", new DateTime(82, 3, 25));
            goran.Friend      = ivan;
            ivan.Friend       = goran;

            initPOFWriter();
            m_pofWriter = new PofStreamWriter(m_writer, ctx);
            m_pofWriter.EnableReference();
            m_pofWriter.WriteObject(0, ivan);

            initPOFReader();
            m_pofReader = new PofStreamReader(m_reader, ctx);

            var ivanR = (PortablePersonReference)m_pofReader.ReadObject(0);

            Assert.IsTrue(ivanR.Name.Equals(ivan.Name));
            Assert.IsTrue(ivanR.Children.Length == 1);
            Assert.IsTrue(ivanR.Friend.Equals(goran));
        }
        /// <summary>
        /// Return this value's serialized form.
        /// </summary>
        /// <returns>
        /// This value's serialized form.
        /// </returns>
        public virtual Binary GetSerializedValue()
        {
            if (IsDirty)
            {
                var buf    = new BinaryMemoryStream(Size);
                var writer = new PofStreamWriter(new DataWriter(buf), PofContext);
                writer.WriteObject(0, m_oValue);
                buf.Position = 0;

                if (IsUniformEncoded)
                {
                    var reader = new DataReader(buf);

                    // skip type id
                    reader.ReadPackedInt32();
                    var of = (int)buf.Position;
                    return(new Binary(buf.GetInternalByteArray(), of,
                                      (int)(buf.Length - of)));
                }
                return(buf.ToBinary());
            }
            return(m_binValue);
        }
Example #17
0
        public void TestReadString()
        {
            la      = new LongSortedList();
            la[1L]  = 'a';
            la[3L]  = 'b';
            la[20L] = 'c';

            DataWriter      writer    = new DataWriter(stream);
            PofStreamWriter pofWriter = new PofStreamWriter(writer, ctx);

            pofWriter.WriteLongArray(0, la);
            pofWriter.WriteLongArray(0, new LongSortedList());
            pofWriter.WriteLongArray(0, la, typeof(char));

            stream.Position = 0;
            DataReader      reader    = new DataReader(stream);
            PofStreamReader pofReader = new PofStreamReader(reader, ctx);

            String resCharArray = pofReader.ReadString(0);

            Assert.AreEqual(la.LastIndex + 1, resCharArray.Length);

            Assert.AreEqual(la[1L], resCharArray[1]);
            Assert.AreEqual(la[3L], resCharArray[3]);
            Assert.AreEqual(la[20L], resCharArray[20]);

            resCharArray = pofReader.ReadString(0);
            Assert.IsTrue(resCharArray.Length == 0);

            //uniform sparse array
            resCharArray = pofReader.ReadString(0);
            Assert.AreEqual(la.LastIndex + 1, resCharArray.Length);

            Assert.AreEqual(la[1L], resCharArray[1]);
            Assert.AreEqual(la[3L], resCharArray[3]);
            Assert.AreEqual(la[20L], resCharArray[20]);
        }
Example #18
0
        public void TestReadByteArray()
        {
            la      = new LongSortedList();
            la[1L]  = (byte)1;
            la[3L]  = (byte)3;
            la[30L] = (byte)30;

            DataWriter      writer    = new DataWriter(stream);
            PofStreamWriter pofWriter = new PofStreamWriter(writer, ctx);

            pofWriter.WriteLongArray(0, la);
            pofWriter.WriteLongArray(0, new LongSortedList());
            pofWriter.WriteLongArray(0, la, typeof(byte));

            stream.Position = 0;
            DataReader      reader    = new DataReader(stream);
            PofStreamReader pofReader = new PofStreamReader(reader, ctx);

            byte[] resByteArray = pofReader.ReadByteArray(0);
            Assert.AreEqual(la.LastIndex + 1, resByteArray.Length);

            Assert.AreEqual(la[1L], resByteArray[1]);
            Assert.AreEqual(la[3L], resByteArray[3]);
            Assert.AreEqual(la[30L], resByteArray[30]);

            resByteArray = pofReader.ReadByteArray(0);
            Assert.AreEqual(0, resByteArray.Length);

            //uniform sparse array
            resByteArray = pofReader.ReadByteArray(0);
            Assert.AreEqual(la.LastIndex + 1, resByteArray.Length);

            Assert.AreEqual(la[1L], resByteArray[1]);
            Assert.AreEqual(la[3L], resByteArray[3]);
            Assert.AreEqual(la[30L], resByteArray[30]);
        }
Example #19
0
        private void writeInt128()
        {
            DataWriter        dw  = initPofWriter("Int128");
            WritingPofHandler wfh = new WritingPofHandler(dw);
            PofStreamWriter   psw = new PofStreamWriter(wfh, new SimplePofContext());

            // Test data:
            //   First byte array is equivalent to BigInteger.TEN
            byte[] b1 = { 0x0a };

            //   Second byte array is equivalent to BigInteger("55 5555 5555 5555 5555")
            //   (spaces are there for human parsing).
            //   Equivalent to hex: 0x 07 b5 ba d5 95 e2 38 e3
            byte[] b2 = { 0x07, 0xb5, 0xba, 0xd5, 0x95, 0xe2, 0x38, 0xe3 };

            RawInt128 rawInt1 = new RawInt128(b1);
            RawInt128 rawInt2 = new RawInt128(b2);

            psw.WriteRawInt128(0, rawInt1);
            psw.WriteRawInt128(0, rawInt2);

            dw.Flush();
            dw.Close();
        }
Example #20
0
        public void TestReadDoubleArray()
        {
            la      = new LongSortedList();
            la[1L]  = 1.0;
            la[3L]  = 3.0;
            la[20L] = 20.0;

            DataWriter      writer    = new DataWriter(stream);
            PofStreamWriter pofWriter = new PofStreamWriter(writer, ctx);

            pofWriter.WriteLongArray(0, new LongSortedList());
            pofWriter.WriteLongArray(0, la);
            pofWriter.WriteLongArray(0, la, typeof(double));

            stream.Position = 0;
            DataReader      reader    = new DataReader(stream);
            PofStreamReader pofReader = new PofStreamReader(reader, ctx);

            double[] resFloatArray = pofReader.ReadDoubleArray(0);
            Assert.AreEqual(0, resFloatArray.Length);

            resFloatArray = pofReader.ReadDoubleArray(0);
            Assert.AreEqual(la.LastIndex + 1, resFloatArray.Length);

            Assert.AreEqual(la[1L], resFloatArray[1]);
            Assert.AreEqual(la[3L], resFloatArray[3]);
            Assert.AreEqual(la[20L], resFloatArray[20]);

            //uniform sparse array
            resFloatArray = pofReader.ReadDoubleArray(0);
            Assert.AreEqual(la.LastIndex + 1, resFloatArray.Length);

            Assert.AreEqual(la[1L], resFloatArray[1]);
            Assert.AreEqual(la[3L], resFloatArray[3]);
            Assert.AreEqual(la[20L], resFloatArray[20]);
        }
 private void initPOFWriter()
 {
     stream    = new MemoryStream();
     writer    = new DataWriter(stream);
     pofwriter = new PofStreamWriter(writer, new SimplePofContext());
 }
Example #22
0
        public void TestWriteGenericList()
        {
            var ctx = new SimplePofContext();

            ctx.RegisterUserType(101, typeof(PortablePersonLite), new PortableObjectSerializer(101));
            ctx.RegisterUserType(102, typeof(PortablePerson), new PortableObjectSerializer(102));
            ctx.RegisterUserType(103, typeof(EvolvablePortablePerson), new PortableObjectSerializer(103));

            //initPOFWriter();
            stream    = new MemoryStream();
            writer    = new DataWriter(stream);
            pofwriter = new PofStreamWriter(writer, ctx);

            ICollection <string> list1 = new List <string>();
            ICollection <object> list2 = new List <object>();

            list1.Add("A"); list1.Add(null); list1.Add("7");
            list2.Add("A"); list2.Add(null); list2.Add(7);

            ICollection <IPortableObject> persons  = new List <IPortableObject>();
            ICollection <PortablePerson>  persons2 = new List <PortablePerson>();
            var ivan = new PortablePerson("Ivan", new DateTime(1978, 4, 25));

            ivan.Children = null;
            var goran = new PortablePerson("Goran", new DateTime(1982, 3, 3));

            goran.Children = null;
            var aleks = new EvolvablePortablePerson("Aleks", new DateTime(1974, 8, 24));

            aleks.Children    = new EvolvablePortablePerson[1];
            aleks.Children[0] = new EvolvablePortablePerson("Ana Maria", new DateTime(2004, 8, 14));
            aleks.DataVersion = 2;

            persons.Add(ivan);
            persons.Add(aleks);
            persons.Add(goran);
            persons.Add(null);
            persons2.Add(ivan);
            persons2.Add(null);
            persons2.Add(goran);


            pofwriter.WriteCollection(0, list1);
            pofwriter.WriteCollection(0, list2);
            pofwriter.WriteCollection(0, persons);
            pofwriter.WriteCollection(0, persons);
            pofwriter.WriteCollection(0, (ICollection)persons2, typeof(PortablePerson));

            //initPOFReader();
            stream.Position = 0;
            reader          = new DataReader(stream);
            pofreader       = new PofStreamReader(reader, ctx);

            ICollection <string> result1 = new List <string>();

            pofreader.ReadCollection(0, result1);
            Assert.AreEqual(3, result1.Count);
            for (int i = 0; i < result1.Count; i++)
            {
                Assert.AreEqual(((List <string>)list1)[i], ((List <string>)result1)[i]);
            }

            ICollection <object> result2 = new List <object>();

            pofreader.ReadCollection(0, result2);
            Assert.AreEqual(3, result2.Count);
            for (int i = 0; i < result2.Count; i++)
            {
                Assert.AreEqual(((List <object>)list2)[i], ((List <object>)result2)[i]);
            }

            ICollection <IPortableObject> result3 = new List <IPortableObject>();

            pofreader.ReadCollection(0, result3);
            Assert.AreEqual(4, result3.Count);
            Assert.IsFalse(((List <IPortableObject>)result3)[0] is EvolvablePortablePerson);
            Assert.IsTrue(((List <IPortableObject>)result3)[1] is EvolvablePortablePerson);
            Assert.IsFalse(((List <IPortableObject>)result3)[2] is EvolvablePortablePerson);
            Assert.AreEqual(((List <IPortableObject>)result3)[3], null);
            EvolvablePortablePerson epp = (EvolvablePortablePerson)((List <IPortableObject>)result3)[1];

            Assert.AreEqual(aleks.Name, epp.Name);
            Assert.AreEqual(aleks.Children[0].Name, epp.Children[0].Name);

            PortablePerson pp = (PortablePerson)((List <IPortableObject>)result3)[0];

            Assert.AreEqual(ivan.Name, pp.Name);
            Assert.IsNull(pp.Children);

            List <IPortableObject> result4 = (List <IPortableObject>)pofreader.ReadCollection <IPortableObject>(0, null);

            Assert.AreEqual(4, result4.Count);
            Assert.IsFalse(result4[0] is EvolvablePortablePerson);
            Assert.IsTrue(result4[1] is EvolvablePortablePerson);
            Assert.IsFalse(result4[2] is EvolvablePortablePerson);
            Assert.AreEqual(result4[3], null);
            epp = (EvolvablePortablePerson)result4[1];
            Assert.AreEqual(aleks.Name, epp.Name);
            Assert.AreEqual(aleks.Children[0].Name, epp.Children[0].Name);

            pp = (PortablePerson)result4[0];
            Assert.AreEqual(ivan.Name, pp.Name);
            Assert.IsNull(pp.Children);

            List <PortablePerson> result5 = (List <PortablePerson>)pofreader.ReadCollection <PortablePerson>(0, null);

            for (int i = 0; i < persons2.Count; i++)
            {
                Assert.AreEqual(((List <PortablePerson>)persons2)[i], result5[i]);
            }
        }
Example #23
0
        public void TestWriteGenericDictionary()
        {
            var ctx = new SimplePofContext();

            ctx.RegisterUserType(101, typeof(PortablePersonLite), new PortableObjectSerializer(101));
            ctx.RegisterUserType(102, typeof(PortablePerson), new PortableObjectSerializer(102));
            ctx.RegisterUserType(103, typeof(EvolvablePortablePerson), new PortableObjectSerializer(103));

            //initPOFWriter();
            stream    = new MemoryStream();
            writer    = new DataWriter(stream);
            pofwriter = new PofStreamWriter(writer, ctx);


            IDictionary <string, double> dict = new Dictionary <string, double>();

            dict.Add("A", 11.11); dict.Add("Z", 88.88); dict.Add("7", 100.1);
            IDictionary <string, string> dict2 = new Dictionary <string, string>();

            dict2.Add("ABC", "value"); dict2.Add("N", null);

            IDictionary <string, IPortableObject> persons = new Dictionary <string, IPortableObject>();
            var ivan = new PortablePerson("Ivan", new DateTime(1978, 4, 25));

            ivan.Children = null;
            var goran = new PortablePerson("Goran", new DateTime(1982, 3, 3));

            goran.Children = null;
            var aleks = new EvolvablePortablePerson("Aleks", new DateTime(1974, 8, 24));

            aleks.Children    = new EvolvablePortablePerson[1];
            aleks.Children[0] = new EvolvablePortablePerson("Ana Maria", new DateTime(2004, 8, 14));
            aleks.DataVersion = 2;

            persons.Add("key1", ivan);
            persons.Add("key2", aleks);
            persons.Add("key3", goran);

            pofwriter.WriteDictionary(0, dict);
            pofwriter.WriteDictionary(0, dict2);
            pofwriter.WriteDictionary(0, persons);
            pofwriter.WriteDictionary(0, persons);

            //initPOFReader();
            stream.Position = 0;
            reader          = new DataReader(stream);
            pofreader       = new PofStreamReader(reader, ctx);

            IDictionary <string, double> result = new Dictionary <string, double>();

            pofreader.ReadDictionary(0, result);
            Assert.AreEqual(3, result.Count);
            foreach (string key in dict.Keys)
            {
                Assert.AreEqual(dict[key], result[key]);
            }

            IDictionary <string, string> result2 = new Dictionary <string, string>();

            pofreader.ReadDictionary(0, result2);
            Assert.AreEqual(2, result2.Count);
            foreach (string key in dict.Keys)
            {
                Assert.AreEqual(dict[key], result[key]);
            }

            IDictionary <string, IPortableObject> result3 = new Dictionary <string, IPortableObject>();

            pofreader.ReadDictionary(0, result3);
            Assert.AreEqual(3, result3.Count);
            Assert.IsFalse(result3["key1"] is EvolvablePortablePerson);
            Assert.IsTrue(result3["key2"] is EvolvablePortablePerson);
            Assert.IsFalse(result3["key3"] is EvolvablePortablePerson);
            EvolvablePortablePerson epp = (EvolvablePortablePerson)result3["key2"];

            Assert.AreEqual(aleks.Name, epp.Name);
            Assert.AreEqual(aleks.Children[0].Name, epp.Children[0].Name);

            var pp = (PortablePerson)result3["key3"];

            Assert.AreEqual(goran.Name, pp.Name);
            Assert.IsNull(pp.Children);

            IDictionary <string, IPortableObject> result4 = pofreader.ReadDictionary <string, IPortableObject>(0, null);

            Assert.AreEqual(3, result4.Count);
            Assert.IsFalse(result4["key1"] is EvolvablePortablePerson);
            Assert.IsTrue(result4["key2"] is EvolvablePortablePerson);
            Assert.IsFalse(result4["key3"] is EvolvablePortablePerson);
            epp = (EvolvablePortablePerson)result4["key2"];
            Assert.AreEqual(aleks.Name, epp.Name);
            Assert.AreEqual(aleks.Children[0].Name, epp.Children[0].Name);

            pp = (PortablePerson)result4["key3"];
            Assert.AreEqual(goran.Name, pp.Name);
            Assert.IsNull(pp.Children);
        }
 public PofStreamImpl(IStream stream, PofStreamReader reader, PofStreamWriter writer)
 {
     this.stream = stream;
     this.reader = reader;
     this.writer = writer;
 }
        public void TestEvolvableObjectSerialization()
        {
            var ctxV1 = new SimplePofContext();

            ctxV1.RegisterUserType(1, typeof(EvolvablePortablePerson),
                                   new PortableObjectSerializer(1));
            ctxV1.RegisterUserType(2, typeof(Address),
                                   new PortableObjectSerializer(2));
            ctxV1.IsReferenceEnabled = true;

            var ctxV2 = new SimplePofContext();

            ctxV2.RegisterUserType(1, typeof(EvolvablePortablePerson2),
                                   new PortableObjectSerializer(1));
            ctxV2.RegisterUserType(2, typeof(Address),
                                   new PortableObjectSerializer(2));
            ctxV2.IsReferenceEnabled = true;

            var person12 = new EvolvablePortablePerson2(
                "Aleksandar Seovic", new DateTime(74, 7, 24));
            var person22 = new EvolvablePortablePerson2(
                "Ana Maria Seovic", new DateTime(104, 7, 14, 7, 43, 0));
            var person32 = new EvolvablePortablePerson2(
                "Art Seovic", new DateTime(107, 8, 12, 5, 20, 0));

            var addr    = new Address("208 Myrtle Ridge Rd", "Lutz", "FL", "33549");
            var addrPOB = new Address("128 Asbury Ave, #401", "Evanston", "IL", "60202");

            person12.Address = addr;
            person22.Address = addr;

            person12.Nationality  = person22.Nationality = "Serbian";
            person12.PlaceOfBirth = new Address(null, "Belgrade", "Serbia", "11000");
            person22.PlaceOfBirth = addrPOB;
            person32.PlaceOfBirth = addrPOB;
            person12.Children     = new EvolvablePortablePerson2[] { person22, person32 };

            initPOFWriter();
            m_pofWriter = new PofStreamWriter(m_writer, ctxV2);
            m_pofWriter.EnableReference();
            m_pofWriter.WriteObject(0, person12);

            initPOFReader();
            m_pofReader = new PofStreamReader(m_reader, ctxV1);
            var person11 = (EvolvablePortablePerson)m_pofReader.ReadObject(0);
            var person21 = new EvolvablePortablePerson(
                "Marija Seovic", new DateTime(78, 1, 20));

            person21.Address  = person11.Address;
            person21.Children = person11.Children;
            person11.Spouse   = person21;

            initPOFWriter();
            m_pofWriter = new PofStreamWriter(m_writer, ctxV1);
            m_pofWriter.EnableReference();
            m_pofWriter.WriteObject(0, person11);

            initPOFReader();
            m_pofReader = new PofStreamReader(m_reader, ctxV2);
            var person = (EvolvablePortablePerson2)m_pofReader.ReadObject(0);

            Assert.IsTrue(person12.Name.Equals(person.Name));
            Assert.IsTrue(person12.Nationality.Equals(person.Nationality));
            Assert.IsTrue(person12.DOB.Equals(person.DOB));
            Assert.IsTrue(person11.Spouse.Name.Equals(person.Spouse.Name));
            Assert.IsTrue(person12.Address.Equals(person.Address));
            Assert.IsTrue(person12.PlaceOfBirth.Equals(person.PlaceOfBirth));
            Assert.IsTrue(person.Address != person.Children[0].Address);
            Assert.IsTrue(person.Address != person.Spouse.Address);
            Assert.IsTrue(person.Children[0] != person.Spouse.Children[0]);
            Assert.IsTrue(person.Children[1] != person.Spouse.Children[1]);
        }
        public void TestReferencesInArray()
        {
            var ctx = new SimplePofContext();

            ctx.RegisterUserType(101, typeof(PortablePerson),
                                 new PortableObjectSerializer(101));
            ctx.RegisterUserType(102, typeof(PortablePersonReference),
                                 new PortableObjectSerializer(102));

            var ivan     = new PortablePersonReference("Ivan", new DateTime(78, 4, 25));
            var goran    = new PortablePersonReference("Goran", new DateTime(82, 3, 3));
            var jack     = new PortablePersonReference("Jack", new DateTime(80, 5, 25));
            var jim      = new PortablePersonReference("Jim", new DateTime(80, 5, 25));
            var siblings = new PortablePersonReference[2];

            siblings[0] = jack;
            siblings[1] = jim;

            ivan.Children     = null;
            jack.Children     = null;
            jim.Children      = null;
            goran.Children    = new PortablePerson[2];
            goran.Children[0] = new PortablePerson("Tom", new DateTime(103, 7, 5));
            goran.Children[1] = new PortablePerson("Ellen", new DateTime(105, 3, 15));
            ivan.Siblings     = siblings;
            goran.Siblings    = siblings;
            Assert.IsTrue(ivan.Siblings == goran.Siblings);

            var col1 = new Collection <IPortableObject>();

            col1.Add(ivan);
            col1.Add(goran);

            initPOFWriter();
            m_pofWriter = new PofStreamWriter(m_writer, ctx);
            m_pofWriter.EnableReference();
            m_pofWriter.WriteCollection(0, (ICollection)col1);
            m_pofWriter.WriteCollection(0, col1, typeof(PortablePersonReference));

            initPOFReader();
            m_pofReader = new PofStreamReader(m_reader, ctx);

            var result  = m_pofReader.ReadCollection(0, null);
            var result2 = m_pofReader.ReadCollection(0, null);

            Assert.IsTrue(2 == result.Count);

            IEnumerator iter = result.GetEnumerator();

            iter.MoveNext();
            var ivanR = (PortablePersonReference)iter.Current;

            iter.MoveNext();
            var goranR = (PortablePersonReference)iter.Current;

            Assert.IsFalse(ivanR.Siblings == goranR.Siblings);
            Assert.IsTrue(ivanR.Siblings[0] == goranR.Siblings[0]);
            Assert.IsTrue(ivanR.Siblings[1] == goranR.Siblings[1]);
            Assert.IsNull(ivanR.Children);

            iter = result2.GetEnumerator();
            iter.MoveNext();
            var ivanR2 = (PortablePersonReference)iter.Current;

            iter.MoveNext();
            var goranR2 = (PortablePersonReference)iter.Current;

            Assert.IsFalse(ivanR2.Siblings == goranR2.Siblings);
            Assert.IsTrue(ivanR2.Siblings[0] == goranR2.Siblings[0]);
            Assert.IsTrue(ivanR2.Siblings[1] == goranR2.Siblings[1]);
            Assert.IsNull(ivanR2.Children);
        }
Example #27
0
        public void TestWriterVersionIdWithException1()
        {
            PofStreamWriter psw = new PofStreamWriter(new DataWriter(new MemoryStream()), new SimplePofContext());

            Console.WriteLine(psw.VersionId);
        }
        public void TestReferencesInUniformArray()
        {
            var localCtx = new SimplePofContext();

            localCtx.RegisterUserType(101, typeof(PortablePerson),
                                      new PortableObjectSerializer(101));
            localCtx.RegisterUserType(102, typeof(PortablePersonReference),
                                      new PortableObjectSerializer(102));
            localCtx.RegisterUserType(201, typeof(CompositeKey),
                                      new PortableObjectSerializer(201));
            localCtx.IsReferenceEnabled = true;

            var ivan  = new PortablePersonReference("Ivan", new DateTime(78, 4, 25));
            var goran = new PortablePersonReference("Goran", new DateTime(82, 3, 3));

            ivan.Children     = null;
            goran.Children    = new PortablePerson[2];
            goran.Children[0] = new PortablePerson("Tom", new DateTime(103, 7, 5));
            goran.Children[1] = new PortablePerson("Ellen", new DateTime(105, 3, 15));
            ivan.Siblings     = new PortablePersonReference[1];
            ivan.Siblings[0]  = goran;
            goran.Siblings    = new PortablePersonReference[1];
            goran.Siblings[0] = ivan;

            IDictionary  mapPerson = new Dictionary <CompositeKey, IPortableObject>();
            const String lastName  = "Smith";
            CompositeKey key1      = new CompositeKey(lastName, "ivan"),
                         key2      = new CompositeKey(lastName, "goran");

            mapPerson.Add(key1, ivan);
            mapPerson.Add(key2, goran);

            initPOFWriter();
            m_pofWriter = new PofStreamWriter(m_writer, localCtx);
            if (localCtx.IsReferenceEnabled)
            {
                m_pofWriter.EnableReference();
            }
            m_pofWriter.WriteDictionary(0, mapPerson);

            initPOFReader();
            m_pofReader = new PofStreamReader(m_reader, localCtx);

            IDictionary mapResult = m_pofReader.ReadDictionary(0, null);

            Assert.IsTrue(2 == mapResult.Count);

            var         ivanR  = (PortablePersonReference)mapResult[key1];
            var         goranR = (PortablePersonReference)mapResult[key2];
            ICollection keySet = mapResult.Keys;
            IEnumerator iter   = keySet.GetEnumerator();

            iter.MoveNext();
            var key1R = (CompositeKey)iter.Current;

            iter.MoveNext();
            var key2R = (CompositeKey)iter.Current;

            Assert.IsFalse(key1R.PrimaryKey == key2R.PrimaryKey);
            Assert.IsTrue(ivanR.Siblings[0] == goranR);
            Assert.IsTrue(goran.Name.Equals(goranR.Name));
            Assert.IsNull(ivanR.Children);
        }
Example #29
0
 public MessageSenderImpl(PofStreamWriter pofStreamWriter, PortableObjectBoxConverter portableObjectBoxConverter)
 {
     this.pofStreamWriter            = pofStreamWriter;
     this.portableObjectBoxConverter = portableObjectBoxConverter;
 }
Example #30
0
        public void TestWriterVersionIdWithException2()
        {
            PofStreamWriter psw = new PofStreamWriter(new DataWriter(new MemoryStream()), new SimplePofContext());

            psw.VersionId = 3;
        }