2

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:

"mystring"

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 stdout it will be printed something like this::

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

it should be printing like this in stdout:

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

Community
  • 1
  • 1
spt025
  • 2,134
  • 2
  • 20
  • 26

2 Answers2

8

I had this problem also until I realized you have to use the Json::Value class accessor functions, e.g. root["stringval"] will be "mystring", but root["stringval"].asString() will be mystring.

user9645
  • 6,286
  • 6
  • 29
  • 43
0

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

spt025
  • 2,134
  • 2
  • 20
  • 26