From e53d2bd66b1b0bebcc355c165c1060a4490f01ab Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sat, 6 Aug 2016 21:10:41 +0000 Subject: MirLua: m_json: - added simple arrays support - added integer support git-svn-id: http://svn.miranda-ng.org/main/trunk@17165 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/Modules/m_json/src/main.cpp | 2 +- plugins/MirLua/Modules/m_json/src/metatable.cpp | 37 +++++++++++++++++++------ 2 files changed, 30 insertions(+), 9 deletions(-) (limited to 'plugins/MirLua/Modules') diff --git a/plugins/MirLua/Modules/m_json/src/main.cpp b/plugins/MirLua/Modules/m_json/src/main.cpp index c26f350f63..fd7c40b1ff 100644 --- a/plugins/MirLua/Modules/m_json/src/main.cpp +++ b/plugins/MirLua/Modules/m_json/src/main.cpp @@ -28,7 +28,7 @@ static int lua_Encode(lua_State *L) static const luaL_Reg methods[] = { - { "Dcode", lua_Decode }, + { "Decode", lua_Decode }, { "Encode", lua_Encode }, { NULL, NULL } diff --git a/plugins/MirLua/Modules/m_json/src/metatable.cpp b/plugins/MirLua/Modules/m_json/src/metatable.cpp index 5ea9697d69..976ccd9b16 100644 --- a/plugins/MirLua/Modules/m_json/src/metatable.cpp +++ b/plugins/MirLua/Modules/m_json/src/metatable.cpp @@ -14,27 +14,44 @@ void lua2json(lua_State *L, JSONNode &node) node = lua_toboolean(L, -1) != 0; break; case LUA_TNUMBER: - node = lua_tonumber(L, -1); + { + lua_Integer val = lua_tointeger(L, -1); + if (lua_isinteger(L, -1) && val >= INT_MIN && val <= INT_MAX) + node = (int)val; + else + node = lua_tonumber(L, -1); break; + } case LUA_TTABLE: { - node.cast(JSON_NODE); + ptrA name(mir_strdup(node.name())); + node.cast(JSON_ARRAY); + node.set_name((char*)name); lua_pushnil(L); while (lua_next(L, -2) != 0) { JSONNode child; + if (!lua_isnumber(L, -2)) + { + if (node.type() == JSON_ARRAY) + { + node.cast(JSON_NODE); + node.set_name((char*)name); + } + + const char *key = lua_tostring(L, -2); + child.set_name(key); + } lua2json(L, child); node << child; lua_pop(L, 1); } + + break; } } - - const char *name = lua_tostring(L, -2); - if (name) - node.set_name(name); } static int json__index(lua_State *L) @@ -73,10 +90,14 @@ static int json__newindex(lua_State *L) if (child.isnull()) { child.set_name(key); + lua2json(L, child); node << child; } - lua2json(L, child); - node[key] = child; + else + { + lua2json(L, child); + node[key] = child; + } return 0; } -- cgit v1.2.3