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