public override void Serialize(DmSerializer dmSerializer, object obj, Type objType) { IDictionary dictionary = obj as IDictionary; if (dictionary == null) { return; } var args = objType.GetGenericArguments(); Type keyType = args[0].GetBaseType(); Type valueType = args[1].GetBaseType(); var keySerializer = TypeSerializer.GetSerializer(keyType); var valueSerializer = TypeSerializer.GetSerializer(valueType); // write length dmSerializer.Writer.Write(dictionary.Count); // Iterate through the dictionary foreach (DictionaryEntry entry in dictionary) { object value = entry.Value; object key = entry.Key; dmSerializer.Serialize(key, keyType); dmSerializer.Serialize(value, valueType); } }
public void TestCases() { DmSerializer serializer = new DmSerializer(); foreach (var obj in SerializableObjects.GetObjects()) { try { var cliByte = serializer.Serialize(obj); var ob2 = serializer.Deserialize(cliByte); bool areEqual = false; if (EqualityHelpers.IsDictionary(obj.GetType())) { areEqual = EqualityHelpers.DictionariesAreEqual((IDictionary)obj, (IDictionary)ob2); } else if (EqualityHelpers.IsEnumerable(obj.GetType())) { areEqual = EqualityHelpers.ArraysAreEqual((IEnumerable)obj, (IEnumerable)ob2); } else { areEqual = obj.Equals(ob2); } Assert.True(areEqual, $"Type: {obj.GetType()}: Instance 1 is not equal to deserialized instance."); } catch (Exception ex) { Assert.True(false, ($"Object 1 Type : {obj.GetType()} - is not serializable. ({ex.Message})")); } } }
public override void Serialize(DmSerializer dmSerializer, object obj, Type objType) { IEnumerable list = obj as IEnumerable; if (list == null) { return; } var valueType = GetArrayItemValueType(objType); IEnumerator e = list.GetEnumerator(); PropertyInfo pi = objType.GetProperties().Where(p => p.Name == "Count" || p.Name == "Length").FirstOrDefault(); if (pi == null) { throw new ArgumentException("an array should have a Count / Length property"); } int count = (int)pi.GetValue(obj); // write length dmSerializer.Writer.Write(count); // Iterate through the dictionary while (e.MoveNext()) { object value = e.Current; dmSerializer.Serialize(value, valueType); } }
private async static Task TestWebPostStream() { var dmTable = GetATestTable(); var dmSet = new DmSet(); dmSet.Tables.Add(dmTable); var surrgotabeTable = new DmSetSurrogate(dmSet); DmSerializer serializer = new DmSerializer(); var binaryData = serializer.Serialize(surrgotabeTable); Uri target = new Uri("http://localhost:5000/api/sync"); var client = new HttpClient(); ByteArrayContent arrayContent = new ByteArrayContent(binaryData); var response = await client.PostAsync(target, arrayContent); if (response.IsSuccessStatusCode) { using (var stream = await response.Content.ReadAsStreamAsync()) { var ds = serializer.Deserialize <DmSetSurrogate>(stream); var newDs = ds.ConvertToDmSet(); } } }
public override void Serialize(DmSerializer dmSerializer, object obj, Type objType) { var context = new StreamingContext(); var value = obj as ISerializable; SerializationInfo serializationInfo = new SerializationInfo(objType, new FormatterConverter()); value.GetObjectData(serializationInfo, context); // write length dmSerializer.Writer.Write(serializationInfo.MemberCount); foreach (SerializationEntry serializationEntry in serializationInfo) { var entryName = serializationEntry.Name; var entryValue = serializationEntry.Value; dmSerializer.Serialize(entryName, entryName.GetType()); dmSerializer.Serialize(entryValue, entryValue.GetType()); } }
public static void Serialize(DmSetSurrogate set, string fileName) { DmSerializer serializer = new DmSerializer(); FileInfo fi = new FileInfo(fileName); if (!Directory.Exists(fi.Directory.FullName)) { Directory.CreateDirectory(fi.Directory.FullName); } // Serialize on disk. using (var f = new FileStream(fileName, FileMode.CreateNew, FileAccess.ReadWrite)) { serializer.Serialize(set, f); } }
public override void Serialize(DmSerializer dmSerializer, object obj, Type objType) { // Get the serialized members var members = DmUtils.GetMembers(objType); //var ctorParameters = objType.GetConstructorParameters(true); //if (ctorParameters != null) // members = DmUtils.GetMembersOrderedByParametersForConstructor(members, ctorParameters); foreach (MemberInfo member in members) { object memberValue = member.GetValue(obj); var memberType = member.GetMemberType(); dmSerializer.Serialize(memberValue, memberType); } }
public void AddingAConverter() { ClassWithAConverter cc = new ClassWithAConverter(); cc.ID = 12; cc.LastName = "Pertus"; cc.Ptr = new IntPtr(12222); DmSerializer serializer = new DmSerializer(); serializer.RegisterConverter(typeof(ClassWithAConverter), new ClassConverterForClassWithAConverter()); var b = serializer.Serialize(cc); var cc2 = serializer.Deserialize <ClassWithAConverter>(b); Assert.Equal(cc, cc2); }
static void Main(string[] args) { Address clientAddress = new Address { City = "Toulouse", PostalCode = "31620" }; Client cli = new Client { Address = clientAddress, Birthday = new DateTime(1976, 10, 23), ClientId = Guid.NewGuid(), FirstName = "Sebastien", LastName = "Pertus", IsAvailable = true, Money = 10, Id = 1, }; Client deserializedClient = null; // ---------------------------------------------------------------------------- // We are on .NET 4.6.2, so using version .Net Standard 1.3 (to 1.5) // ---------------------------------------------------------------------------- DmSerializer serializer = new DmSerializer(); using (FileStream fs = new FileStream("client.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite)) { serializer.Serialize(cli, fs); } using (FileStream fs = new FileStream("client.bin", FileMode.Open, FileAccess.ReadWrite)) { deserializedClient = serializer.Deserialize <Client>(fs); } }
public override void Serialize(T obj, Stream ms) { serializer.Serialize(obj, ms); }
private static void TestSerializerSize() { DmTable dtClient = new DmTable("Client"); DmColumn dcId = new DmColumn <int>("Id"); DmColumn dcName = new DmColumn <string>("Name"); DmColumn dcBirthdate = new DmColumn <DateTime>("Date"); DmColumn dcMoney = new DmColumn <Double>("Money"); DmColumn dcInt = new DmColumn <Int64>("i64"); DmColumn dcJob = new DmColumn <String>("Job"); dtClient.Columns.AddRange(new[] { dcId, dcName, dcBirthdate, dcMoney, dcInt, dcJob }); long rowsSize = 0; foreach (var c in dtClient.Columns) { DmColumnSurrogate dcs = new DmColumnSurrogate(c); Console.WriteLine($"Column {dcs.ColumnName} Surrogate length : " + dcs.GetBytesLength()); rowsSize += dcs.GetBytesLength(); } var dmTableS = new DmTableSurrogate(dtClient); var dmTableSLength = dmTableS.GetEmptyBytesLength(); Console.WriteLine("DmTableSurrogate length = " + dmTableSLength); rowsSize += dmTableSLength; for (int i = 0; i < 50000; i++) { var newRow = dtClient.NewRow(); Random r = new Random(DateTime.Now.Second); var l = r.NextDouble() * 100; string name = ""; for (int j = 0; j < l; j++) { name += "a"; } newRow["Id"] = 12; newRow["Name"] = name; newRow["Date"] = DateTime.Now; newRow["Money"] = l; var l2 = r.NextDouble() * 100; string job = ""; for (int j = 0; j < l2; j++) { job += "b"; } newRow["Job"] = job; newRow["i64"] = l2; rowsSize += DmTableSurrogate.GetRowSizeFromDataRow(newRow); dtClient.Rows.Add(newRow); } var serializer = new DmSerializer(); long dtSize = 0; if (File.Exists("dt.bin")) { File.Delete("dt.bin"); } using (var fs = new FileStream("dt.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite)) { serializer.Serialize(new DmTableSurrogate(dtClient), fs); dtSize = fs.Length; } //using (var fs = new FileStream("dt.bin", FileMode.Open, FileAccess.Read)) //{ // var ds = serializer.Deserialize<DmTableSurrogate>(fs); //} Console.WriteLine("Rows Size : " + rowsSize); Console.WriteLine("Table Size : " + dtSize); }