weird member name string with rapidjson weird member name string with rapidjson json json

weird member name string with rapidjson


If you want a variable string as the member name in the addmember function, the following worked for me:

    char buff[50];    sprintf(buff, "%d", somefacyNumber);    Value vMemberName  (kStringType);    vMemberName.SetString(buff, strlen(buff), nalloc);     // for buffs we must do this,        // if we use stringref, then next next time we add a buffer we stamp on previous value    someObject.AddMember(vMemberName, vSomeOtherValue, nalloc); // nalloc is our allocator


based on what vaultah suggested, I'd found out that I have to explicitly create

rapidjson::Value name(pair.first.c_str(), allocator);

to force it to use a string-copy constructor, and use

json.AddMember(name.Move(), Value(123).Move(), allocator);

to add to json document.


rapidjson::Document json;json.setObject();json.AddMemeber(StringRef(TEST01.c_str()), Value(123), json.GetAllocator());

So, why "test02" and "test03" are mojibake, it's because of the memory isn't allocated in the right way.
While c_str() is weak reference, so, the above code can work, but are not good.