Skip to content

Commit 1ecf51a

Browse files
committedSep 2, 2013
Add minetest.parse_json, engine.parse_json
1 parent 71a6ffa commit 1ecf51a

File tree

6 files changed

+141
-0
lines changed

6 files changed

+141
-0
lines changed
 

‎doc/lua_api.txt

+6
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,12 @@ minetest.get_content_id(name) -> integer
14581458
^ Gets the internal content ID of name
14591459
minetest.get_name_from_content_id(content_id) -> string
14601460
^ Gets the name of the content with that content ID
1461+
minetest.parse_json(string[, nullvalue]) -> something
1462+
^ Convert a string containing JSON data into the Lua equivalent
1463+
^ nullvalue: returned in place of the JSON null; defaults to nil
1464+
^ On success returns a table, a string, a number, a boolean or nullvalue
1465+
^ On failure outputs an error message and returns nil
1466+
^ Example: parse_json("[10, {\"a\":false}]") -> {[1] = 10, [2] = {a = false}}
14611467
minetest.serialize(table) -> string
14621468
^ Convert a table containing tables, strings, numbers, booleans and nils
14631469
into string form readable by minetest.deserialize

‎doc/menu_lua_api.txt

+2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ engine.gettext(string) -> string
174174
fgettext(string, ...) -> string
175175
^ call engine.gettext(string), replace "$1"..."$9" with the given
176176
^ extra arguments, call engine.formspec_escape and return the result
177+
engine.parse_json(string[, nullvalue]) -> something
178+
^ see minetest.parse_json (lua_api.txt)
177179
dump(obj, dumped={})
178180
^ Return object serialized as a string
179181
string:split(separator)

‎src/script/common/c_content.cpp

+82
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3131
#include "tool.h"
3232
#include "serverobject.h"
3333
#include "mapgen.h"
34+
#include "json/json.h"
3435

