aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api
diff options
context:
space:
mode:
authorShadowNinja <shadowninja@minetest.net>2013-12-18 16:46:53 -0500
committerShadowNinja <shadowninja@minetest.net>2013-12-18 16:46:53 -0500
commit1ed90c90c304c6cc9cfddb722e4d15a1221d0177 (patch)
tree915dec8c285fa72866c5f250e84bec9af339fc11 /src/script/lua_api
parent49cec3f78240ed6310a9b5dd05ce09a79ed5a12e (diff)
downloadminetest-1ed90c90c304c6cc9cfddb722e4d15a1221d0177.tar.gz
minetest-1ed90c90c304c6cc9cfddb722e4d15a1221d0177.tar.bz2
minetest-1ed90c90c304c6cc9cfddb722e4d15a1221d0177.zip
Add 'minetest.write_json'
Diffstat (limited to 'src/script/lua_api')
-rw-r--r--src/script/lua_api/l_util.cpp27
-rw-r--r--src/script/lua_api/l_util.h3
2 files changed, 30 insertions, 0 deletions
diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp
index fe10e4f57..9fa6fcb77 100644
--- a/src/script/lua_api/l_util.cpp
+++ b/src/script/lua_api/l_util.cpp
@@ -179,6 +179,32 @@ int ModApiUtil::l_parse_json(lua_State *L)
return 1;
}
+// write_json(data[, styled]) -> string
+int ModApiUtil::l_write_json(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+
+ bool styled = false;
+ if (!lua_isnone(L, 2)) {
+ styled = lua_toboolean(L, 2);
+ lua_pop(L, 1);
+ }
+
+ Json::Value root;
+ get_json_value(L, root, 1);
+
+ std::string out;
+ if (styled) {
+ Json::StyledWriter writer;
+ out = writer.write(root);
+ } else {
+ Json::FastWriter writer;
+ out = writer.write(root);
+ }
+ lua_pushlstring(L, out.c_str(), out.size());
+ return 1;
+}
+
// get_dig_params(groups, tool_capabilities[, time_from_last_punch])
int ModApiUtil::l_get_dig_params(lua_State *L)
{
@@ -249,6 +275,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)
API_FCT(setting_save);
API_FCT(parse_json);
+ API_FCT(write_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 d91c880cf..13357587a 100644
--- a/src/script/lua_api/l_util.h
+++ b/src/script/lua_api/l_util.h
@@ -64,6 +64,9 @@ private:
// parse_json(str[, nullvalue])
static int l_parse_json(lua_State *L);
+ // write_json(data[, styled])
+ static int l_write_json(lua_State *L);
+
// get_dig_params(groups, tool_capabilities[, time_from_last_punch])
static int l_get_dig_params(lua_State *L);