Tag Archives: JSON

JSON++: A JSON parser for C++

I wrote a very simple JSON parser in C++ a while ago. It’s just a pair of header and source files, thus very easy to use (compile the .cc file and link with your source). Check it out here.

Here are a few examples:

string teststr(
        "{" 
        "  \"foo\" : 1," 
        "  \"bar\" : false," 
        "  \"person\" : {\"name\" : \"GWB\", \"age\" : 60}," 
        "  \"data\": [\"abcd\", 42]" 
        "}" 
               );
istringstream input(teststr);
Object o;
assert(o.parse(input));
assert(1 == o.get<long>("foo"));
assert(o.has<bool>("bar"));
assert(o.has<Object>("person"));
assert(o.get<Object>("person").has<long>("age"));
assert(o.has<Array>("data"));
assert(o.get<Array>("data").get<long>(1) == 42);
assert(o.get<Array>("data").get<string>(0) == "abcd");
assert(!o.has<long>("data"));

Caveat: It currently does not support floating-point numbers.