public NFXSlim(TestingSystem context, IConfigSectionNode conf) : base(context, conf) { Type[] known = ReadKnownTypes(conf); //we create type registry with well-known types that serializer does not have to emit every time m_TypeRegistry = new TypeRegistry(TypeRegistry.BoxedCommonTypes, TypeRegistry.BoxedCommonNullableTypes, TypeRegistry.CommonCollectionTypes, known); m_Serializer = new SlimSerializer(m_TypeRegistry); //batching allows to remember the encountered types and hence it is a "stateful" mode //where serialization part and deserialization part retain the type registries that //get auto-updated. This mode is not thread safe if (m_Batching) { m_BatchSer = new SlimSerializer(m_TypeRegistry); m_BatchSer.TypeMode = TypeRegistryMode.Batch; m_BatchDeser = new SlimSerializer(m_TypeRegistry); m_BatchDeser.TypeMode = TypeRegistryMode.Batch; } }
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)); } }
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 ); } }
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) ); }
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 ); } }
public void Slim_SerializeTable_TypedRows() { var tbl = new Table(Schema.GetForTypedRow(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; Assert.IsNotNull( tbl2 ); Assert.IsFalse( object.ReferenceEquals(tbl ,tbl2) ); Assert.AreEqual( 1000, tbl2.Count); Assert.IsTrue( tbl.SequenceEqual( tbl2 ) ); } }
private void Initialize() { if (!base.JustInitialized) return; var typeList = new List<Type> {_primaryType}; if (_secondaryTypes != null) typeList.AddRange(_secondaryTypes); var treg = new TypeRegistry(typeList, TypeRegistry.BoxedCommonNullableTypes, TypeRegistry.BoxedCommonTypes); _serializer = new SlimSerializer(treg); JustInitialized = false; }
public void T1_ResetCallBatch() { using (var ms = new MemoryStream()) { var s1 = new SlimSerializer(); 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); Assert.IsTrue( s1.BatchTypesAdded ); s1.ResetCallBatch(); Assert.IsFalse( s1.BatchTypesAdded ); } }
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 ); } }
public void NLS_Array() { using (var ms = new MemoryStream()) { var s = new SlimSerializer(); var dIn = new NLSMap[]{ new NLSMap("eng{n='name' d='description'} rus{n='имя' d='описание'}".AsLaconicConfig()), new NLSMap("eng{n='color' d='of product'} rus{n='zvet' d='producta'}".AsLaconicConfig()), new NLSMap("eng{n='size' d='of item'} rus{n='razmer' d='tovara'}".AsLaconicConfig()) }; s.Serialize(ms, dIn); ms.Seek(0, SeekOrigin.Begin); var dOut = (NLSMap[])s.Deserialize(ms); Assert.IsNotNull(dOut); Assert.AreEqual( 3, dOut.Length); Assert.AreEqual( "name", dOut[0].Get(NLSMap.GetParts.Name, "eng")); Assert.AreEqual( "имя", dOut[0].Get(NLSMap.GetParts.Name, "rus")); Assert.AreEqual( "description", dOut[0].Get(NLSMap.GetParts.Description, "eng")); Assert.AreEqual( "описание", dOut[0].Get(NLSMap.GetParts.Description, "rus")); Assert.AreEqual( "color", dOut[1].Get(NLSMap.GetParts.Name, "eng")); Assert.AreEqual( "zvet", dOut[1].Get(NLSMap.GetParts.Name, "rus")); Assert.AreEqual( "of product", dOut[1].Get(NLSMap.GetParts.Description, "eng")); Assert.AreEqual( "producta", dOut[1].Get(NLSMap.GetParts.Description, "rus")); Assert.AreEqual( "size", dOut[2].Get(NLSMap.GetParts.Name, "eng")); Assert.AreEqual( "razmer", dOut[2].Get(NLSMap.GetParts.Name, "rus")); Assert.AreEqual( "of item", dOut[2].Get(NLSMap.GetParts.Description, "eng")); Assert.AreEqual( "tovara", dOut[2].Get(NLSMap.GetParts.Description, "rus")); } }
public void T2_PerCall_CSUM_Mismatch() { using (var ms = new MemoryStream()) { var s1 = new SlimSerializer(new Type[]{typeof(A1)}); var o1 = new A1{ I1 = 12}; s1.Serialize(ms, o1); ms.Seek(0, SeekOrigin.Begin); var s2 = new SlimSerializer(new Type[]{typeof(A2)}); var o2 = (A1)s2.Deserialize(ms); } }
public void Slim_SerializeTable_ComplexTypedRows() { var tbl = new Table(Schema.GetForTypedRow(typeof(PersonWithNesting))); for(var i=0; i<1000; i++) tbl.Insert( new PersonWithNesting{ ID = "POP{0}".Args(i), FirstName = "Oleg", LastName = "Popov-{0}".Args(i), DOB = new DateTime(1953, 12, 10), YearsInSpace = 12, LatestHistory = new HistoryItem{ ID = "111", StartDate = DateTime.Now, Description="Chaplin" }, History1 = new List<HistoryItem> { new HistoryItem{ ID = "789211", StartDate = DateTime.Now, Description="Chaplin with us" }, new HistoryItem{ ID = "234234", StartDate = DateTime.Now, Description="Chaplin with you" } }, History2 = new HistoryItem[2] }); 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 ) ); } }
public void Slim_SerializeRow_ComplexTypedRow() { var row1 = new PersonWithNesting{ ID = "A1", FirstName = "Joseph", LastName = "Mc'Cloud", DOB = new DateTime(1953, 12, 10), YearsInSpace = 12, LatestHistory = new HistoryItem{ ID = "111", StartDate = DateTime.Now, Description="Chaplin" }, History1 = new List<HistoryItem> { new HistoryItem{ ID = "789211", StartDate = DateTime.Now, Description="Chaplin with us" }, new HistoryItem{ ID = "234234", StartDate = DateTime.Now, Description="Chaplin with you" } }, History2 = new HistoryItem[2] }; var ser = new SlimSerializer(); using(var ms = new MemoryStream()) { ser.Serialize(ms, row1); Console.WriteLine("1 row took {0} bytes".Args(ms.Position)); ms.Position = 0; var row2 = ser.Deserialize(ms) as PersonWithNesting; Assert.IsNotNull( row2 ); Assert.IsFalse( object.ReferenceEquals(row1 , row2) ); Assert.AreEqual("A1", row2.ID); Assert.AreEqual("Joseph", row2.FirstName); Assert.AreEqual("Mc'Cloud",row2.LastName); Assert.AreEqual("111", row2.LatestHistory.ID); Assert.AreEqual(2, row2.History1.Count); Assert.AreEqual("234234", row2.History1[1].ID); } }
public PODSlimSerializer() { m_Serializer = new SlimSerializer(TypeRegistry.PODTypes); }
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 ); } }
public PODSlimSerializer() { m_Serializer = new SlimSerializer( TypeRegistry.PODTypes ); }
public void T4_CountMismatchResetBatch_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 ); Assert.AreEqual(12, o2a.I1); s2.ResetCallBatch(); var o2b = (A2)s2.Deserialize(ms); //Exception } }
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(); } }
private void button1_Click(object sender, EventArgs e) { var Frank = new Person { Name = "Frank Frankfurter", Age = 99, IsHappy = true, MarriageDate = App.LocalizedTime, Father = new Person { Name = "Zaxar Mai", Age = 44}, Type = typeof(System.Reflection.Assembly) }; var Marry = new Person { Name = "Marry Morra", Age = 44, IsHappy = false, MarriageDate = App.LocalizedTime, Father = Frank.Father, Type = typeof(System.Diagnostics.Stopwatch) //Buffer = new byte[] {10,13,65,65,65,65,65,65,65,65,66,66,66,66,66,66,66,66}, //Chars = new char[] {'i', ' ', 'a', 'm', ' ','!'} }; var Dodik = new Person { Name = "Dodik Kutzhenbukher", Age = 12, IsHappy = true, MarriageDate = App.LocalizedTime.AddDays(-100), Father = Frank, Mother = Marry }; Marry.Mother = Dodik;//Cycle var rec = PatientRecord.Make<PatientRecord>(); //Frank.Father.Items["Absukov"] = 123; //Frank.Father.Items["Bosurxevich"] = true; //Frank.Father.Items["Corshunovich"] = "ya est tot kto mojet byt"; //Frank.Father.Items["Zmejukka"] = " do svazi!"; //Dodik.IntArray = new int[10,10,10]; //Dodik.IntArray[5,3,4] = 67; //Dodik.IntArray[9,9,9] = 65; //Dodik.ObjArray = new object[10]; //for(int i=0; i<Dodik.ObjArray.Length; i++) // Dodik.ObjArray[i] = new Person{ Name = "Chelovek-"+(i%4).ToString(), Age = 1, Father = Dodik, IsHappy = true, MarriageDate = App.TimeSource.UTCNow}; //for(int i=0; i<10; i++) // Dodik.Relatives.Add( new Person{ Name = "Solovei-"+(i%4).ToString(), Age = 1, Father = Dodik, IsHappy = true, MarriageDate = App.TimeSource.UTCNow} ); //for(int i=0; i<1000; i++) // Dodik.Numbers.Add( 10000-i ); var tr = new TypeRegistry(TypeRegistry.RecordModelTypes, TypeRegistry.CommonCollectionTypes, TypeRegistry.BoxedCommonTypes, TypeRegistry.BoxedCommonNullableTypes); tr.Add(typeof(PatientRecord)); tr.Add(typeof(Person)); tr.Add(typeof(Person2)); tr.Add(typeof(Person[])); tr.Add(typeof(System.Collections.Generic.List<Person>)); tr.Add(typeof(System.Drawing.Point)); tr.Add(typeof(int[])); tr.Add(typeof(int[,,])); var data = PatientRecord.Make<PatientRecord>();// make(); var clk = Stopwatch.StartNew(); // for(var i=1; i<1000; i++) using (var fs = new FileStream(@"c:\NFX\SLIM.slim", FileMode.Create)) { var s = new SlimSerializer(tr); for(var i=1; i<1000; i++) { s.Serialize(fs, data);//Dodik); } } Text = clk.ElapsedMilliseconds.ToString(); clk.Restart(); using (var fs = new FileStream(@"c:\NFX\FORMATTER.bin", FileMode.Create)) { var bf = new BinaryFormatter(); for(var i=1; i<1000; i++) { bf.Serialize(fs, data);//Dodik); } } Text += " Binary formatter: " + clk.ElapsedMilliseconds.ToString(); }
public void FullLifecycle() { 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); Assert.AreEqual(ParcelState.Sealed, parcel2.State); parcel2.Open(); Assert.AreEqual(ParcelState.Modifying, parcel2.State); parcel2.Payload[1]="Boyarskiy"; parcel2.Seal(FakeNOPBank.Instance); ms.Position = 0; ser.Serialize(ms, parcel2); ms.Position = 0; var parcel3 = ser.Deserialize( ms ) as PeopleNamesParcel; Assert.IsTrue( new List<string>{"Kozloff", "Boyarskiy", "Aroyan", "Gurevich"}.SequenceEqual( parcel3.Payload) ); }
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 ); } }
/// <summary> /// Duplicates this parcel by doing a complete deep-clone of its state via serialization. /// This method is useful for making copies of the same parcel for different threads as it is thread-safe while no other thread mutates the instance, /// however Parcel instances are NOT THREAD-SAFE for parallel changes. /// The existing parcel MUST be SEALED (otherwise it can not be serialized). /// This method is also used before a call to Open() if parcel needs to be "un-opened" the cloned instance may be reverted to /// </summary> public Parcel DeepClone() { if (m_State!=ParcelState.Sealed) throw new DistributedDataAccessException(StringConsts.DISTRIBUTED_DATA_PARCEL_INVALID_OPERATION_ERROR.Args("DeepClone", GetType().FullName, m_State) ); using(var ms = new System.IO.MemoryStream(4*1024)) { var serializer = new SlimSerializer( STANDARD_KNOWN_SERIALIZER_TYPES ); serializer.Serialize(ms, this); ms.Position = 0; return serializer.Deserialize(ms) as Parcel; } }
private void button6_Click(object sender, EventArgs e) { var ms1 = new MemoryStream(); var ms2 = new MemoryStream(); var slim1 = new SlimSerializer(new TypeRegistry(new Type[]{typeof(Library), typeof(Book), typeof(Perzon), typeof(List<Book>), typeof(object[]), typeof(string[])}, TypeRegistry.CommonCollectionTypes)); slim1.TypeMode = TypeRegistryMode.Batch; slim1.Serialize(ms1, data); slim1.Serialize(ms2, data); slim1.ResetCallBatch(); ms1.Position = 0; slim1.Deserialize(ms1); var w = Stopwatch.StartNew(); for(var i=0; i<CNT; i++) { ms2.Position = 0; slim1.Deserialize(ms2); } w.Stop(); Text = string.Format("Slim deserialized {0:n2} in {1:n2} ms, {2:n2}/sec {3}bytes",CNT, w.ElapsedMilliseconds, (CNT/(double)w.ElapsedMilliseconds)*1000, ms2.Length); }
private void button1_Click(object sender, EventArgs e) { GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); var ms = new MemoryStream(); var slim = new SlimSerializer(new TypeRegistry(new Type[]{typeof(TradingRec), typeof(Library), typeof(Book), typeof(Perzon), typeof(List<Book>), typeof(object[]), typeof(string[])}, TypeRegistry.CommonCollectionTypes)); slim.TypeMode = TypeRegistryMode.Batch; var w = Stopwatch.StartNew(); for(var i=0; i<CNT; i++) { ms.Position = 0; slim.Serialize(ms, data); } w.Stop(); using (var fs =new FileStream("C:\\NFX\\SerializerForm2.slim", FileMode.Create)) { fs.Write(ms.GetBuffer(), 0, (int)ms.Length); } Text = string.Format("Slim serialized {0:n2} in {1:n2} ms, {2:n2}/sec {3}bytes",CNT, w.ElapsedMilliseconds, (CNT/(double)w.ElapsedMilliseconds)*1000, ms.Length); // MessageBox.Show( ms.GetBuffer().ToDumpString(DumpFormat.Hex,0, (int)ms.Length)); }
public void T3_Batch_CSUM_Mismatch() { using (var ms = new MemoryStream()) { var s1 = new SlimSerializer(new Type[]{typeof(A1)}); s1.TypeMode= TypeRegistryMode.Batch; var o1 = new A1{ I1 = 12}; s1.Serialize(ms, o1); ms.Seek(0, SeekOrigin.Begin); var s2 = new SlimSerializer(new Type[]{typeof(A2)}); s2.TypeMode= TypeRegistryMode.Batch; var o2 = (A1)s2.Deserialize(ms); } }
public void StateError_4() { var names = new List<string>{"Kozloff", "Sergeev", "Aroyan", "Gurevich"}; var parcel = new PeopleNamesParcel(new GDID(0, 123), names); Assert.AreEqual(ParcelState.Creating, parcel.State); //not sealed var ser = new SlimSerializer( Parcel.STANDARD_KNOWN_SERIALIZER_TYPES ); var ms = new MemoryStream(); ser.Serialize(ms, parcel);//can not serialize open parcel }
private void button3_Click(object sender, EventArgs e) { const int CNT = 1000; var lst = new TwitterMsg[CNT]; for(var i=0; i<CNT; i++) lst[i] = ( new TwitterMsg { ID = new GDID(0, (ulong)i), AuthorID = new GDID(0, (ulong)i*251), Banned = i%45==0, Rating = i%5, ResponseToMsg = new GDID(0, (ulong)i), MsgText = NFX.Parsing.NaturalTextGenerator.Generate(0), When = DateTime.Now, ri_Deleted = false, ri_Host = "Zukini1234", ri_Version = DateTime.Now }); var tr = new TypeRegistry(TypeRegistry.RecordModelTypes, TypeRegistry.CommonCollectionTypes, TypeRegistry.BoxedCommonTypes, TypeRegistry.BoxedCommonNullableTypes); tr.Add( typeof(GDID)); tr.Add( typeof(TwitterMsg)); var clk = Stopwatch.StartNew(); using (var fs = new FileStream(@"c:\NFX\SLIM.slim", FileMode.Create, FileAccess.Write, FileShare.None, 1024*1024)) { var s = new SlimSerializer(tr); s.Serialize(fs, lst); } Text = "SLIM took {0} ms ".Args(clk.ElapsedMilliseconds); clk.Restart(); using (var fs = new FileStream(@"c:\NFX\FORMATTER.bin", FileMode.Create, FileAccess.Write, FileShare.None, 1024*1024)) { var bf = new BinaryFormatter(); bf.Serialize(fs, lst); } Text += " Binary formatter took {0}ms ".Args(clk.ElapsedMilliseconds); }
public void TZ9999_StringInVariousLanguages() { using (var ms = new MemoryStream()) { var s1 = new SlimSerializer(); var o1 = new A3{ Text = "Hello 久有归天愿,Աեցեհի, Не менее 100 советских самолетов поднялись в воздух, asağıda yağız yer yaratıldıkta. I Agree!"}; Console.WriteLine(o1.Text); s1.Serialize(ms, o1); ms.Seek(0, SeekOrigin.Begin); var s2 = new SlimSerializer(); var o2 = (A3)s2.Deserialize(ms); Console.WriteLine(o2.Text); Assert.AreEqual(o1.Text, o2.Text); } }
private byte[] serialize(object payload, out int payloadSize, out byte serVersion) { var stream = getTLWriteStream(); var serializer = ts_WriteSerializer; if (serializer==null || serializer.Owner != this) { serializer = new SlimSerializer(m_CurrentTypeRegistry, SlimFormat.Instance); serializer.Owner = this; serializer.TypeMode = TypeRegistryMode.Batch; ts_WriteSerializer = serializer; } while(Running) { stream.Position = 0; serializer.Serialize(stream, payload); if (!serializer.BatchTypesAdded) break; TypeRegistry newReg; lock(m_CurrentTypeRegistryLock) { newReg = new TypeRegistry( m_CurrentTypeRegistry ); foreach(var t in serializer.BatchTypeRegistry) newReg.Add( t ); m_CurrentTypeRegistry = newReg; } //re-allocate serializer with the new type registry serializer = new SlimSerializer(newReg, SlimFormat.Instance); serializer.Owner = this; serializer.TypeMode = TypeRegistryMode.Batch; ts_WriteSerializer = serializer; }//while payloadSize = (int)stream.Position; serVersion = 0;//not used for now return stream.GetBuffer(); }
private object deserialize(byte[] data, int addr, int payloadSize) { var stream = getTLReadStream(data, addr, payloadSize); var serializer = ts_ReadSerializer; if (serializer==null || serializer.Owner!=this || serializer.__globalTypeRegistry!=m_CurrentTypeRegistry) { serializer = new SlimSerializer(m_CurrentTypeRegistry, SlimFormat.Instance); serializer.Owner = this; serializer.TypeMode = TypeRegistryMode.Batch; ts_ReadSerializer = serializer; } try { return serializer.Deserialize(stream); } catch(SlimDeserializationException e) { if (!(e.InnerException is SlimInvalidTypeHandleException)) throw; serializer = new SlimSerializer(m_CurrentTypeRegistry, SlimFormat.Instance); serializer.Owner = this; serializer.TypeMode = TypeRegistryMode.Batch; ts_ReadSerializer = serializer; return serializer.Deserialize(stream); } }
public void T06() { using(var ms = new MemoryStream()) { var s = new SlimSerializer(SlimFormat.Instance); var root = new ObjectA[3]; root[0] = null; root[1] = new ObjectA(); root[2] = new ObjectB(); root[1].AField = 2345; root[1].Another1 = new ObjectA{ AField = 27892}; root[1].Another2 = new ObjectB{ AField = -278, BField = -12, Another1 = root[1]}; root[2].AField = 2345; ((ObjectB)root[2]).BField = 900333; root[2].Another1 = new ObjectA{ AField = 8000000}; root[2].Another2 = new ObjectB{ AField = -278, BField = -1532, Another1 = root[2]}; root[2].Another9 = root[1]; 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( 3, deser.Length); Assert.IsNull(deser[0]); Assert.IsNotNull(deser[1]); Assert.IsNotNull(deser[2]); Assert.AreEqual(2345, deser[1].AField ); Assert.IsNotNull( deser[1].Another1 ); Assert.IsTrue( deser[1].Another1.GetType() == typeof(ObjectA)); Assert.AreEqual(27892, deser[1].Another1.AField ); Assert.IsNotNull( deser[1].Another2 ); Assert.AreEqual(-278, deser[1].Another2.AField ); Assert.IsTrue( deser[1].Another2.GetType() == typeof(ObjectB)); Assert.AreEqual(-12, ((ObjectB)deser[1].Another2).BField ); Assert.IsNotNull( deser[1].Another2.Another1 ); Assert.IsTrue( object.ReferenceEquals(deser[1], deser[1].Another2.Another1)); Assert.IsTrue( deser[1].Another2.GetType() == typeof(ObjectB)); Assert.IsNull( deser[1].Another3 ); Assert.IsNull( deser[1].Another4 ); Assert.IsNull( deser[1].Another5 ); Assert.IsNull( deser[1].Another6 ); Assert.IsNull( deser[1].Another7 ); Assert.IsNull( deser[1].Another8 ); Assert.IsNull( deser[1].Another9 ); Assert.IsNull( deser[1].Another10 ); Assert.AreEqual(2345, deser[2].AField ); Assert.AreEqual(900333, ((ObjectB)deser[2]).BField ); Assert.IsNotNull( deser[2].Another1 ); Assert.IsTrue( deser[2].Another1.GetType() == typeof(ObjectA)); Assert.AreEqual(8000000, deser[2].Another1.AField ); Assert.IsNotNull( deser[2].Another2 ); Assert.AreEqual(-278, deser[2].Another2.AField ); Assert.IsTrue( deser[2].Another2.GetType() == typeof(ObjectB)); Assert.AreEqual(-1532, ((ObjectB)deser[2].Another2).BField ); Assert.IsNotNull( deser[2].Another2.Another1 ); Assert.IsTrue( object.ReferenceEquals(deser[2], deser[2].Another2.Another1)); Assert.IsTrue( deser[2].Another2.GetType() == typeof(ObjectB)); Assert.IsNull( deser[2].Another3 ); Assert.IsNull( deser[2].Another4 ); Assert.IsNull( deser[2].Another5 ); Assert.IsNull( deser[2].Another6 ); Assert.IsNull( deser[2].Another7 ); Assert.IsNull( deser[2].Another8 ); Assert.IsNotNull( deser[2].Another9 ); Assert.IsNull( deser[2].Another10 ); Assert.IsTrue( object.ReferenceEquals(deser[1], deser[2].Another9) ); } }