Serialization is a touchy topic in C++...
Quick question:
- Serialization: short-lived structure, one encoder/decoder
- Messaging: longer life, encoders / decoders in multiple languages
The 2 are useful, and have their use.
Boost.Serialization is the most recommended library for serialization usually, though the odd choice of operator&
which serializes or deserializes depending on the const-ness is really an abuse of operator overloading for me.
For messaging, I would rather suggest Google Protocol Buffer. They offer a clean syntax for describing the message and generate encoders and decoders for a huge variety of languages. There are also one other advantage when performance matters: it allows lazy deserialization (ie only part of the blob at once) by design.
Moving on
Now, as for the details of implementation, it really depends on what you wish.
- You need versionning, even for regular serialization, you'll probably need backward compatibility with the previous version anyway.
- You may, or may not, need a system of
tag
+ factory
. It's only necessary for polymorphic class. And you will need one factory
per inheritance tree (kind
) then... the code can be templatized of course!
- Pointers / References are going to bite you in the ass... they reference a position in memory that changes after deserialization. I usually choose a tangent approach: each object of each
kind
is given an id
, unique for its kind
, and so I serialize the id
rather than a pointer. Some framework handles it as long as you don't have circular dependency and serialize the objects pointed to / referenced first.
Personally, I tried as much as I can to separate the code of serialization / deserialization from the actual code that runs the class. Especially, I try to isolate it in the source files so that changes on this part of the code does not annihilate the binary compatibility.
On versionning
I usually try to keep serialization and deserialization of one version close together. It's easier to check that they are truly symmetric. I also try to abstract the versionning handling directly in my serialization framework + a few other things, because DRY should be adhered to :)
On error-handling
To ease error-detection, I usually use a pair of 'markers' (special bytes) to separate one object from another. It allows me to immediately throw during deserialization because I can detect a problem of desynchronization of the stream (ie, somewhat ate too much bytes or did not ate sufficiently).
If you want permissive deserialization, ie deserializing the rest of the stream even if something failed before, you'll have to move toward byte-count: each object is preceded by its byte-count and can only eat so much byte (and is expected to eat them all). This approach is nice because it allows for partial deserialization: ie you can save the part of the stream required for an object and only deserialize it if necessary.
Tagging (your class IDs) is useful here, not (only) for dispatching, but simply to check that you are actually deserializing the right type of object. It also allows for pretty error messages.
Here are some error messages / exceptions you may wish:
No version X for object TYPE: only Y and Z
Stream is corrupted: here are the next few bytes BBBBBBBBBBBBBBBBBBB
TYPE (version X) was not completely deserialized
Trying to deserialize a TYPE1 in TYPE2
Note that as far as I remember both Boost.Serialization
and protobuf
really help for error/version handling.
protobuf
has some perks too, because of its capacity of nesting messages:
- the byte-count is naturely supported, as well as the versionning
- you can do lazy deserialization (ie, store the message and only deserialize if someone asks for it)
The counterpart is that it's harder to handle polymorphism because of the fixed format of the message. You have to carefully design them for that.