-
Notifications
You must be signed in to change notification settings - Fork 174
Custom serialization
MessagePack for CLI built-in serialization mechanism can support most cases, but there are edge case which it does not support. For example:
- The type defines own binary representation.
- The requirement for serialization/deserialization speed is critical (note that default implementation generates straitforward code -- it has good performance).
- You do not refer *.Serialization assembly in Silverlight/WindowsPhone project to reduce application size. In this case, you must use Manually packing/unpacking because built-in serialization mechanism is contained in those assembly.
This article discuss first and second items above. See Manual packing for third item.
There are two options to implement custom serialization -- IPackable/IUnpackable
and Custom Serializer.
The easiest way to implement custom serialization is implementing IPackable
and IUnpackable
on your class. In this case, you will interatct with packers/unpackers via MessagePackObject
.
Note that serialization mechanism prefer to use registered (custom) serializer even if the type implements IPackable
and IUnpackable
because the runtime generate dedicated serializer for the type, which delegates actual serialization process to IPackable
/IUnpackable
methods.
The type must have default public constructor even if it implements IPackable
and IUnpackable
.
If you want to serialize existing types, want to share the serialization logic in a number of MessagePackSerialier
, or the type does not have default public constructor, you can implement own custom MessagePackSerializer
.
In this case, you must register its instance to the using serialization context.
To use custom serializer, you must do following:
- Create dedicated
SerializationContext
instance. - Implement custom serializer. The serializer must derived from
MessagePackSerializer<T>
, and implements its abstract methods.-
PackToCore
method. It is similar toIPackable.PackTo
method. Implement packing logic from the instance to the stream viaPacker
object. -
UnpackFromCore
method. It is similar toIUnpackable.UnpackFrom
method. Implement unpacking logic which pulls data from the stream viaUnpacker
object and deserialize to newly created instance. - Optinal
UnpackToCore
method. IfT
is collection and it has public default constructor, this method implements item feeding style deserialization. Specifically, if the hosted type hasT
type member which is read only and initialized in its constructor, the serializer will useUnpackTo
to deserialize elements for the read only collection member.
-
- Create custom serializer using dedicated serialization context.
- Register serializer instance to
SerialzationRepository
viaSerializationContext.Serializers
property.
If you use MessagePackSerializer.Create
to get built-in serializer, you must pass dedicated serialization context so that the generated serializers use registered custom serializers. Also, you must pass dedicated serialization context to use custom serializers in MessagePack RPC APIs.