Take the 2-minute tour ×

I've been using JsonCpp for processing JSON objects in C++. I came across a few Java JSON parsers like gson that can convert a Java object into a JSON document and vice versa.

Is there some equivalent JSON library in C++ that does this?

share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

There are a number of C++ JSON libraries. Nosjob looks like it might do what you want. It can be extended to serialize user-defined types by overloading two template functions.

share|improve this answer
AFAIK, nojob or any of the excellent c++ json libraries cannot do that. –  d-_-b Aug 20 '12 at 22:58
add comment

I don't know of any. It would require something like reflection. How would the parser know which members of a class to match up? You have to manually translate unless you want to add reflection or rtti to your project.

share|improve this answer
add comment

Any good C++ JSON library should do this and it is sad to see that they don't -- with the exception of ThorsSerializer and apparently Nosjob as mentioned by Ben Straub.

Of course, C++ does not have reflection like Java, so you have to explicitly annotate your types:
(copied from the ThorsSerializer documentation)

struct MyClass
    int         data1;
    float       data2;
    std::string data3;
// Macro to describe persisted members generates serialization code:
JsonSerializeTraits_MAKE(void, MyClass, data1, data2, data3)

This allows you to e.g. write:

MyClass   data = {56, 23.456, "Hi there"};
std::cout << ThorsAnvil::Serializer::jsonExport(data) << "\n";
share|improve this answer
add comment

There isn't one and couldn't be, only compiler knows the information necessary for conversion, it's not exposed on language level. That said, writing a conversion program for a restricted C++ subset from or to JSON isn't difficult. I haven't seen anybody doing that though in practice, it looks like everybody is working with xml<->c++, which is a more flexible approach, and there are commercial tools for that.

share|improve this answer
add comment

Your Answer


