Giter Site home page Giter Site logo

binaryserialization's Introduction

BinarySerialization

Pure C# binary serializer

Quick Start

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);

Features

  • 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.

Todo

  • Properties support
  • Add List<> Dictionary<,> support.
  • Reference loops may cause exceptions.

Simple Benchmark with Json.net

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

binaryserialization's People

Contributors

zcyemi avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.