//----------------------------------------------------------------// void _jsonObjectToLua ( lua_State* L, json_t* json ) { assert ( json->type == JSON_OBJECT ); lua_newtable ( L ); void* iter = json_object_iter ( json ); for ( ; iter; iter = json_object_iter_next ( json, iter )) { cc8* key = json_object_iter_key ( iter ); json_t* value = json_object_iter_value ( iter ); _jsonToLua ( L, value ); lua_setfield ( L, -2, key ); } }
//----------------------------------------------------------------// void _jsonArrayToLua ( lua_State* L, json_t* json ) { assert ( json->type == JSON_ARRAY ); lua_newtable ( L ); size_t size = json_array_size ( json ); for ( size_t i = 0; i < size; ++i ) { json_t* value = json_array_get ( json, i ); if ( value ) { lua_pushnumber ( L, i + 1 ); _jsonToLua ( L, value ); lua_settable ( L, -3 ); } } }
/** @name decode @text Decode a JSON string into a hierarchy of Lua tables. @in string input @out table result */ int MOAIJsonParser::_decode ( lua_State* L ) { UNUSED ( L ); MOAILuaState state ( L ); if ( state.IsType ( 1, LUA_TSTRING )) { size_t bufflen; cc8* buffer = lua_tolstring ( L, -1, &bufflen ); json_error_t error; json_t* json = json_loadb ( buffer, bufflen, JSON_DISABLE_EOF_CHECK, &error ); if ( json ) { _jsonToLua ( L, json ); json_decref ( json ); return 1; } } return 0; }
/** @lua decode @text Decode a JSON string into a hierarchy of Lua tables. @in string input @out table result */ int MOAIJsonParser::_decode ( lua_State* L ) { UNUSED ( L ); MOAILuaState state ( L ); if ( state.IsType ( 1, LUA_TSTRING )) { size_t bufflen; cc8* buffer = lua_tolstring ( L, -1, &bufflen ); json_error_t error; json_t* json = json_loadb ( buffer, bufflen, JSON_DISABLE_EOF_CHECK, &error ); if ( json ) { _jsonToLua ( L, json ); json_decref ( json ); return 1; } else { printf ( "%d %d %d %s %s\n", error.line, error.column, error.position, error.source, error.text ); } } return 0; }