AlexV
AlexV

Reputation: 23088

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 {}...

Upvotes: 3

Views: 11445

Answers (2)

InTERpLAY
InTERpLAY

Reputation: 129

It may be helpful for someone: In current ver. that i'm using - 1.8.2, you can get empty Object like this:

Json::Value root;
root = Json::objectValue;

Upvotes: 6

user932887
user932887

Reputation:

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?

Upvotes: 5

Related Questions