public void WriteRecord(int DglbId, object data) { //Code for serializing record BinarySerializer serializer = new BinarySerializer(new Type[] { data.GetType() }); byte[] serializedData; try { serializedData = serializer.SerializeData(data); } catch (SerializerException e) { throw new CommunicationException("Problems with serialization", e); } WriteData(DglbId, serializedData); }
public void TestMediumImport() { int offset = 0; BinarySerializer serializer = new BinarySerializer(); serializer.SetSerializableTypes(new Type[] { typeof(string), typeof(Int16), typeof(UInt32), typeof(Int16), typeof(string) }); Byte[] export = serializer.SerializeData(new object[] { "abcd", 100, 100000, 900, "defghi" }); object[] import = serializer.DeserializeData(export,0,ref offset); Assert.AreEqual(new object[] { "abcd", (UInt16)100, (UInt32)100000, (UInt16)900, "defghi" }, import, "defghi"); Assert.AreEqual(20, offset); }
public object ReadRecord(int DglbId, Type type) { BinarySerializer serializer = new BinarySerializer(new Type[] { type }); uint readCount; //Code for reading record try { readCount = serializer.GetSerializedDataLength(); } catch (SerializerException e) { throw new CommunicationException("Problems with getting length of serialized data", e); } //Here we read record UInt16[] receivedData = new UInt16[readCount / 2]; BPModbusDecoder decoder = new BPModbusDecoder(receivedData, DglbId, (int)DevMgmtS_dcte.mgmt_cmd.DGLB_RD_FIRST); decoder._data = ReadData(DglbId, readCount); //Here we deserialize record try { return serializer.DeserializeData(decoder.data)[0]; } catch (SerializerException e) { throw new CommunicationException("Problems with deserialization", e); } }
public void TestManyParse() { BinarySerializer serializer = new BinarySerializer(); serializer.SetSerializableTypes(new Type[] { typeof(VarByte), typeof(UInt16) }); object[] TestData= new object[]{ 5, 1, 2, 3, 4, 5 }; Byte[] export = serializer.SerializeData(TestData); int offset=0; object[] import = serializer.DeserializeData(export, 0, ref offset); Assert.AreEqual(TestData, import); serializer.SetSerializableTypes(new Type[] { typeof(VarUInt32), typeof(string), typeof(Int16), typeof(VarUInt16), typeof(Byte) }); TestData = new object[] { 5, "a", "b", "c", "d", "e", 10, 3, 1, 2, 3 }; export = serializer.SerializeData(TestData); offset = 0; import = serializer.DeserializeData(export, 0, ref offset); Assert.AreEqual(TestData, import); }
public void TestMediumExportSerialization() { BinarySerializer serializer = new BinarySerializer(); serializer.SetSerializableTypes(new Type[] { typeof(UInt16), typeof(UInt32) }); Byte[] output = serializer.SerializeData(new object[] { 1, 100000 }); string CompareData = GetBinaryString(SumByteArrays(GetData(1, typeof(UInt16)), GetData(100000, typeof(UInt32)))); Assert.AreEqual(GetBinaryString(output), CompareData); serializer.SetSerializableTypes(new Type[] { typeof(string) }); output = serializer.SerializeData(new object[] { "abcd" }); Assert.AreEqual(new byte[] { (byte)'a', (byte)'b', (byte)'c', (byte)'d', 0 }, output); serializer.SetSerializableTypes(new Type[] { typeof(string), typeof(Int16), typeof(UInt32), typeof(Int16), typeof(string) }); output = serializer.SerializeData(new object[] { "abcd", 100, 100000, 900, "defghi" }); CompareData = GetBinaryString(SumByteArrays(new byte[] { (byte)'a', (byte)'b', (byte)'c', (byte)'d', 0 }, GetData(100, typeof(Int16)), GetData(100000, typeof(Int32)), GetData(900, typeof(Int16)), new byte[] { (byte)'d', (byte)'e', (byte)'f', (byte)'g', (byte)'h', (byte)'i', 0 })); Assert.AreEqual(GetBinaryString(output), CompareData); }
public void TestClassParse() { BinarySerializer serializer = new BinarySerializer(new Type[] { typeof(test2) }); test2 t2= new test2(); t2.a1=1; t2.a2=2; t2.a3=3; t2.a4=4; int off = 0; byte[] serData= serializer.SerializeData(new object[] { t2 }); test2 desData = (test2)serializer.DeserializeData(serData, 0, ref off)[0]; Assert.AreEqual(t2.a1, desData.a1); Assert.AreEqual(t2.a2, desData.a2); Assert.AreEqual(t2.a3, desData.a3); Assert.AreEqual(t2.a4, desData.a4); }
public void TestByteSerialize() { int offset = 0; BinarySerializer serializer = new BinarySerializer(); serializer.SetSerializableTypes(new Type[] { typeof(Byte[]) }); Byte[] input = new Byte[] { 1, 2, 3, 4, 5 }; Byte[] export = serializer.SerializeData(new object[] { input }); object[] import = serializer.DeserializeData(export, 0, ref offset); Assert.AreEqual(input, import[0]); }
public void TestBasicImport() { int offset = 0; BinarySerializer serializer = new BinarySerializer(new Type[] { typeof(UInt16) }); Byte[] export = serializer.SerializeData(new object[] { 1 }); object[] import = serializer.DeserializeData(export,0,ref offset); Assert.AreEqual(new object[] { (UInt16)1 }, import); Assert.AreEqual(2, offset); }
public void TestBasicExportSerialization() { BinarySerializer serializer = new BinarySerializer(new Type[] { typeof(UInt16) }); Byte[] output = serializer.SerializeData(new object[] { 1 }); Assert.AreEqual(GetBinaryString(output), "0000000000000001"); }
public static Byte[] GetData(object data, Type type) { BinarySerializer serializer = new BinarySerializer(new Type[] { type }); Byte[] output = serializer.SerializeData(new object[] { data }); return output; }