Giter VIP home page Giter VIP logo

commonserializer's Introduction

CommonSerializer

This library collection provides a common interface for common .NET serialization engines. It allows for quickly switching between the various serializers for comparing performance, features, etc.

These serializers are supported:

  1. Newtonsoft Json.NET
  2. MsgPack.CLI
  3. Protobuf-net
  4. Jil
  5. PowerJSON (built from fastJSON)

If there are others that you want supported, please file an issue requesting that.

The first three serializers listed support a feature for partial serialization. Consider that you might want to deserialize an outer object first so that you can determine the right data type for some inner object. The recommended method for accomplishing this works like this. You deserialize the thing twice -- the second time after you know the right data type:

[DataContract]
public class RemoteProcedureCall // deserialize me first
{
   [DataMember(Order = 1)]
   public string MethodName {get;set;}
}

[DataContract]
public class RemoteProcedureCall<T>: RemoteProcedureCall
{
   [DataMember(Order = 1)]
   public T Arguments {get;set;}
}

The interface supports a method to register inheritors: RegisterSubtype. That will be necessary for serializers like Protobuf-net (or you can reference the Protobuf-net assembly directly and use its attributes).

However, you can use partial serialization like this:

[DataContract]
public class RemoteProcedureCall
{
   [DataMember(Order = 1)]
   public string MethodName {get;set;}
   
   [DataMember(Order = 2)]
   public ISerializedContainer Arguments {get;set;}
}

...

var serializer = new ProtobufCommonSerializer();
var msg = new RemoteProcedureCall { MethodName = "method" };
msg.Arguments = serializer.GenerateContainer();
serializer.Serialize(msg.Arguments, myFirstArgument);
serializer.Serialize(msg.Arguments, mySecondArgument);
serializer.Serialize(targetStream, msg);

// and similarly for the reverse
var msg2 = serializer.Deserialize<RemoteProcedureCall>(targetStream);
var argument1 = serializer.Deserialize<MyFirstArgumentType>(msg2.Arguments);

commonserializer's People

Contributors

brannonking avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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