Search code examples
c++rapidjson

Writing normal C++ String to Rapid JSON is resulting in string with backslash


string str = {"appId":"com.mymusic.app","Connectivity":True,"DistractionLevel":2,"display":True};

if(!str.empty())
{
StringBuffer bf;
PrettyWriter<StringBuffer> writer (bf);
writer.StartObject();
writer.Key("info"));
writer.String(str.c_str());
writer.EndObject();    
cout<<"request string is:" , (char *)bf.GetString());

}

cout is printing the below line with back slash

{"info":"\"appId\":\"com.mymusic.app\",\"checkConnectivity\":True,\"driverDistractionLevel\":2,\"display\":True}"}

What i was expecting is

{"info": {"appId":"com.mymusic.app","Connectivity":True,"DistractionLevel":2,"display":True} }

Solution

  • You are using the the wrong function. The String function will add a string value to the json-object and in this context the escaping of " to \" is expected.

    I think what you actually want to do is add the string as a json-sub-object. From what I found in the rapidjson documentation the function you want to use for that is RawValue.