Serialization
Category | C++ |
---|
Overview
Serialization is the process of converting the state of an object into a format that can be stored and reconstructed later.
Goal
Read and write a set of objects from and to memory.
Serializer
The serializer is an interface for serializing data.
The format of the data in depends on the serializer implementation.
Examples of serializers:
- Binary
- JSON
- XML
- YAML
Code Generation
With a reflection system where code is generated to build type information, the serialization code can be generated as well.
Inheritance
The inheritance approach:
- Define a base type for serializable objects.
- Define a method that is overriden in serializable objects to implement the serialization of each members.
class ISerializable
{
public:
virtual void Serialize(ISerializer* serializer) abstract;
};
class MyComponent : public ISerializable
{
private:
int _int;
float _float;
Color _color;
public:
virtual void Serialize(ISerializer* serializer) override
{
serializer->Serialize(“int”, _int);
serializer->Serialize(“float”, _float);
serializer->Serialize(“color”, _color);
}
};
✔
- Data introspection is easily implemented with a special type serializer that builds type information.
- Data can be serialized and deserialized in the same method.
❌
- The serialization process is hidden in the method implementation.
- Metadata has to specified in the serialize method which complxify the implementation or it has to be provided in a separate format (for default values, ranges...).
- Serializable classes need to inherit from the serializable interface.
- POD types have to handled separately.
Schema
A different approach is to define a custom schema to describe the data.
- In development builds, the runtime reads a key-value store based on the schema. Backward and forward compatibility is easily supported.
- In final builds, the runtime reads a block of data. Backward and forward compatibility is not needed, and it is the most efficient solution.
Backward Compatibility
Backward compatibility is the ability of newer versions of a program to load data stored from older versions.
Version
You store a version number before the serialized data, and you check the current version to provide different code paths to deserialize the data.
The latest version number is generally defined in the code, and the serializer writes this value for the newly serialized data.
When the definition of the data changes, the version number is incremented.
if (version == 1)
{
...
}
else if (version == 2)
{
...
}
Forward Compatibility
Forward compatibility is the ability of older versions of a program to load data stored from newer versions.
Versioning is impossible, as it's impossible to predict the new changes related to newer versions.
- Ignore new elements.