mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Various improvements to push_json_value
This commit is contained in:
parent
d95e916a42
commit
cbc741f464
2 changed files with 35 additions and 22 deletions
|
@ -153,6 +153,18 @@ local function test_urlencode()
|
||||||
end
|
end
|
||||||
unittests.register("test_urlencode", test_urlencode)
|
unittests.register("test_urlencode", test_urlencode)
|
||||||
|
|
||||||
|
local function test_parse_json()
|
||||||
|
local raw = "{\"how\\u0000weird\":\n\"yes\\u0000really\",\"n\":-1234567891011,\"z\":null}"
|
||||||
|
local data = core.parse_json(raw)
|
||||||
|
assert(data["how\000weird"] == "yes\000really")
|
||||||
|
assert(data.n == -1234567891011)
|
||||||
|
assert(data.z == nil)
|
||||||
|
local null = {}
|
||||||
|
data = core.parse_json(raw, null)
|
||||||
|
assert(data.z == null)
|
||||||
|
end
|
||||||
|
unittests.register("test_parse_json", test_parse_json)
|
||||||
|
|
||||||
local function test_game_info()
|
local function test_game_info()
|
||||||
local info = minetest.get_game_info()
|
local info = minetest.get_game_info()
|
||||||
local game_conf = Settings(info.path .. "/game.conf")
|
local game_conf = Settings(info.path .. "/game.conf")
|
||||||
|
|
|
@ -2063,6 +2063,10 @@ bool read_tree_def(lua_State *L, int idx, const NodeDefManager *ndef,
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
#if defined(JSONCPP_STRING) || !(JSONCPP_VERSION_MAJOR < 1 || JSONCPP_VERSION_MINOR < 9)
|
||||||
|
#define HAVE_JSON_STRING
|
||||||
|
#endif
|
||||||
|
|
||||||
// Returns depth of json value tree
|
// Returns depth of json value tree
|
||||||
static int push_json_value_getdepth(const Json::Value &value)
|
static int push_json_value_getdepth(const Json::Value &value)
|
||||||
{
|
{
|
||||||
|
@ -2070,11 +2074,8 @@ static int push_json_value_getdepth(const Json::Value &value)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
int maxdepth = 0;
|
int maxdepth = 0;
|
||||||
for (const auto &it : value) {
|
for (const auto &it : value)
|
||||||
int elemdepth = push_json_value_getdepth(it);
|
maxdepth = std::max(push_json_value_getdepth(it), maxdepth);
|
||||||
if (elemdepth > maxdepth)
|
|
||||||
maxdepth = elemdepth;
|
|
||||||
}
|
|
||||||
return maxdepth + 1;
|
return maxdepth + 1;
|
||||||
}
|
}
|
||||||
// Recursive function to convert JSON --> Lua table
|
// Recursive function to convert JSON --> Lua table
|
||||||
|
@ -2087,41 +2088,40 @@ static bool push_json_value_helper(lua_State *L, const Json::Value &value,
|
||||||
lua_pushvalue(L, nullindex);
|
lua_pushvalue(L, nullindex);
|
||||||
break;
|
break;
|
||||||
case Json::intValue:
|
case Json::intValue:
|
||||||
lua_pushinteger(L, value.asLargestInt());
|
|
||||||
break;
|
|
||||||
case Json::uintValue:
|
case Json::uintValue:
|
||||||
lua_pushinteger(L, value.asLargestUInt());
|
|
||||||
break;
|
|
||||||
case Json::realValue:
|
case Json::realValue:
|
||||||
|
// push everything as a double since Lua integers may be too small
|
||||||
lua_pushnumber(L, value.asDouble());
|
lua_pushnumber(L, value.asDouble());
|
||||||
break;
|
break;
|
||||||
case Json::stringValue:
|
case Json::stringValue: {
|
||||||
{
|
#ifdef HAVE_JSON_STRING
|
||||||
|
const auto &str = value.asString();
|
||||||
|
lua_pushlstring(L, str.c_str(), str.size());
|
||||||
|
#else
|
||||||
const char *str = value.asCString();
|
const char *str = value.asCString();
|
||||||
lua_pushstring(L, str ? str : "");
|
lua_pushstring(L, str ? str : "");
|
||||||
}
|
#endif
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case Json::booleanValue:
|
case Json::booleanValue:
|
||||||
lua_pushboolean(L, value.asInt());
|
lua_pushboolean(L, value.asInt());
|
||||||
break;
|
break;
|
||||||
case Json::arrayValue:
|
case Json::arrayValue:
|
||||||
lua_createtable(L, value.size(), 0);
|
lua_createtable(L, value.size(), 0);
|
||||||
for (Json::Value::const_iterator it = value.begin();
|
for (auto it = value.begin(); it != value.end(); ++it) {
|
||||||
it != value.end(); ++it) {
|
|
||||||
push_json_value_helper(L, *it, nullindex);
|
push_json_value_helper(L, *it, nullindex);
|
||||||
lua_rawseti(L, -2, it.index() + 1);
|
lua_rawseti(L, -2, it.index() + 1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Json::objectValue:
|
case Json::objectValue:
|
||||||
lua_createtable(L, 0, value.size());
|
lua_createtable(L, 0, value.size());
|
||||||
for (Json::Value::const_iterator it = value.begin();
|
for (auto it = value.begin(); it != value.end(); ++it) {
|
||||||
it != value.end(); ++it) {
|
#ifdef HAVE_JSON_STRING
|
||||||
#if !defined(JSONCPP_STRING) && (JSONCPP_VERSION_MAJOR < 1 || JSONCPP_VERSION_MINOR < 9)
|
const auto &str = it.name();
|
||||||
|
lua_pushlstring(L, str.c_str(), str.size());
|
||||||
|
#else
|
||||||
const char *str = it.memberName();
|
const char *str = it.memberName();
|
||||||
lua_pushstring(L, str ? str : "");
|
lua_pushstring(L, str ? str : "");
|
||||||
#else
|
|
||||||
std::string str = it.name();
|
|
||||||
lua_pushstring(L, str.c_str());
|
|
||||||
#endif
|
#endif
|
||||||
push_json_value_helper(L, *it, nullindex);
|
push_json_value_helper(L, *it, nullindex);
|
||||||
lua_rawset(L, -3);
|
lua_rawset(L, -3);
|
||||||
|
@ -2130,6 +2130,7 @@ static bool push_json_value_helper(lua_State *L, const Json::Value &value,
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// converts JSON --> Lua table; returns false if lua stack limit exceeded
|
// converts JSON --> Lua table; returns false if lua stack limit exceeded
|
||||||
// nullindex: Lua stack index of value to use in place of JSON null
|
// nullindex: Lua stack index of value to use in place of JSON null
|
||||||
bool push_json_value(lua_State *L, const Json::Value &value, int nullindex)
|
bool push_json_value(lua_State *L, const Json::Value &value, int nullindex)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue