Пример #1
0
        public void Circular1()
        {
            using (var ms = new MemoryStream())
            {
                var s = new SlimSerializer();

                var dIn = new cA
                {
                    A = 2190,
                    B = 23232
                };

                dIn.Child = new cA {
                    A = -100, B = -900, Child = dIn
                };                                             //circular reference!!!


                s.Serialize(ms, dIn);
                ms.Seek(0, SeekOrigin.Begin);

                var dOut = (cA)s.Deserialize(ms);

                Aver.AreEqual(2190, dOut.A);
                Aver.AreEqual(23232, dOut.B);
                Aver.IsNotNull(dOut.Child);
                Aver.AreEqual(-100, dOut.Child.A);
                Aver.AreEqual(-900, dOut.Child.B);
                Aver.AreSameRef(dOut, dOut.Child.Child);
            }
        }
Пример #2
0
        public void Slim_SerializeTable_TypedRows()
        {
            var tbl = new Table(Schema.GetForTypedDoc(typeof(Person)));

            for (var i = 0; i < 1000; i++)
            {
                tbl.Insert(new Person {
                    ID           = "POP{0}".Args(i),
                    FirstName    = "Oleg",
                    LastName     = "Popov-{0}".Args(i),
                    DOB          = new DateTime(1953, 12, 10),
                    YearsInSpace = 12
                });
            }

            var ser = new SlimSerializer();

            using (var ms = new MemoryStream())
            {
                ser.Serialize(ms, tbl);
                Console.WriteLine("{0} rows took {1} bytes".Args(tbl.Count, ms.Position));
                ms.Position = 0;

                var tbl2 = ser.Deserialize(ms) as Table;

                Aver.IsNotNull(tbl2);
                Aver.IsFalse(object.ReferenceEquals(tbl, tbl2));

                Aver.AreEqual(1000, tbl2.Count);

                Aver.IsTrue(tbl.SequenceEqual(tbl2));
            }
        }
Пример #3
0
        public void T1_TypeWasAdded()
        {
            using (var ms = new MemoryStream())
            {
                var s1 = new SlimSerializer();
                Assert.AreEqual(TypeRegistryMode.PerCall, s1.TypeMode);
                Assert.IsTrue(s1.IsThreadSafe);
                Assert.IsFalse(s1.BatchTypesAdded);

                s1.TypeMode = TypeRegistryMode.Batch;

                Assert.AreEqual(TypeRegistryMode.Batch, s1.TypeMode);
                Assert.IsFalse(s1.IsThreadSafe);
                Assert.IsFalse(s1.BatchTypesAdded);


                var o1 = new A1 {
                    I1 = 12
                };

                s1.Serialize(ms, o1);
                ms.Seek(0, SeekOrigin.Begin);

                var s2 = new SlimSerializer();
                s2.TypeMode = TypeRegistryMode.Batch;
                var o2 = (A1)s2.Deserialize(ms);

                Assert.AreEqual(12, o2.I1);

                Assert.IsTrue(s1.BatchTypesAdded);
                Assert.IsTrue(s2.BatchTypesAdded);
            }
        }
Пример #4
0
        public void T1_TypeWasNotAdded()
        {
            using (var ms = new MemoryStream())
            {
                var s1 = new SlimSerializer(new Type[] { typeof(A1) });//put it in globals
                s1.TypeMode = TypeRegistryMode.Batch;
                Assert.IsFalse(s1.BatchTypesAdded);


                var o1 = new A1 {
                    I1 = 12
                };

                s1.Serialize(ms, o1);
                ms.Seek(0, SeekOrigin.Begin);

                var s2 = new SlimSerializer(new Type[] { typeof(A1) });
                s2.TypeMode = TypeRegistryMode.Batch;
                var o2 = (A1)s2.Deserialize(ms);

                Assert.AreEqual(12, o2.I1);

                Assert.IsFalse(s1.BatchTypesAdded);
                Assert.IsFalse(s2.BatchTypesAdded);
            }
        }
Пример #5
0
        public void T11_DictComparer()
        {
            using (var ms = new MemoryStream())
            {
                var s = new SlimSerializer();

                var dIn = new Dictionary <int, int>(new Comparer1());
                dIn[1]  = 100;
                dIn[11] = 200;
                dIn[2]  = 300;

                s.Serialize(ms, dIn);
                ms.Seek(0, SeekOrigin.Begin);

                var dOut = (Dictionary <int, int>)s.Deserialize(ms);

                Aver.AreEqual(2, dOut.Count);

                //foreach (var kvp in dOut)
                //{
                //  Console.WriteLine(kvp.Key + "=>" + kvp.Value);
                //}

                Aver.AreEqual(200, dOut[1]);
                Aver.AreEqual(300, dOut[2]);

                dOut[1] = 100;

                Aver.AreEqual(2, dOut.Count);
                Aver.AreEqual(100, dOut[1]);
            }
        }
Пример #6
0
        public void Create_Seal_SerializeDeserialize_Read()
        {
            var names = new List <string> {
                "Kozloff", "Sergeev", "Aroyan", "Gurevich"
            };

            var parcel = new PeopleNamesParcel(new GDID(0, 123), names);

            Assert.AreEqual(ParcelState.Creating, parcel.State);
            parcel.Seal(FakeNOPBank.Instance);



            var ser = new SlimSerializer(Parcel.STANDARD_KNOWN_SERIALIZER_TYPES);

            var ms = new MemoryStream();

            ser.Serialize(ms, parcel);
            ms.Position = 0;
            var parcel2 = ser.Deserialize(ms) as PeopleNamesParcel;

            Assert.IsNotNull(parcel2);
            var payload2 = parcel2.Payload;

            Assert.IsNotNull(payload2);


            Assert.AreEqual(payload2.Count, names.Count);
            Assert.IsTrue(payload2.SequenceEqual(names));
        }
Пример #7
0
        public void TypedRows()
        {
            var tbl = new Table(Schema.GetForTypedRow(typeof(Person)));


            tbl.Insert(new Person {
                ID           = "POP1",
                FirstName    = "Oleg",
                LastName     = "Popov-1",
                DOB          = new DateTime(1953, 12, 10),
                YearsInSpace = 12
            });

            var ser = new SlimSerializer();

            using (var ms = new MemoryStream())
            {
                ser.Serialize(ms, tbl);

                ms.Position = 0;

                var tbl2 = ser.Deserialize(ms) as Table;

                Assert.IsNotNull(tbl2);
                Assert.IsFalse(object.ReferenceEquals(tbl, tbl2));

                Assert.IsFalse(object.ReferenceEquals(tbl.Schema, tbl2.Schema));


                Assert.IsTrue(tbl.Schema.IsEquivalentTo(tbl2.Schema));
            }
        }
Пример #8
0
        public void DynamicRows()
        {
            var tbl = new Table(Schema.GetForTypedRow(typeof(Person)));


            var row = new DynamicRow(tbl.Schema);

            row["ID"]           = "POP1";
            row["FirstName"]    = "Oleg";
            row["LastName"]     = "Popov-1";
            row["DOB"]          = new DateTime(1953, 12, 10);
            row["YearsInSpace"] = 12;

            tbl.Insert(row);


            var ser = new SlimSerializer();

            using (var ms = new MemoryStream())
            {
                ser.Serialize(ms, tbl);

                ms.Position = 0;

                var tbl2 = ser.Deserialize(ms) as Table;

                Assert.IsNotNull(tbl2);
                Assert.IsFalse(object.ReferenceEquals(tbl, tbl2));

                Assert.IsFalse(object.ReferenceEquals(tbl.Schema, tbl2.Schema));

                Assert.IsTrue(tbl.Schema.IsEquivalentTo(tbl2.Schema));
            }
        }
Пример #9
0
        public void NLS_InClass()
        {
            using (var ms = new MemoryStream())
            {
                var s = new SlimSerializer();

                var dIn = new nlsCls {
                    Map = new NLSMap("eng{n='name' d='description'} rus{n='имя' d='описание'}".AsLaconicConfig())
                };

                s.Serialize(ms, dIn);
                ms.Seek(0, SeekOrigin.Begin);

                var dOut = (nlsCls)s.Deserialize(ms);

                Assert.IsNotNull(dOut);

                Assert.AreEqual(2, dOut.Map.Count);
                Assert.AreEqual("name", dOut.Map.Get(NLSMap.GetParts.Name, "eng"));
                Assert.AreEqual("имя", dOut.Map.Get(NLSMap.GetParts.Name, "rus"));

                Assert.AreEqual("description", dOut.Map.Get(NLSMap.GetParts.Description, "eng"));
                Assert.AreEqual("описание", dOut.Map.Get(NLSMap.GetParts.Description, "rus"));
            }
        }
Пример #10
0
        public void Test_InjectionTarget_Serialization()
        {
            using (var app = new AzosApplication(null, BASE_CONF))
            {
                const string DATA = "lalala!";

                var target = new InjectionTarget_Root();
                target.Data = DATA;

                app.DependencyInjector.InjectInto(target);
                target.AssertInjectionCorrectness(app);

                using (var ms = new MemoryStream())
                {
                    var ser = new SlimSerializer();
                    ser.Serialize(ms, target);
                    ms.Position = 0;

                    var target2 = ser.Deserialize(ms) as InjectionTarget_Root;
                    Aver.IsNotNull(target2);           //we deserialized the instance

                    Aver.AreEqual(DATA, target2.Data); //the Data member got deserialized ok
                    Aver.AreNotSameRef(target.Data, target2.Data);
                    Aver.AreNotSameRef(DATA, target2.Data);

                    target2.AssertAllInjectionsNull();       //but all injections are transitive, hence are null
                    app.DependencyInjector.InjectInto(target2);
                    target2.AssertInjectionCorrectness(app); //and are re-hydrated again after InjectInto() call
                }
            }
        }
Пример #11
0
        public void StringMap_One_InClass()
        {
            using (var ms = new MemoryStream())
            {
                var s = new SlimSerializer();

                var dIn = new stringMapCls
                {
                    Map1 = new StringMap(false)
                    {
                        { "a", "Alex" },
                        { "b", "Boris" }
                    }
                };

                s.Serialize(ms, dIn);
                ms.Seek(0, SeekOrigin.Begin);

                var dOut = (stringMapCls)s.Deserialize(ms);

                Assert.IsNotNull(dOut);
                Assert.IsNotNull(dOut.Map1);
                Assert.IsNull(dOut.Map2);

                Assert.IsFalse(dOut.Map1.CaseSensitive);
                Assert.AreEqual(2, dOut.Map1.Count);
                Assert.AreEqual("Alex", dOut.Map1["a"]);
                Assert.AreEqual("Alex", dOut.Map1["A"]);

                Assert.AreEqual("Boris", dOut.Map1["b"]);
                Assert.AreEqual("Boris", dOut.Map1["B"]);
            }
        }
Пример #12
0
        public void StringMap_Sensitive()
        {
            using (var ms = new MemoryStream())
            {
                var s = new SlimSerializer();

                var dIn = new StringMap
                {
                    { "a", "Alex" },
                    { "b", "Boris" }
                };

                s.Serialize(ms, dIn);
                ms.Seek(0, SeekOrigin.Begin);

                var dOut = (StringMap)s.Deserialize(ms);

                Assert.IsNotNull(dOut);

                Assert.IsTrue(dOut.CaseSensitive);
                Assert.AreEqual(2, dOut.Count);
                Assert.AreEqual("Alex", dOut["a"]);
                Assert.AreEqual(null, dOut["A"]);

                Assert.AreEqual("Boris", dOut["b"]);
                Assert.AreEqual(null, dOut["B"]);
            }
        }
Пример #13
0
        public void ValidationBatchException_Serialization()
        {
            var error1 = new AzosException("error1");
            var error2 = new ArgumentException("error2");

            var got = ValidationBatchException.Concatenate(error1, error2);

            Aver.IsNotNull(got);
            var ser = new SlimSerializer();
            var ms  = new MemoryStream();

            ser.Serialize(ms, got);
            ms.Position = 0;

            var deser = ser.Deserialize(ms) as ValidationBatchException;

            Aver.IsNotNull(deser);
            Aver.AreNotSameRef(got, deser);
            Aver.IsNotNull(deser.Batch);
            Aver.AreEqual(2, deser.Batch.Count);

            Aver.IsTrue(deser.Batch[0] is AzosException);
            Aver.IsTrue(deser.Batch[1] is ArgumentException);

            Aver.AreEqual("error1", deser.Batch[0].Message);
            Aver.AreEqual("error2", deser.Batch[1].Message);
        }
Пример #14
0
        public void Slim_SerializeTable_DynamicRows()
        {
            var tbl = new Table(Schema.GetForTypedRow(typeof(Person)));

            for (var i = 0; i < 1000; i++)
            {
                var row = new DynamicRow(tbl.Schema);

                row["ID"]           = "POP{0}".Args(i);
                row["FirstName"]    = "Oleg";
                row["LastName"]     = "Popov-{0}".Args(i);
                row["DOB"]          = new DateTime(1953, 12, 10);
                row["YearsInSpace"] = 12;

                tbl.Insert(row);
            }

            var ser = new SlimSerializer();

            using (var ms = new MemoryStream())
            {
                ser.Serialize(ms, tbl);
                Console.WriteLine("{0} rows took {1} bytes".Args(tbl.Count, ms.Position));
                ms.Position = 0;

                var tbl2 = ser.Deserialize(ms) as Table;

                Assert.IsNotNull(tbl2);
                Assert.IsFalse(object.ReferenceEquals(tbl, tbl2));

                Assert.AreEqual(1000, tbl2.Count);
                Assert.IsTrue(tbl.SequenceEqual(tbl2));
            }
        }
Пример #15
0
        public void T01()
        {
            using (var ms = new MemoryStream())
            {
                var s = new SlimSerializer(SlimFormat.Instance);

                var root = new ObjectA()
                {
                    AField = -890
                };

                s.Serialize(ms, root);

                ms.Position = 0;


                var deser = s.Deserialize(ms) as ObjectA;

                Assert.IsNotNull(deser);
                Assert.IsTrue(deser.GetType() == typeof(ObjectA));

                Assert.AreEqual(-890, deser.AField);
                Assert.IsNull(deser.Another1);
                Assert.IsNull(deser.Another2);
                Assert.IsNull(deser.Another3);
                Assert.IsNull(deser.Another4);
                Assert.IsNull(deser.Another5);
                Assert.IsNull(deser.Another6);
                Assert.IsNull(deser.Another7);
                Assert.IsNull(deser.Another8);
                Assert.IsNull(deser.Another9);
                Assert.IsNull(deser.Another10);
            }
        }
Пример #16
0
        public void T08_PreserveReferenceToTheSameArray()
        {
            using (var ms = new MemoryStream())
            {
                var s = new SlimSerializer();

                var bytes   = new byte[] { 0x07, 0x12 };
                var strings = new string[] { "Veingarten", "Vecherovskiy", "Zahar Gubar'" };
                var ints    = new int[] { -100, 2345, 19044, 888889 };
                var dIn     = new C11()
                {
                    FBytes = bytes,
                    FC11   = new C11_1()
                    {
                        FBytes_1 = bytes, FStrings_1 = strings, FInts_1 = ints
                    },
                    FC12 = new C11_2()
                    {
                        FBytes_2 = bytes, FStrings_2 = strings, FInts_2 = ints
                    }
                };

                s.Serialize(ms, dIn);
                ms.Seek(0, SeekOrigin.Begin);

                var dOut = (C11)s.Deserialize(ms);

                Aver.AreSameRef(dOut.FBytes, dOut.FC11.FBytes_1);
                Aver.AreSameRef(dOut.FBytes, dOut.FC12.FBytes_2);

                Aver.AreSameRef(dOut.FC11.FStrings_1, dOut.FC12.FStrings_2);
                Aver.AreSameRef(dOut.FC11.FInts_1, dOut.FC12.FInts_2);
            }
        }
Пример #17
0
        public void T15_Struct3()
        {
            //MyStructWithReadonlyField z = new MyStructWithReadonlyField(1, 2, false);
            //object oz = z;
            //typeof(MyStructWithReadonlyField).GetField("F").SetValue(oz, true);
            //z = (MyStructWithReadonlyField)oz;
            //Console.WriteLine(z.F);
            //return;

            using (var ms = new MemoryStream())
            {
                var s = new SlimSerializer();

                var s1 = new MyStructWithReadonlyField(10, 15, true);

                s.Serialize(ms, s1);
                ms.Seek(0, SeekOrigin.Begin);

                var s2 = (MyStructWithReadonlyField)s.Deserialize(ms);

                Console.WriteLine(NFX.Serialization.JSON.JSONWriter.Write(s1));
                Console.WriteLine(NFX.Serialization.JSON.JSONWriter.Write(s2));

                Aver.AreEqual(s1.X, s2.X);
                Aver.AreEqual(s1.Y, s2.Y);
                Aver.AreEqual(s1.F, s2.F);
            }
        }
Пример #18
0
        public void T2000_TypeRegistry()
        {
            using (var ms = new MemoryStream())
            {
                var s   = new SlimSerializer();
                var tr1 = new TypeRegistry();

                Aver.IsTrue(tr1.Add(typeof(List <S3>)));
                Aver.IsTrue(tr1.Add(typeof(S3[])));
                Aver.IsTrue(tr1.Add(typeof(Tuple <string, S3>)));


                s.Serialize(ms, tr1);

                ms.Position = 0;

                var tr2 = s.Deserialize(ms) as TypeRegistry;

                Aver.IsNotNull(tr2);

                Aver.AreEqual(7, tr2.Count);//including system types
                Aver.AreObjectsEqual(typeof(Tuple <string, S3>), tr2["$6"]);
                Aver.AreObjectsEqual(typeof(S3[]), tr2["$5"]);
                Aver.AreObjectsEqual(typeof(List <S3>), tr2["$4"]);
            }
        }
Пример #19
0
        public void T3000_Dictionary_TYPE_Int_Root()
        {
            using (var ms = new MemoryStream())
            {
                var s  = new SlimSerializer();
                var d1 = new Dictionary <Type, int>();

                d1.Add(typeof(List <S3>), 1);
                d1.Add(typeof(S3[]), 20);
                d1.Add(typeof(Tuple <string, S3>), 90);


                s.Serialize(ms, d1);

                ms.Position = 0;

                var d2 = s.Deserialize(ms) as Dictionary <Type, int>;

                Aver.IsNotNull(d2);

                Aver.AreEqual(3, d2.Count);
                Aver.AreEqual(1, d2[typeof(List <S3>)]);
                Aver.AreEqual(20, d2[typeof(S3[])]);
                Aver.AreEqual(90, d2[typeof(Tuple <string, S3>)]);
            }
        }
Пример #20
0
        public void T4_Batch_WriteMany()
        {
            using (var ms = new MemoryStream())
            {
                var s1 = new SlimSerializer();
                s1.TypeMode = TypeRegistryMode.Batch;
                var o1a = new A1 {
                    I1 = 12
                };
                var o1b = new A2 {
                    I2 = 13
                };
                var o1c = new A1 {
                    I1 = 14
                };
                var o1d = new A1 {
                    I1 = 15
                };
                var o1e = new A1 {
                    I1 = 16
                };

                s1.Serialize(ms, o1a);  Assert.IsTrue(s1.BatchTypesAdded);
                s1.Serialize(ms, o1b);  Assert.IsTrue(s1.BatchTypesAdded);
                s1.Serialize(ms, o1c);  Assert.IsFalse(s1.BatchTypesAdded);
                s1.Serialize(ms, o1d);  Assert.IsFalse(s1.BatchTypesAdded);
                s1.Serialize(ms, o1e);  Assert.IsFalse(s1.BatchTypesAdded);
                ms.Seek(0, SeekOrigin.Begin);

                var buf = ms.GetBuffer();
                Console.WriteLine(buf.ToDumpString(DumpFormat.Printable, 0, (int)ms.Length));

                var s2 = new SlimSerializer();
                s2.TypeMode = TypeRegistryMode.Batch;
                var o2a = (A1)s2.Deserialize(ms); Assert.IsTrue(s2.BatchTypesAdded);
                var o2b = (A2)s2.Deserialize(ms); Assert.IsTrue(s2.BatchTypesAdded);
                var o2c = (A1)s2.Deserialize(ms); Assert.IsFalse(s2.BatchTypesAdded);
                var o2d = (A1)s2.Deserialize(ms); Assert.IsFalse(s2.BatchTypesAdded);
                var o2e = (A1)s2.Deserialize(ms); Assert.IsFalse(s2.BatchTypesAdded);

                Assert.AreEqual(12, o2a.I1);
                Assert.AreEqual(13, o2b.I2);
                Assert.AreEqual(14, o2c.I1);
                Assert.AreEqual(15, o2d.I1);
                Assert.AreEqual(16, o2e.I1);
            }
        }
Пример #21
0
            public CustomSer(SerializationInfo info, StreamingContext context)
            {
                var buf = (byte[])info.GetValue("d", typeof(byte[]));
                var ms  = new MemoryStream(buf);
                var s   = new SlimSerializer();

                Data = s.Deserialize(ms);
            }
Пример #22
0
        public void T7_CountMismatchResetBatch_WriteMany()
        {
            using (var ms = new MemoryStream())
            {
                var s1 = new SlimSerializer
                {
                    TypeMode = TypeRegistryMode.Batch
                };
                var o1a = new A1 {
                    I1 = 12
                };
                var o1b = new A2 {
                    I2 = 13
                };
                var o1c = new A1 {
                    I1 = 14
                };
                var o1d = new A1 {
                    I1 = 15
                };
                var o1e = new A1 {
                    I1 = 16
                };

                s1.Serialize(ms, o1a); Aver.IsTrue(s1.BatchTypesAdded);
                s1.Serialize(ms, o1b); Aver.IsTrue(s1.BatchTypesAdded);
                s1.Serialize(ms, o1c); Aver.IsFalse(s1.BatchTypesAdded);
                s1.Serialize(ms, o1d); Aver.IsFalse(s1.BatchTypesAdded);
                s1.Serialize(ms, o1e); Aver.IsFalse(s1.BatchTypesAdded);
                ms.Seek(0, SeekOrigin.Begin);

                var buf = ms.GetBuffer();
                buf.ToDumpString(DumpFormat.Printable, 0, (int)ms.Length).See();

                var s2 = new SlimSerializer
                {
                    TypeMode = TypeRegistryMode.Batch
                };
                var o2a = (A1)s2.Deserialize(ms); Aver.IsTrue(s2.BatchTypesAdded);
                Aver.AreEqual(12, o2a.I1);

                s2.ResetCallBatch();
                var o2b = (A2)s2.Deserialize(ms); //Exception
            }
        }
Пример #23
0
        public override object Deserialize(Stream stream)
        {
            if (m_Batching)
            {
                return(m_BatchDeser.Deserialize(stream));
            }

            return(m_Serializer.Deserialize(stream));
        }
Пример #24
0
        public void StringManyLanguages()
        {
            using (var ms = new MemoryStream())
            {
                var s = new SlimSerializer();

                var original = @"
        外国語の学習と教授

Language Learning and Teaching

Изучение и обучение иностранных языков

Tere Daaheng Aneng Karimah

語文教學・语文教学

Enseñanza y estudio de idiomas

Изучаване и Преподаване на Чужди Езици

ქართული ენის შესწავლა და სწავლება

'læŋɡwidʒ 'lɘr:niŋ ænd 'ti:tʃiŋ

Lus kawm thaib qhia

Ngôn Ngữ, Sự học,

‭‫ללמוד וללמד את השֵפה

L'enseignement et l'étude des langues

말배우기와 가르치기

Nauka języków obcych

Γλωσσική Εκμὰθηση και Διδασκαλία

‭‫ﺗﺪﺭﯾﺲ ﻭ ﯾﺎﺩﮔﯿﺮﯼ ﺯﺑﺎﻥ

Sprachlernen und -lehren

‭‫ﺗﻌﻠﻢ ﻭﺗﺪﺭﻳﺲ ﺍﻟﻌﺮﺑﻴﺔ

เรียนและสอนภาษา";


                s.Serialize(ms, original);
                ms.Seek(0, SeekOrigin.Begin);

                var got = s.Deserialize(ms) as string;

                Aver.IsNotNull(got);
                Aver.AreEqual(original, got);
            }
        }
Пример #25
0
        public void T03()
        {
          using(var ms = new MemoryStream())
          {           
            var s = new SlimSerializer(SlimFormat.Instance);
            
            var root = new ObjectA
            {
              AField = 2345,
              Another1 = new ObjectA{ AField = 9001},
              Another2 = new ObjectA{ AField = 9002},
              Another3 = new ObjectA{ AField = 9003},
              Another4 = new ObjectA{ AField = 9004},
              Another5 = new ObjectA{ AField = 9005},
              Another6 = new ObjectA{ AField = 9006},
              Another7 = new ObjectA{ AField = 9007},
              Another8 = new ObjectA{ AField = 9008},
              Another9 = new ObjectA{ AField = 9009},
              Another10 = new ObjectA{ AField = 9010},
            };
             
            s.Serialize(ms, root);

            ms.Position = 0;


            var deser = s.Deserialize(ms) as ObjectA;

            Assert.IsNotNull( deser );
            Assert.IsTrue( deser.GetType() == typeof(ObjectA));
            Assert.AreEqual(2345,  deser.AField );

            Assert.IsNotNull( deser.Another1 );
            Assert.IsNotNull( deser.Another1 );
            Assert.IsNotNull( deser.Another2 );
            Assert.IsNotNull( deser.Another3 );
            Assert.IsNotNull( deser.Another4 );
            Assert.IsNotNull( deser.Another5 );
            Assert.IsNotNull( deser.Another6 );
            Assert.IsNotNull( deser.Another7 );
            Assert.IsNotNull( deser.Another8 );
            Assert.IsNotNull( deser.Another9 );
            Assert.IsNotNull( deser.Another10 );

             Assert.AreEqual(9001,  deser.Another1.AField );
             Assert.AreEqual(9002,  deser.Another2.AField );
             Assert.AreEqual(9003,  deser.Another3.AField );
             Assert.AreEqual(9004,  deser.Another4.AField );
             Assert.AreEqual(9005,  deser.Another5.AField );
             Assert.AreEqual(9006,  deser.Another6.AField );
             Assert.AreEqual(9007,  deser.Another7.AField );
             Assert.AreEqual(9008,  deser.Another8.AField );
             Assert.AreEqual(9009,  deser.Another9.AField );
             Assert.AreEqual(9010,  deser.Another10.AField );
          }
        }
Пример #26
0
 private Transaction deserializeTransaction(byte[] buf)
 {
     lock (m_Serializer)
     {
         using (var ms = new MemoryStream(buf))
         {
             var ta = (Transaction)m_Serializer.Deserialize(ms);
             return(ta);
         }
     }
 }
Пример #27
0
        private void button2_Click(object sender, EventArgs e)
        {
            var tr = new TypeRegistry(TypeRegistry.RecordModelTypes,
                                      TypeRegistry.CommonCollectionTypes,
                                      TypeRegistry.BoxedCommonTypes,
                                      TypeRegistry.BoxedCommonNullableTypes);

            tr.Add(typeof(PatientRecord));
            tr.Add(typeof(Person2));
            tr.Add(typeof(System.Drawing.Point));
            tr.Add(typeof(TimeSpan));
            tr.Add(typeof(Kozel));

            var p1 = PatientRecord.Make <PatientRecord>(); // make();



            using (var ms = new FileStream(@"c:\NFX\PERSON2.slim", FileMode.Create))   //new MemoryStream())
            {
                var s = new SlimSerializer(tr);

                s.Serialize(ms, p1);

                var clk = Stopwatch.StartNew();

                for (var i = 1; i < 4000; i++)
                {
                    ms.Seek(0, SeekOrigin.Begin);
                    var p2 = s.Deserialize(ms);
                }
                Text = clk.ElapsedMilliseconds.ToString();

                //Text = p2.Name;
            }

            //BINARY formatterr

            using (var ms = new FileStream(@"c:\NFX\PERSON2.bin", FileMode.Create))    //new MemoryStream())
            {
                var s = new BinaryFormatter();

                s.Serialize(ms, p1);

                var clk = Stopwatch.StartNew();

                for (var i = 1; i < 4000; i++)
                {
                    ms.Seek(0, SeekOrigin.Begin);
                    var p2 = s.Deserialize(ms);
                }
                Text += "        Binary formatter: " + clk.ElapsedMilliseconds.ToString();
            }
        }
Пример #28
0
        public void T01()
        {
            using (var ms = new MemoryStream())
            {
                var s = new SlimSerializer(SlimFormat.Instance);

                var root = new ObjectA()
                {
                    Values = new List <ObjectB> { //error shows when more than 3 items is added
                        new ObjectB {
                            BField = 10000000
                        },
                        new ObjectB {
                            BField = 21
                        },
                        new ObjectB {
                            BField = 31
                        },
                        new ObjectB {
                            BField = 41
                        },
                        new ObjectB {
                            BField = 51
                        },
                        new ObjectB {
                            BField = 61
                        },
                        new ObjectB {
                            BField = 71
                        },
                        new ObjectB {
                            BField = 81
                        },
                        new ObjectB {
                            BField = 91
                        },
                        new ObjectB {
                            BField = 101
                        }
                    }
                };

                s.Serialize(ms, root);

                ms.Position = 0;

                var deser = s.Deserialize(ms) as ObjectA;

                Aver.IsNotNull(deser);
                Aver.AreEqual(root.Values.Count, deser.Values.Count);
                deser.See();
            }
        }
Пример #29
0
        public void ASCII8Nullable_Root()
        {
            using (var ms = new MemoryStream())
            {
                var s = new SlimSerializer();

                Atom?dIn = Atom.Encode("ABC123");

                s.Serialize(ms, (Atom?)null);
                s.Serialize(ms, dIn);
                ms.Seek(0, SeekOrigin.Begin);

                var dOut = (Atom?)s.Deserialize(ms);

                Aver.IsFalse(dOut.HasValue);
                dOut = (Atom?)s.Deserialize(ms);

                Aver.IsTrue(dOut.HasValue);
                Aver.AreEqual(dOut, dIn);
                Aver.AreEqual("ABC123", dOut.Value.Value);
            }
        }
Пример #30
0
        public void T05()
        {
            using (var ms = new MemoryStream())
            {
                var s = new SlimSerializer(SlimFormat.Instance);

                var root = new ObjectA();

                root.AField   = 2345;
                root.Another1 = new ObjectA {
                    AField = 27892
                };
                root.Another2 = new ObjectB {
                    AField = -278, BField = -12, Another1 = root
                };

                s.Serialize(ms, root);

                ms.Position = 0;


                var deser = s.Deserialize(ms) as ObjectA;

                Assert.IsNotNull(deser);
                Assert.IsTrue(deser.GetType() == typeof(ObjectA));
                Assert.AreEqual(2345, deser.AField);

                Assert.IsNotNull(deser.Another1);
                Assert.IsTrue(deser.Another1.GetType() == typeof(ObjectA));
                Assert.AreEqual(27892, deser.Another1.AField);

                Assert.IsNotNull(deser.Another2);
                Assert.AreEqual(-278, deser.Another2.AField);

                Assert.IsTrue(deser.Another2.GetType() == typeof(ObjectB));
                Assert.AreEqual(-12, ((ObjectB)deser.Another2).BField);

                Assert.IsNotNull(deser.Another2.Another1);
                Assert.IsTrue(object.ReferenceEquals(deser, deser.Another2.Another1));
                Assert.IsTrue(deser.Another2.GetType() == typeof(ObjectB));

                Assert.IsNull(deser.Another3);
                Assert.IsNull(deser.Another4);
                Assert.IsNull(deser.Another5);
                Assert.IsNull(deser.Another6);
                Assert.IsNull(deser.Another7);
                Assert.IsNull(deser.Another8);
                Assert.IsNull(deser.Another9);
                Assert.IsNull(deser.Another10);
            }
        }