aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api
diff options
context:
space:
mode:
Diffstat (limited to 'src/script/lua_api')
-rw-r--r--src/script/lua_api/l_util.cpp42
-rw-r--r--src/script/lua_api/l_util.h3
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);