cocos2d x 3.0 - what is the difference between `doc.AddMember("key1",1,document.GetAllocator())` and `doc["key1"]=1`? -
i want create json object in cocos2d-x 3.4
rapidjson
, convert string:
rapidjson::document doc; doc.setobject(); doc.addmember("key1",1,doc.getallocator()); doc["key2"]=2; rapidjson::stringbuffer sb; rapidjson::writer<rapidjson::stringbuffer> writer(sb); doc.accept(writer); cclog("%s",sb.getstring());
but output {"key1":1} not {"key1":1,"key2":2}
, why?
in old (0.1x) versions of rapidjson, doc["key2"]
returns value
singleton representing null. doc["key2"] = 2
writes singleton.
in newer versions of rapidjson (v1.0.x), behavior has been changed. make assertion fail key not found in json object, in order solve exact problem mentioned.
as reminder, when operation potentially requires allocating memory (such addmember
or pushback
, allocator
object must appeared. since operator[]
has 1 parameter, cannot add new members in stl. quite weird , not user-friendly, tradeoff in rapidjson's design performance , memory overheads.