Git Product home page Git Product logo

biser's Introduction

Biser.NET

Image of Build Image of Build NuGet Badge NuGet Badge Image of Build

Cross-platform BINARY and JSON serializer for .NET dotnet.

  • Doesn't use reflection inside, only managed code, perfectly works in MONO WASM and CoreRT where AOT compilers are used, that makes it a possible alternative to protobuf-net, MessagePack, NetJSON, JSON.NET.
  • Has the same operational speed (Benchmark) as protobuf-net and NetJSON, smaller payload and a tiny source code.
  • Has custom encoding possibilities of any complexity.
  • Different encoding/decoding scenarios for one object are supported.
  • Thread safe. No need to "warm up" serializing/encoding entities.

Integrated part of DBreeze database, used in Raft.NET

Quick start

  • Grab from NuGet Biser (or DBreeze that contains Biser), grab from Nuget BiserObjectify.
  • Let’s assume you have several objects to serialize. It is necessary to prepare them:

Call next line to create code for the serialzer:

 var resbof = BiserObjectify.Generator.Run(typeof(TS6),true, 
      @"D:\Temp\1\", forBiserBinary: true, forBiserJson: true, null);

First argument is the type of the root object to be serialized (it can contain other objects that also must be serialized). Second argument means that BiserObjectify must prepare serializer for all objects included into the root object. Third argument points to the folder where C# files per object will be created. The fourth and fifth arguments mean that we want to use both Binary and JSON serializers. The sixth argument is a HashSet (or null) with the property names that will not be serialized.

resbof variable will contain the same information that in generated files also as a Dictionary.

  • Copy generated files into your project and embed/link them to the project. Try to recompile.
  • Probably, it will be necessary to add “partial” keyword to objects that must be serialized:
 public partial class TS6
    {
        public string P1 { get; set; }
...
  • Remove BiserObjectify from your project, it will not be necessary until next time. Usage:
 TS6 t6 = new TS6()
            {                
                P1 = "dsfs",
                P2 = 456,
                P3 = DateTime.UtcNow,
                P4 = new List<Dictionary<DateTime, Tuple<int, string>>>
                    {
                        new Dictionary<DateTime, Tuple<int, string>>{
                            { DateTime.UtcNow.AddMinutes(-1), new Tuple<int, string>(12,"testvar") },
                            { DateTime.UtcNow.AddMinutes(-2), new Tuple<int, string>(125,"testvar123") }
                        },
                        new Dictionary<DateTime, Tuple<int, string>>{
                            { DateTime.UtcNow.AddMinutes(-3), new Tuple<int, string>(17,"dsfsdtestvar") },
                            { DateTime.UtcNow.AddMinutes(-4), new Tuple<int, string>(15625,"sdfsdtestvar") }
                        }
                    },
                P5 = new Dictionary<int, Tuple<int, string>> {
                     { 12, new Tuple<int, string>(478,"dsffdf") },
                     { 178, new Tuple<int, string>(5687,"sdfsd") }
                 },
                P6 = new Tuple<int, string, Tuple<List<string>, DateTime>>(445, "dsfdfgfgfg", 
                new Tuple<List<string>, DateTime>(new List<string> { "a1", "a2" }, DateTime.Now.AddDays(58))),
                P7 = new List<string> { "fgdfgrdfg", "dfgfdgdfg" },
                P8 = new Dictionary<int, List<string>> {
                        { 34,new List<string> { "drtttz","ghhtht"} },
                        { 4534,new List<string> { "dfgfghfgz","6546ghhtht"} }
                    },
					
				P25 = new Dictionary<int, List<string[,][][,,]>>[,,,][][,,]

...
}

Binary serialization:

 var serializedObjectAsByteArray = t6.BiserEncoder().Encode();
 var retoredBinaryObject= TS6.BiserDecode(serializedObjectAsByteArray);
NOTE (for Binary serializer only)
  • To have consistent data, after first serialization and storing byte[] into database - never delete serialized object/class properties.
  • To have consistent data, after first serialization and storing byte[] into database - add new properties only to the end of the object/class, after all other properties are listed.

JSON serialization:

 var jsonSettings = new Biser.JsonSettings { DateFormat = Biser.JsonSettings.DateTimeStyle.ISO };
 string prettifiedJsonString = new Biser.JsonEncoder(t6, jsonSettings)
            .GetJSON(Biser.JsonSettings.JsonStringStyle.Prettify);
 var restoredJsonObject= TS6.BiserJsonDecode(prettifiedJsonString, settings: jsonSettings);
NOTE (for JSON serializer only)
  • JSON serializer can also store multi-dimensional arrays like [,,] [,] [,,,] etc., representing it as a Tuple<List, object itself> where Item1 represents array dimensions.
Example of the TS6 object for serialization and generated by BiserObjectify Binary and JSON serializer

For the deep understanding:


[email protected]

biser's People

Contributors

hhblaze avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

biser's Issues

Handle Polymorphism in list?

It seems that I'm unable to have a list such as List that contains instances of a child class in it.

For example:

List<ParentClass>() myList;
ChildClass foo = new ChildClass();
myList.Add(foo);

Is it possible to serialize/deserialzie like this?

Initialization template for the object decoding static function has changed in v.1.7

Binary Biser decoding became faster starting from v1.7, but for that objects decoding initialization template must be changed - compiler will show errors in those parts.

Example:

 public static T4 BiserDecode(byte[] enc = null, Biser.Decoder extDecoder = null) 
        {
            Biser.Decoder decoder = null;
            if (extDecoder == null)
            {
                if (enc == null || enc.Length == 0)
                    return null;
                decoder = new Biser.Decoder(enc);
                if (decoder.CheckNull())
                    return null;
            }
            else
            {
/************************ MUST BE LIKE THIS **************************/
//STARTING FROM v1.7, init template has changed
                  if (extDecoder.CheckNull())
                    return null;
                else
                    decoder = extDecoder;
/************************************************************************/
            }

            T4m = new T4();  

            m.Data = decoder.GetByteArray();
            m.Id = decoder.GetULong();
            
            return m;
        }

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.