Pure C# binary serializer
define class
public class SampleData{
public string strFiled = "hello world";
public UInt64 uint64 = UInt64.MaxValue;
public float[] array = new float[4]{1.0f,2.0f,3.0f,4.0f};
public System.Numerics.Vector4 vec1 = new Vector4(1.0f,0.5f,0.25f,-1.0f);
}
serialize object
var sample = new SampleData();
byte[] data = BinarySeralizer.Serialize(sample);
Console.WriteLine(string.Join(',',data));
byte array output
4,0,155,152,185,128,1,4,0,153,153,153,153,0,1,11,0,0,0,104,101,108,108,111,32,119,111,114,108,100,255,255,255,255,255,255,255,255,1,4,0,0,0,0,0,128,63,0,0,0,64,0,0,64,64,0,0,128,64,1,1,0,0,128,63,0,0,0,63,0,0,128,62,0,0,128,191
Deserialize byte array
var sampl1 = BinarySeralizer.Deserialize<SampleData>(data);
- Support array type T[].
- Only public field will be seralized/deserialize
- No attaching attributes required, class/struct in any assembly are supported.
- Byte data can be deseralized into a different type which has same field layout.
- Properties support
- Add List<> Dictionary<,> support.
- Reference loops may cause exceptions.
sample class definition:
public class CC
{
public float cc = 10;
}
public class AA
{
public int xx;
public CC c = new CC { cc = 433.9733f };
}
public class TestData
{
public CC[] ccary = null;
public AA[] aaary = new AA[0] { };
public string xxx = "DWDWW";
public AA s1 = null;
public AA[] s2 = new AA[3]{
new AA{xx = 192},
null,
new AA{xx =421},
};
public CC classc = new CC { cc = 0.315854f };
}
Result:
Json string:
{"ccary":null,"aaary":[],"xxx":"DWDWW","s1":null,"s2":[{"xx":192,"c":{"cc":433.9733}},null,{"xx":421,"c":{"cc":433.9733}}],"classc":{"cc":0.315854}}
byte array:
6,0,160,161,155,129,161,128,2,1,0,153,0,2,0,148,128,1,1,0,153,0,1,0,1,0,0,0,0,5,0,0,0,68,87,68,87,87,0,1,3,0,0,0,1,192,0,0,0,1,1,149,252,216,67,0,1,165,1,0,0,1,1,149,252,216,67,1,1,158,183,161,62
Json serialize:495679tick - 141ms
Json deseralize:171089tick - 48ms
Binary serialize:32031tick - 9ms
Binary deseralize:8841tick - 2ms
binary: 73byte json: 148byte