Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsoncpp formatting problems

Tags:

c++

json

jsoncpp

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?

like image 310
KaiserJohaan Avatar asked Sep 26 '11 15:09

KaiserJohaan


2 Answers

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);
like image 159
Ramiro Avatar answered Sep 21 '22 02:09

Ramiro


If you use Jsoncpp version 1.1, you can use Json::FastWriter instead of Json::StyledWriter or Json::Writer :

The JSON document is written in a single line. It is not intended for 'human' consumption, but may be usefull to support feature such as RPC where bandwith is limited.

like image 25
masoud Avatar answered Sep 17 '22 02:09

masoud