A while ago I’ve started a convenience, zero error-handling, json serializer wrapper around picojson based on the Boost.Serialization and hiberlite APIs. Today the wrapper got even less intrusive for DTOs that expose their members.
Given a class that cannot be extended in its declaration, but the internals are accessible (following to the Boost.Serialization free function API):
struct Untouchable {
int value;
};
With a free function defined in the ::picojson::convert namespace,
namespace picojson {
namespace convert {
template <class Archive>
void json(Archive &ar, Untouchable &u) {
ar &picojson::convert::member("value", u.value);
}
}
}
serialization is “again” possible:
Untouchable example = { 42 };
std::string example_string( picojson::convert::to_string(example) );
Untouchable example_deserialized = { 0 };
picojson::convert::from_string( example_string, example_deserialized );
CHECK( example.value == example_deserialized.value );