diff options
author | ShadowNinja <shadowninja@minetest.net> | 2013-12-18 16:46:53 -0500 |
---|---|---|
committer | ShadowNinja <shadowninja@minetest.net> | 2013-12-18 16:46:53 -0500 |
commit | 1ed90c90c304c6cc9cfddb722e4d15a1221d0177 (patch) | |
tree | 915dec8c285fa72866c5f250e84bec9af339fc11 /src/script/common | |
parent | 49cec3f78240ed6310a9b5dd05ce09a79ed5a12e (diff) | |
download | minetest-1ed90c90c304c6cc9cfddb722e4d15a1221d0177.tar.gz minetest-1ed90c90c304c6cc9cfddb722e4d15a1221d0177.tar.bz2 minetest-1ed90c90c304c6cc9cfddb722e4d15a1221d0177.zip |
Add 'minetest.write_json'
Diffstat (limited to 'src/script/common')
-rw-r--r-- | src/script/common/c_content.cpp | 49 | ||||
-rw-r--r-- | src/script/common/c_content.h | 3 |
2 files changed, 52 insertions, 0 deletions
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index cf9f28d30..8eb57ba41 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1081,3 +1081,52 @@ bool push_json_value(lua_State *L, const Json::Value &value, int nullindex) else return false; } + +// Converts Lua table --> JSON +void get_json_value(lua_State *L, Json::Value &root, int index) +{ + int type = lua_type(L, index); + if (type == LUA_TBOOLEAN) { + root = (bool) lua_toboolean(L, index); + } else if (type == LUA_TNUMBER) { + root = lua_tonumber(L, index); + } else if (type == LUA_TSTRING) { + size_t len; + const char *str = lua_tolstring(L, index, &len); + root = std::string(str, len); + } else if (type == LUA_TTABLE) { + lua_pushnil(L); + while (lua_next(L, index)) { + // Key is at -2 and value is at -1 + Json::Value value; + get_json_value(L, value, lua_gettop(L)); + + Json::ValueType roottype = root.type(); + int keytype = lua_type(L, -1); + if (keytype == LUA_TNUMBER) { + lua_Number key = lua_tonumber(L, -1); + if (roottype != Json::nullValue && roottype != Json::arrayValue) { + throw LuaError(NULL, "Can't mix array and object values in JSON"); + } else if (key < 1) { + throw LuaError(NULL, "Can't use zero-based or negative indexes in JSON"); + } else if (floor(key) != key) { + throw LuaError(NULL, "Can't use indexes with a fractional part in JSON"); + } + root[(Json::ArrayIndex) key - 1] = value; + } else if (keytype == LUA_TSTRING) { + if (roottype != Json::nullValue && roottype != Json::objectValue) { + throw LuaError(NULL, "Can't mix array and object values in JSON"); + } + root[lua_tostring(L, -1)] = value; + } else { + throw LuaError(NULL, "Lua key to convert to JSON is not a string or number"); + } + } + } else if (type == LUA_TNIL) { + root = Json::nullValue; + } else { + throw LuaError(NULL, "Can only store booleans, numbers, strings, objects, arrays, and null in JSON"); + } + lua_pop(L, 1); // Pop value +} + diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index 27019e29e..3b85e5403 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -150,6 +150,9 @@ void luaentity_get (lua_State *L,u16 id); bool push_json_value (lua_State *L, const Json::Value &value, int nullindex); +void get_json_value (lua_State *L, + Json::Value &root, + int index); extern struct EnumString es_TileAnimationType[]; |