I’m trying to convert a helper class that converts between CCObjects and JSON string from Jansson to rapidjson since it is included with Cocos2D-x v.3, and am having problems with the CCObject to JSON conversion, mainly that the deeper JSON objects are not getting stored in the parent object even though it gets parsed.
When I run the following code:
Dictionary *dict = Dictionary::create();
dict->setObject( String::create( "value1" ), "key1" );
Array *arr = Array::create();
arr->addObject( String::create( "value2" ) );
arr->addObject( String::create( "value3" ) );
dict->setObject( arr, "key2" );
std::string jsonStr = getJSONStringFromObject(dict);
log( "jsonStr = \n%s", jsonStr.c_str() );
All I get is:
cocos2d: jsonStr =
{"key1":true,"key2":true}
I think the problem is in the following lines, where AddMember is putting the value2 in another JSON Value instead of storing it directly, and I cannot figure out how I should cast the variable to get it to store properly:
rapidjson::Value *value2 = jsonFromObject(mainDict->objectForKey(key), document);
retVal->AddMember( key, value2, document->GetAllocator() );
I have tried rewriting the code to not use pointers, but then I get “calling a private constructor of class…” errors, so that doesn’t seem to be the answer.
Attached are the source files of the helper. Any suggestions would be helpful.
JsonHelper.zip (3.1 KB)