A Flexible Serialization Library

December 17, 2012

What I need in one of my projects, is an serialization library for C# objects, that is independent on the encoding / decoding mechanism and offers quite a bit of flexibility beyond that.

This library should cover all ranges from an offset / byte-oriented fixed length byte records in network byte order to simple unordered name/value lines but in which the names can be different from the property / field name.

So what's needed is a solution that supports a number of different configuration aspects:

Attributes

Every public field or property of a type should be encoded / decoded by default.

The attributes that can be put on the types, fields, and properties need to be extensible, but at the same time easily accessible. So there is some Reflection magic needed here. Performance comes later.

Type declaration independent configuration

Usually, attributes put quite a cognitive burden on these small little DTO objects. So a fluent interface should be provided, that allows a per type and per member configuration.

And because the Attribute classes already exist, they could be reused for the DSL based configuration. To help the caller to associate these Attribute instances with their members, the fluent interface should be simplified by extension methods.

All field or properties should be referred by lambda expressions, so that renaming a member does not affect the configuration.

Encoder/Decoder configuration, per type / primitive type

The encoding mechanism needs to be completely independent of the library. So we need type encoder / decoders that can be injected.

Composite encoders should drive the operation. They should be capable to iterate over the fields and properties and decide for each what to do.

Element encoders and decoders can be specified individually. So that for simple formats that require only a few of the numerous C# types, only the encoders for the ones that are actually used need to be specified.

Wrapping Up

I've written this post a few days ago, and had time to start implementing a library based on these requirements. If you have some time left, check it out on Github and tell me what you think. I will make an announcement as soon it looks more promising.