3536
struct EnumString es_TileAnimationType[] =
3637
{
@@ -998,3 +999,84 @@ bool read_schematic(lua_State *L, int index, DecoSchematic *dschem, Server *serv
998999

9991000
return true;
10001001
}
1002+
1003+
/******************************************************************************/
1004+
// Returns depth of json value tree
1005+
static int push_json_value_getdepth(const Json::Value &value)
1006+
{
1007+
if (!value.isArray() && !value.isObject())
1008+
return 1;
1009+
1010+
int maxdepth = 0;
1011+
for (Json::Value::const_iterator it = value.begin();
1012+
it != value.end(); ++it) {
1013+
int elemdepth = push_json_value_getdepth(*it);
1014+
if (elemdepth > maxdepth)
1015+
maxdepth = elemdepth;
1016+
}
1017+
return maxdepth + 1;
1018+
}
1019+
// Recursive function to convert JSON --> Lua table
1020+
static bool push_json_value_helper(lua_State *L, const Json::Value &value,
1021+
int nullindex)
1022+
{
1023+
switch(value.type()) {
1024+
case Json::nullValue:
1025+
default:
1026+
lua_pushvalue(L, nullindex);
1027+
break;
1028+
case Json::intValue:
1029+
lua_pushinteger(L, value.asInt());
1030+
break;
1031+
case Json::uintValue:
1032+
lua_pushinteger(L, value.asUInt());
1033+
break;
1034+
case Json::realValue:
1035+
lua_pushnumber(L, value.asDouble());
1036+
break;
1037+
case Json::stringValue:
1038+
{
1039+
const char *str = value.asCString();
1040+
lua_pushstring(L, str ? str : "");
1041+
}
1042+
break;
1043+
case Json::booleanValue:
1044+
lua_pushboolean(L, value.asInt());
1045+
break;
1046+
case Json::arrayValue:
1047+
lua_newtable(L);
1048+
for (Json::Value::const_iterator it = value.begin();
1049+
it != value.end(); ++it) {
1050+
push_json_value_helper(L, *it, nullindex);
1051+
lua_rawseti(L, -2, it.index() + 1);
1052+
}
1053+
break;
1054+
case Json::objectValue:
1055+
lua_newtable(L);
1056+
for (Json::Value::const_iterator it = value.begin();
1057+
it != value.end(); ++it) {
1058+
const char *str = it.memberName();
1059+
lua_pushstring(L, str ? str : "");
1060+
push_json_value_helper(L, *it, nullindex);
1061+
lua_rawset(L, -3);
1062+
}
1063+
break;
1064+
}
1065+
return true;
1066+
}
1067+
// converts JSON --> Lua table; returns false if lua stack limit exceeded
1068+
// nullindex: Lua stack index of value to use in place of JSON null
1069+
bool push_json_value(lua_State *L, const Json::Value &value, int nullindex)
1070+
{
1071+
if(nullindex < 0)
1072+
nullindex = lua_gettop(L) + 1 + nullindex;
1073+
1074+
int depth = push_json_value_getdepth(value);
1075+
1076+
// The maximum number of Lua stack slots used at each recursion level
1077+
// of push_json_value_helper is 2, so make sure there a depth * 2 slots
1078+
if (lua_checkstack(L, depth * 2))
1079+
return push_json_value_helper(L, value, nullindex);
1080+
else
1081+
return false;
1082+
}

‎src/script/common/c_content.h

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ extern "C" {
3939
#include "irrlichttypes_bloated.h"
4040
#include "util/string.h"
4141

42+
namespace Json { class Value; }
43+
4244
struct MapNode;
4345
class INodeDefManager;
4446
struct PointedThing;
@@ -145,6 +147,10 @@ bool read_schematic (lua_State *L, int index,
145147

146148
void luaentity_get (lua_State *L,u16 id);
147149

150+
bool push_json_value (lua_State *L,
151+
const Json::Value &value,
152+
int nullindex);
153+
148154
extern struct EnumString es_TileAnimationType[];
149155

150156
#endif /* C_CONTENT_H_ */

‎src/script/lua_api/l_util.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2626
#include "tool.h"
2727
#include "settings.h"
2828
#include "main.h" //required for g_settings, g_settings_path
29+
#include "json/json.h"
2930

3031
// debug(...)
3132
// Writes a line to dstream
@@ -138,6 +139,45 @@ int ModApiUtil::l_setting_save(lua_State *L)
138139
return 0;
139140
}
140141

142+
// parse_json(str[, nullvalue])
143+
int ModApiUtil::l_parse_json(lua_State *L)
144+
{
145+
NO_MAP_LOCK_REQUIRED;
146+
147+
const char *jsonstr = luaL_checkstring(L, 1);
148+
149+
// Use passed nullvalue or default to nil
150+
int nullindex = 2;
151+
if (lua_isnone(L, nullindex)) {
152+
lua_pushnil(L);
153+
nullindex = lua_gettop(L);
154+
}
155+
156+
Json::Value root;
157+
158+
{
159+
Json::Reader reader;
160+
std::istringstream stream(jsonstr);
161+
162+
if (!reader.parse(stream, root)) {
163+
errorstream << "Failed to parse json data "
164+
<< reader.getFormattedErrorMessages();
165+
errorstream << "data: \"" << jsonstr << "\""
166+
<< std::endl;
167+
lua_pushnil(L);
168+
return 1;
169+
}
170+
}
171+
172+
if (!push_json_value(L, root, nullindex)) {
173+
errorstream << "Failed to parse json data, "
174+
<< "depth exceeds lua stack limit" << std::endl;
175+
errorstream << "data: \"" << jsonstr << "\"" << std::endl;
176+
lua_pushnil(L);
177+
}
178+
return 1;
179+
}
180+
141181
// get_dig_params(groups, tool_capabilities[, time_from_last_punch])
142182
int ModApiUtil::l_get_dig_params(lua_State *L)
143183
{
@@ -191,6 +231,8 @@ void ModApiUtil::Initialize(lua_State *L, int top)
191231
API_FCT(setting_getbool);
192232
API_FCT(setting_save);
193233

234+
API_FCT(parse_json);
235+
194236
API_FCT(get_dig_params);
195237
API_FCT(get_hit_params);
196238

‎src/script/lua_api/l_util.h

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class ModApiUtil : public ModApiBase {
5959
// setting_save()
6060
static int l_setting_save(lua_State *L);
6161

62+
// parse_json(str[, nullvalue])
63+
static int l_parse_json(lua_State *L);
64+
6265
// get_dig_params(groups, tool_capabilities[, time_from_last_punch])
6366
static int l_get_dig_params(lua_State *L);
6467

0 commit comments

Comments
 (0)
Please sign in to comment.