From: Anbang Wen Date: Wed, 18 Apr 2018 20:37:42 +0000 (-0700) Subject: export a JSON decode function to lua X-Git-Tag: v2.3.0~4^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=15ba9591a7973f82f27901030cb9f7ca9c31073d;p=thirdparty%2Fknot-resolver.git export a JSON decode function to lua Since there is already a bundled JSON library, expose it to lua for modules to use. --- diff --git a/daemon/engine.c b/daemon/engine.c index 91390c452..7dc89d431 100644 --- a/daemon/engine.c +++ b/daemon/engine.c @@ -472,6 +472,26 @@ static int l_tojson(lua_State *L) return 1; } +static int l_fromjson(lua_State *L) +{ + if (lua_gettop(L) != 1 || !lua_isstring(L, 1)) { + lua_pushliteral(L, "a JSON string is required"); + lua_error(L); + } + + const char *json_str = lua_tostring(L, 1); + JsonNode *root_node = json_decode(json_str); + + if (!root_node) { + lua_pushliteral(L, "invalid JSON string"); + lua_error(L); + } + l_unpack_json(L, root_node); + json_delete(root_node); + + return 1; +} + /** @internal Throw Lua error if expr is false */ #define expr_checked(expr) \ if (!(expr)) { lua_pushboolean(L, false); lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); continue; } @@ -642,6 +662,8 @@ static int init_state(struct engine *engine) lua_setglobal(engine->L, "libzscanner_SONAME"); lua_pushcfunction(engine->L, l_tojson); lua_setglobal(engine->L, "tojson"); + lua_pushcfunction(engine->L, l_fromjson); + lua_setglobal(engine->L, "fromjson"); lua_pushcfunction(engine->L, l_map); lua_setglobal(engine->L, "map"); lua_pushlightuserdata(engine->L, engine); diff --git a/tests/config/basic.test.lua b/tests/config/basic.test.lua index b4d756285..1d0ea6b42 100644 --- a/tests/config/basic.test.lua +++ b/tests/config/basic.test.lua @@ -122,9 +122,32 @@ local function test_packet_functions() is(#pkt:towire(), 12, 'recycle() clears the packet wireformat') end +-- test JSON encode/decode functions +local function test_json_functions() + for msg, obj in pairs({ + ['number'] = 0, + ['string'] = 'ok', + ['list'] = {1, 2, 3}, + ['map'] = {foo='bar'}, + ['nest structure'] = {foo='bar', baz={1,2,3}}, + }) do + same(fromjson(tojson(obj)), obj, 'json test: ' .. msg) + end + + for _, str in ipairs({ + '{', '}', + '[', ']', + 'x,', + '[1,2,3,]', + }) do + boom(fromjson, {'{'}, 'json test: invalid \'' .. str .. '\'') + end +end + return { test_constants, test_globals, test_rrset_functions, test_packet_functions, + test_json_functions, }