Search code examples
c++jsoncpp

JsonCpp: How to get en empty object in a Json::Value?


I have an external library that receive a Json::Value as a parameter. It will then output a string containing a JSON structure with my passed Json::Value somewhere in that structure.

I'm trying to put an empty object {} in that Json::Value with JsonCpp.

Ultimately I'm trying to parse that empty JSON object from a std::string that contain "{}" and I need that in a Json::Value.

When I declare a Json::Value and do nothing with it and pass it to my library, I get NULL instead of {}...


Solution

  • Sample:

    Json::Value root;
    Json::Reader reader;
    reader.parse("{}", root);
    assert(root != Json::nullValue);
    std::string someStr = Json::FastWriter().write(root);
    assert(someStr == "{}\n");
    

    All of the assertions pass, so the parsed object doesn't have a null value and when you write it back to string you get your empty object "{}" back. Did this not work for you?