I've just begun using RapidJSON; currently, I've got a STL map of strings; and I want to represent it as JSON.
So far, I've done this:
using JSONDocument = rapidjson::GenericDocument<rapidjson::UTF8<>>;
using JSONValue = rapidjson::GenericValue<rapidjson::UTF8<>>;
JSONDocument jsonRoot(rapidjson::kArrayType);
for (auto const &kv : localeMap) {
std::string key = kv.first;
std::replace(key.begin(), key.end(), ' ','_');
boost::to_lower(key);
JSONValue k(rapidjson::kObjectType);
JSONValue v(rapidjson::kObjectType);
k.SetString(key,jsonRoot.GetAllocator());
v.SetString(kv.second,jsonRoot.GetAllocator());
JSONValue kv_(rapidjson::kObjectType);
kv_.AddMember(k,v,jsonRoot.GetAllocator());
jsonRoot.PushBack(kv_, jsonRoot.GetAllocator());
}
And it works, but that looks terribly clunky to me. I cannot figure out if there's a better/shorter/more performant way of doing this... I've tried several variations of trying to create the object in place but they either don't compile or crash at runtime.
Any hints?