s11n

JSON-based Native Object Serialization

"s11n" is a short form of the word "serialization", as described on the libs11n home page at s11n.net. The nosjob::s11n namespace holds a stripped-down version of libs11n which can serialize "native objects" (of non-JSON classes like std::map) to/from JSON. It uses templates to ensure compile-time type checking where possible, and is client-extensible by providing a couple of template specializations customized for serializing client-side native types.

Here's an example:

typedef std::map<int,double> Map;
Map map;
// populate map ...
for( int i = 0; i < 5; ++i ) {
    map[i] = i * 1.1;
}
// Then serialize it to JSON ...
// Approach #1:
// Serialize and save in one step:
nosjob::s11n::save( map, std::cout );
// Approach #2:
// First serialize:
Array target;
nosjob::s11n::serialize( target, map );
// Then save:
nosjob::s11n::save( target, std::cout );

The output (reformatted for readability):

[ {"k":0, "v":0.0}, {"k":1, "v":1.1},
  {"k":2, "v":2.2}, {"k":3, "v":3.3},
  {"k":4, "v":4.4}
]

Note that when serializing a std::map, we have to add an extra level of indirection (a list of key/value pairs instead of an Object) in order to keep as much type information as possible. In JSON, all keys are string, which means we cannot use JSON Objects to serialize maps with arbitrary key types. We do not have this extra layer with std::list and similar types.

To load a deserialized object:

Map map;
bool ok = s11n::load( map, anInputStream );
// load() also takes an input iterator range as a source.
// load() might throw on error.

Notable Features

Notable Limitations

The more notable limitations include: