In COCOS2DX for Lua, we often use LUA's table to pass in C + + and then manipulate the data early in the C + + layer.
The implementation steps are roughly as follows:
Table->string->c++ layer, managed by Rapidjson resolution, stored in map
In Lua, the conversion table is roughly the following
Local TBL = {}
Tbl["Fang"] = 1
tbl["jian"] = 1.4
Tbl["Heng"] = True
tbl["FJH"] = 12345677
tbl["what"] = "fuck"
Local str = Json.encode (TBL)
--Print Str:
{"Fang": 1, "Jian": 1.4, "Heng": True, "Fjh": 12345677, "what": "Fuck"}
The STR is passed into the C + + layer and parsed by Rapidjson::D Ocument, however, the data type in the JSON string is indeterminate,
But touch has encapsulated a Cclvalue class for us to use, it can store any value simple data type to save, and then need to
Time to get it back. CCLValue.h and cclvalue Two file downloads are available here:
Http://pan.baidu.com/s/1c06Z0xM
The following methods are used to convert a string into a map-saved method:
Map<string,lvalue> testlvalue::jsonstringtomap (const string& jsonstring)
{
Rapidjson::D ocument doc;
Doc. SetObject ();
Doc. Parse<rapidjson::kparsedefaultflags> (Jsonstring.c_str ());
Map<string,lvalue> m;
for (Rapidjson::value::memberiterator iter = doc. Memberonbegin (); iter! = Doc. Memberonend (); iter++)
{
const char * key = Iter->name. GetString ();
Const rapidjson::value& val = iter->value;
if (Val. Isdouble ())
M.insert (Make_pair (Key, LValue (Val. GetDouble ())));
else if (Val. Isbool ())
M.insert (Make_pair (Key, LValue (Val. Getbool ())));
else if (Val. Isint ())
M.insert (Make_pair (Key, LValue (Val. GetInt ())));
else if (Val. IsInt64 ())
M.insert (Make_pair (Key, LValue (Val. GetInt64 ())));
else if (Val. Isstring ())
M.insert (Make_pair (Key, LValue (Val. GetString ())));
}
return m;
}
The procedure is simple, the value of the LValue in the map<string,lvalue> of the return value needs to be judged by its data type and then obtained
if (It->second.gettype () ==lvalue::type::D ouble) {
Double value = it->second.asdouble ();
}
else if (It->second.gettype () ==lvalue::type::integer) {
int value = It->second.asint ();
}
.
.
.
and float, string, int64, bool, int64 don't write it down.
Reprint please indicate the source, from the blog Park Hemjohn
C + + JSON string conversion to map management