C++ API Reference
Defining serialization formats

Here are the steps for creating a set of serialization formats. More...

Here are the steps for creating a set of serialization formats.

Create a base class for each type of serialization format which uses the first set of macros to set up its interface and then create your derived classes which implement specific formats.

Here's a full example with a class "MyClass" to be serialized in XML and JSON format. For simplicity they can be assumed to be in a single header and source file but it's not necessary.

Class declarations look like this:

class MyClassSerializer
{
DeclareSerializerFormatType( MyClassSerializer );
...
};
class MyClassSerializerXML : public MyClassSerializer
{
DeclareSerializerFormat( MyClassSerializerXML, MyClassSerializer );
...
};
class MyClassSerializerJSON : public MyClassSerializer
{
DeclareSerializerFormat( MyClassSerializerJSON, MyClassSerializer );
...
};

The implementation source file starts with this:

ImplementSerializerFormatType( MyClassSerializer );
ImplementSerializerFormat( MyClassSerializerXML, MyClassSerializer, "XML" );
ImplementSerializerFormat( MyClassSerializerJSON, MyClassSerializer, "JSON" );
...

If you wish automatic registration/deregistration include the lines below, otherwise allocate and destroy an object of these class types when you wish the registration/deregistration to happen.

static SerializerInitializer<MyClassSerializerXML> _initializerXML( MyClassSerializerXML::theFormat() );
static SerializerInitializer<MyClassSerializerJSON> _initializerJSON( MyClassSerializerJSON::theFormat() );
...

This provides a common static interface to the class types. The most common examples are to access the implementation of a particular format by name:

MyClassSerializer* xmlFormat = MyClassSerializer::formatByName( "XML" );

And to iterate over all available formats of a given type:

MyClassSerializer::FormatSet::iterator serIt;
for( serIt = MyClassSerializer::allFormats().begin();
serIt != MyClassSerializer::allFormats().end(); ++serIt )
{
MyClassSerializer* currentFormat = (*serIt);
...
}

Note that in the case of dynamically loaded code (e.g. plug-ins) you may not get the static object destructor called in the individual formats so instead you will have to dynamically allocate and destroy your SerializerInitializer<> objects on load and unload of your code.