diff options
author | Kahrl <kahrl@gmx.net> | 2013-09-02 02:01:49 +0200 |
---|---|---|
committer | Kahrl <kahrl@gmx.net> | 2013-09-02 02:20:08 +0200 |
commit | 1ecf51a13f434f5cbc0f6ccc1b9a2ac6402a895f (patch) | |
tree | e8ac8ced0ed73bd9a4a803dc703a213e3a9b91ec /src/script/lua_api | |
parent | 71a6ffa76203a13b6cb0ec88b7ee57e04f809148 (diff) | |
download | minetest-1ecf51a13f434f5cbc0f6ccc1b9a2ac6402a895f.tar.gz minetest-1ecf51a13f434f5cbc0f6ccc1b9a2ac6402a895f.tar.bz2 minetest-1ecf51a13f434f5cbc0f6ccc1b9a2ac6402a895f.zip |
Add minetest.parse_json, engine.parse_json
Diffstat (limited to 'src/script/lua_api')
-rw-r--r-- | src/script/lua_api/l_util.cpp | 42 | ||||
-rw-r--r-- | src/script/lua_api/l_util.h | 3 |
2 files changed, 45 insertions, 0 deletions
diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index 0e4de9eee..30fa56c42 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "tool.h" #include "settings.h" #include "main.h" //required for g_settings, g_settings_path +#include "json/json.h" // debug(...) // Writes a line to dstream @@ -138,6 +139,45 @@ int ModApiUtil::l_setting_save(lua_State *L) return 0; } +// parse_json(str[, nullvalue]) +int ModApiUtil::l_parse_json(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + const char *jsonstr = luaL_checkstring(L, 1); + + // Use passed nullvalue or default to nil + int nullindex = 2; + if (lua_isnone(L, nullindex)) { + lua_pushnil(L); + nullindex = lua_gettop(L); + } + + Json::Value root; + + { + Json::Reader reader; + std::istringstream stream(jsonstr); + + if (!reader.parse(stream, root)) { + errorstream << "Failed to parse json data " + << reader.getFormattedErrorMessages(); + errorstream << "data: \"" << jsonstr << "\"" + << std::endl; + lua_pushnil(L); + return 1; + } + } + + if (!push_json_value(L, root, nullindex)) { + errorstream << "Failed to parse json data, " + << "depth exceeds lua stack limit" << std::endl; + errorstream << "data: \"" << jsonstr << "\"" << std::endl; + lua_pushnil(L); + } + return 1; +} + // get_dig_params(groups, tool_capabilities[, time_from_last_punch]) int ModApiUtil::l_get_dig_params(lua_State *L) { @@ -191,6 +231,8 @@ void ModApiUtil::Initialize(lua_State *L, int top) API_FCT(setting_getbool); API_FCT(setting_save); + API_FCT(parse_json); + API_FCT(get_dig_params); API_FCT(get_hit_params); diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h index b102e315b..71c55b342 100644 --- a/src/script/lua_api/l_util.h +++ b/src/script/lua_api/l_util.h @@ -59,6 +59,9 @@ private: // setting_save() static int l_setting_save(lua_State *L); + // parse_json(str[, nullvalue]) + static int l_parse_json(lua_State *L); + // get_dig_params(groups, tool_capabilities[, time_from_last_punch]) static int l_get_dig_params(lua_State *L); |