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:
- Newtonsoft Json.NET
- MsgPack.CLI
- Protobuf-net
- Jil
- 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]publicclassRemoteProcedureCall// deserialize me first{[DataMember(Order=1)]publicstringMethodName{get;set;}}[DataContract]publicclassRemoteProcedureCall<T>:RemoteProcedureCall{[DataMember(Order=1)]publicTArguments{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]publicclassRemoteProcedureCall{[DataMember(Order=1)]publicstringMethodName{get;set;}[DataMember(Order=2)]publicISerializedContainerArguments{get;set;}}...var serializer=newProtobufCommonSerializer();varmsg=newRemoteProcedureCall{MethodName="method"};msg.Arguments=serializer.GenerateContainer();serializer.Serialize(msg.Arguments,myFirstArgument);serializer.Serialize(msg.Arguments,mySecondArgument);serializer.Serialize(targetStream,msg);// and similarly for the reversevarmsg2=serializer.Deserialize<RemoteProcedureCall>(targetStream);varargument1=serializer.Deserialize<MyFirstArgumentType>(msg2.Arguments);