I am facing a strange problem : when i try to add a Json variable inside a for loop, it is not written properly in the output file whereas it works well outside the loop (rapidJson v0.11).
Edit : the loop is not the problem but the bug appears even only with brackets
Here is a sample of my code :
rapidjson::Document output;
output.SetObject();
rapidjson::Document::AllocatorType& allocator = output.GetAllocator();
{
std::string s1("test");
output.AddMember("test_field",s1.c_str(), allocator);
}
std::string s2("test");
output.AddMember("test_field2",s2.c_str(), allocator);
rapidjson::FileStream f(stdout);
rapidjson::PrettyWriter<rapidjson::FileStream> writer(f);
output.Accept(writer);
The output I get is :
{"test_field": "\u0000est",
"test_field2": "test"}
So there seems to be a problem with the variable added inside the bracket.. Do you have any idea where it comes from ?
Thanks to Steve I managed to find the answer :
as said in the documentation : "rapidjson provide two strategies for storing string.
So the right way to do what I wanted is :
rapidjson::Document output;
output.SetObject();
rapidjson::Document::AllocatorType& allocator = output.GetAllocator();
{
std::string s1("test");
rapidjson::Value field;
char buffer[10];
int len = sprintf(buffer, s1); .
field.SetString(buffer, len, allocator);
output.AddMember("test_field",field, allocator);
}
std::string s2("test");
output.AddMember("test_field2",s2.c_str(), allocator);
rapidjson::FileStream f(stdout);
rapidjson::PrettyWriter<rapidjson::FileStream> writer(f);
output.Accept(writer);
The output I get is now :
{"test_field": "test",
"test_field2": "test"}