spt025 spt025 - 4 years ago 273
C++ Question

JSONCPP is adding extra double quotes to string

I have a root in JSONcpp having string value like this.

Json::Value root;
std::string val = "{\"stringval\": \"mystring\"}";
Json::Reader reader;
bool parsingpassed = reader.parse(val, root, false);

Now when I am trying to retrieve this value using this piece of code.

Json::StreamWriterBuilder builder;
builder.settings_["indentation"] = "";
std::string out = Json::writeString(builder, root["stringval"]);

here out string ideally should be giving containing:


whereas it is giving output like this:

"\"mystring\"" \\you see this in debug mode if you check your string content

by the way if you print this value using
it will be printed something like this::

"mystring" \\ because \" is an escape sequence and prints " in stdout

it should be printing like this in

mystring \\Expected output

Any idea how to avoid this kind of output when converting json output to std::string ?
Please avoid suggesting fastwriter as it also adds newline character and it deprecated API as well.

Constraint: I do not want to modify the string by removing extra \" with string manipulation rather I am willing to know how I can I do that with JSONcpp directly.

This is StreamWriterBuilder Reference code which I have used

Also found this solution, which gives optimal solution to remove extra quotes from your current string , but I don't want it to be there in first place

Answer Source

Okay so This question did not get answer after thorough explanation as well and I had to go through JSONCPP apis and documentation for a while.

I did not find any api as of now which takes care of this scenario of extra double quote addition. Now from their wikibook I could figure out that some escape sequences might come in String. It is as designed and they haven't mentioned exact scenario.

   \" - quote
   \\ - backslash
   \/ - slash
   \n - newline
   \t - tabulation
   \r - carriage return
   \b - backspace
   \f - form feed
   \uxxxx , where x is a hexadecimal digit - any 2-byte symbol

Link Explaining what all extra Escape Sequence might come in String

Anyone coming around this if finds out better explanation for the same issue , please feel free to post your answer.Till then I guess only string manipulation is the option to remove those extra escape sequence..

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download