Skip to content

Commit 1ed90c9

Browse files
committedDec 18, 2013
Add 'minetest.write_json'
1 parent 49cec3f commit 1ed90c9

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed
 

Diff for: ‎doc/lua_api.txt

+10-1
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,16 @@ minetest.parse_json(string[, nullvalue]) -> something
15301530
^ nullvalue: returned in place of the JSON null; defaults to nil
15311531
^ On success returns a table, a string, a number, a boolean or nullvalue
15321532
^ On failure outputs an error message and returns nil
1533-
^ Example: parse_json("[10, {\"a\":false}]") -> {[1] = 10, [2] = {a = false}}
1533+
^ Example: parse_json("[10, {\"a\":false}]") -> {10, {a = false}}
1534+
minetest.write_json(data[, styled]) -> string
1535+
^ Convert a Lua table into a JSON string
1536+
^ styled: Outputs in a human-readable format if this is set, defaults to false
1537+
^ Un-serializable things like functions and userdata are saved as null.
1538+
^ Warning: JSON is more strict than the Lua table format.
1539+
1. You can only use strings and positive integers of at least one as keys.
1540+
2. You can not mix string and integer keys.
1541+
This is due to the fact that Javascript has two distinct array and object values.
1542+
^ Example: write_json({10, {a = false}}) -> "[10, {\"a\": false}]"
15341543
minetest.serialize(table) -> string
15351544
^ Convert a table containing tables, strings, numbers, booleans and nils
15361545
into string form readable by minetest.deserialize

Diff for: ‎src/script/common/c_content.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -1081,3 +1081,52 @@ bool push_json_value(lua_State *L, const Json::Value &value, int nullindex)
10811081
else
10821082
return false;
10831083
}
1084+
1085+
// Converts Lua table --> JSON
1086+
void get_json_value(lua_State *L, Json::Value &root, int index)
1087+
{
1088+
int type = lua_type(L, index);
1089+
if (type == LUA_TBOOLEAN) {
1090+
root = (bool) lua_toboolean(L, index);
1091+
} else if (type == LUA_TNUMBER) {
1092+
root = lua_tonumber(L, index);
1093+
} else if (type == LUA_TSTRING) {
1094+
size_t len;
1095+
const char *str = lua_tolstring(L, index, &len);
1096+
root = std::string(str, len);
1097+
} else if (type == LUA_TTABLE) {
1098+
lua_pushnil(L);
1099+
while (lua_next(L, index)) {
1100+
// Key is at -2 and value is at -1
1101+
Json::Value value;
1102+
get_json_value(L, value, lua_gettop(L));
1103+
1104+
Json::ValueType roottype = root.type();
1105+
int keytype = lua_type(L, -1);
1106+
if (keytype == LUA_TNUMBER) {
1107+
lua_Number key = lua_tonumber(L, -1);
1108+
if (roottype != Json::nullValue && roottype != Json::arrayValue) {
1109+
throw LuaError(NULL, "Can't mix array and object values in JSON");
1110+
} else if (key < 1) {
1111+
throw LuaError(NULL, "Can't use zero-based or negative indexes in JSON");
1112+
} else if (floor(key) != key) {
1113+
throw LuaError(NULL, "Can't use indexes with a fractional part in JSON");
1114+
}
1115+
root[(Json::ArrayIndex) key - 1] = value;
1116+
} else if (keytype == LUA_TSTRING) {
1117+
if (roottype != Json::nullValue && roottype != Json::objectValue) {
1118+
throw LuaError(NULL, "Can't mix array and object values in JSON");
1119+
}
1120+
root[lua_tostring(L, -1)] = value;
1121+
} else {
1122+
throw LuaError(NULL, "Lua key to convert to JSON is not a string or number");
1123+
}
1124+
}
1125+
} else if (type == LUA_TNIL) {
1126+
root = Json::nullValue;
1127+
} else {
1128+
throw LuaError(NULL, "Can only store booleans, numbers, strings, objects, arrays, and null in JSON");
1129+
}
1130+
lua_pop(L, 1); // Pop value
1131+
}
1132+

Diff for: ‎src/script/common/c_content.h

+3
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ void luaentity_get (lua_State *L,u16 id);
150150
bool push_json_value (lua_State *L,
151151
const Json::Value &value,
152152
int nullindex);
153+
void get_json_value (lua_State *L,
154+
Json::Value &root,
155+
int index);
153156

154157
extern struct EnumString es_TileAnimationType[];
155158

Diff for: ‎src/script/lua_api/l_util.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,32 @@ int ModApiUtil::l_parse_json(lua_State *L)
179179
return 1;
180180
}
181181

182+
// write_json(data[, styled]) -> string
183+
int ModApiUtil::l_write_json(lua_State *L)
184+
{
185+
NO_MAP_LOCK_REQUIRED;
186+
187+
bool styled = false;
188+
if (!lua_isnone(L, 2)) {
189+
styled = lua_toboolean(L, 2);
190+
lua_pop(L, 1);
191+
}
192+
193+
Json::Value root;
194+
get_json_value(L, root, 1);
195+
196+
std::string out;
197+
if (styled) {
198+
Json::StyledWriter writer;
199+
out = writer.write(root);
200+
} else {
201+
Json::FastWriter writer;
202+
out = writer.write(root);
203+
}
204+
lua_pushlstring(L, out.c_str(), out.size());
205+
return 1;
206+
}
207+
182208
// get_dig_params(groups, tool_capabilities[, time_from_last_punch])
183209
int ModApiUtil::l_get_dig_params(lua_State *L)
184210
{
@@ -249,6 +275,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)
249275
API_FCT(setting_save);
250276

251277
API_FCT(parse_json);
278+
API_FCT(write_json);
252279

253280
API_FCT(get_dig_params);
254281
API_FCT(get_hit_params);

Diff for: ‎src/script/lua_api/l_util.h

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class ModApiUtil : public ModApiBase {
6464
// parse_json(str[, nullvalue])
6565
static int l_parse_json(lua_State *L);
6666

67+
// write_json(data[, styled])
68+
static int l_write_json(lua_State *L);
69+
6770
// get_dig_params(groups, tool_capabilities[, time_from_last_punch])
6871
static int l_get_dig_params(lua_State *L);
6972

0 commit comments

Comments
 (0)
Please sign in to comment.