Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rapidjson proper json creation

I'm trying to create a json using rapidjson and I am having some unexpected problems with generating a proper output.

I'm creating and populating a document like this:

Document d;
d.SetObject();

rapidjson::Document::AllocatorType& allocator = d.GetAllocator();

size_t sz = allocator.Size();

d.AddMember("version",  1, allocator);
d.AddMember("testId",   2, allocator);
d.AddMember("group",    3, allocator);
d.AddMember("order",    4, allocator);

Value tests(kArrayType);
Value obj(kObjectType);
Value val(kObjectType);

obj.AddMember("id", 1, allocator);

string description = "a description";
val.SetString(description.c_str(), static_cast<SizeType>(description.length()), allocator);
obj.AddMember("description", val, allocator);

string help = "some help";
val.SetString(help.c_str(), static_cast<SizeType>(help.length()), allocator);
obj.AddMember("help", val, allocator);

string workgroup = "a workgroup";
val.SetString(workgroup.c_str(), static_cast<SizeType>(workgroup.length()), allocator);
obj.AddMember("workgroup", val, allocator);

val.SetBool(true);
obj.AddMember("online", val, allocator);

tests.PushBack(obj, allocator);
d.AddMember("tests", tests, allocator);

// Convert JSON document to string
rapidjson::StringBuffer strbuf;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(strbuf);
d.Accept(writer);

When I run this code, I'm expecting to obtain this json:

{
    "version": 1,
    "testId": 2,
    "group": 3,
    "order": 4,
    "tests": [
        {
            "id": 1,
            "description": "a description",
            "help": "some help",
            "workgroup": "a workgroup",
            "online": true
        }
    ]
}

but the actual generated output is...

{
    "version": 1,
    "testId": 2,
    "group": 3,
    "order": 4,
    "tests": [
        {
            "id": 1,
            "description": "a description",
            "help": "some help",
            "workgroup": "a workgroup",
            "online": tr{
    "version": 1,
    "testId": 2,
    "group": 3,
    "order": 4,
    "tests": [
        {
            "id": 1,
            "description": "a description",
            "help": "some help",
            "workgroup": "a workgroup",
            "online": true
        }
    ]
}

Any ideas?

like image 591
André Moreira Avatar asked Mar 01 '16 16:03

André Moreira


People also ask

Why is RapidJSON so fast?

rapidjson uses the magnitude of JSON numbers to choose a suitable numeric data type, and triggers the appropriate event accordingly. So it is fast and also providing full 64-bit integer precision.

What is DOM in JSON?

Document Object Model(DOM) is an in-memory representation of JSON for query and manipulation. The basic usage of DOM is described in Tutorial. This section will describe some details and more advanced usages.

How do I read a JSON file in CPP?

Simple JSON Parser in C++ using JsonCpp library We can use Json::Reader and Json::Writer for reading and writing in JSON files. Json::reader will read the JSON file and will return the value as Json::Value . Parsing the JSON is very simple by using parse() .


1 Answers

In the end I managed to track the problem down to the way I was outputing the string with OutputDebugString in VS. If I saved the resulting string (given by GetString()), the output was as expected!

Foiled by the debug trap I was!

like image 60
André Moreira Avatar answered Oct 14 '22 19:10

André Moreira