All solutions, that require custom translators for strings explicitly, seem to be quite error prone for me since it's likely to forget it sometimes. It would be nice to have some kind of overload way via inheritance for the property tree's put method to handle this implicitly but that's not possible in a robust way since it's a template and you would have to ensure full covariance for all methods of the tree. Also changing boost library stuff as a workaround should be avoided in general if possible.
The most robust way without hacks I found so far is (since C++11):
- use a boost-property tree with >
- Provide a translator for your variant (details see link below!) but do not "hackify" the content in terms of JSON-specifics! These are almost totally orthogonal aspects!
- Write an own reader and writer for JSON, should be easily adapted from the Boost ones
Pros:
- no hacks required to affect the property tree in terms of JSON-specific details
- no pollution of the Boost libraries and their namespaces except the specializations of your new own types (the variant translator)
- type safer than the custom string based property tree approach
- should be faster for many runtime scenarios with non-frequent serialization of the tree
Cons:
- requires some efforts for a quite small detail of the behavior
- might be a bit slower in terms of compilation
- might be a bit slower for runtime scenarios with frequent serialization and minor changes of the tree (can be optimized for sure)
- reading the json back into the tree is some kind of philosophic work in doubt to ensure as much symmetry as possible between the used types (for many purposes rather an academic issue)
For more details, for instance see
http://marko-editor.com/articles/property_tree_store_anything/
You can easily adapt this for variant usage.