Using libraries like jsoncpp
to serialize my C++ objects and data (e.g. to plot them in Python), I really get tired of all the loops in my code to convert std::vector< double >
s into Json::Value
s. I end up re-typing the same lines again and again. Therefore I'd like to make this a little easier.
I have two use cases: Convert STL
types (at least std::vector
, std::map
) from and to Json::Value
, and do this with my own data types as well.
Solution for own data types:
- define constructor expecting
const Json::Value&
, the constructor then tries to fill the object with data or throws exceptions if the passed value does not meet specific requirements - define
operator Json::Value()
as a member function of my class.
This way, I should be able to write stuff like that:
MyObj o;
Json::Value root;
root["foo"] = o;
MyObj reconstructed(root["foo"]);
But how should I do this with STL
types? Write explicit functions, maybe templates, or should I do the same like for my own data types, except that I use free functions instead of member functions? What is the best practice?