jsoncpp formatting problems

KaiserJohaan picture KaiserJohaan · Sep 26, 2011 · Viewed 7.6k times · Source

I'm using jsoncpp and I'm having a problem with how the json messages are formatted when they are written using one of the Writers.

For example:

root["name"] = "monkey";
std::cout << writer.write(root) << "\n";

Gives me something formatted like this

{
    "name" : "monkey"
}

While I actually want:

{"name":"monkey"}

I've looked at the documentation and there are mentions of setIndentLength() but they don't appear in the source files, so maybe they are deprecated or something.

Anyway anyone knows how to do this?

Answer

Ramiro picture Ramiro · Jun 8, 2016

As an extension of cdunn2001's answer, there is no need to re-write default settings (.settings_). You can just override 'indentation' value of StreamWriterBuilder builder:

Json::Value json = ...
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = ""; //The JSON document is written in a single line
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(json, &std::cout);