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.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -