const char *json = R"({"hello": "world"})";
rapidjson::Document d;
d.Parse<0> (json);
rapidjson::Value::Member* hello = d.FindMember ("hello"); if (hello) {
d.AddMember ("goodbye", hello->value, d.GetAllocator());
d.RemoveMember ("hello");
}
typedef rapidjson::GenericStringBuffer<rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<>> StringBuffer;
StringBuffer buf (&d.GetAllocator());
rapidjson::Writer<StringBuffer> writer (buf, &d.GetAllocator());
d.Accept (writer);
json = buf.GetString();
PS Most likely you should copy json, because his memory will be freed with d.
PPS You can also replace the field name in place without deleting it:
rapidjson::Value::Member* hello = d.FindMember ("hello");
if (hello) hello->name.SetString ("goodbye", d.GetAllocator());
Or during iteration:
for (auto it = d.MemberBegin(); it != d.MemberEnd(); ++it)
if (strcmp (it->name.GetString(), "hello") == 0) it->name.SetString ("goodbye", d.GetAllocator());
source
share