Procházet zdrojové kódy

Merge pull request #1417 from rts-cn/fix-lua-json

[mod_lua] fix json encoding of lua number string
Andrey Volk před 3 roky
rodič
revize
f96f83ee3b

+ 3 - 3
src/mod/languages/mod_lua/freeswitch_lua.cpp

@@ -568,11 +568,11 @@ void JSON::LuaTable2cJSON(lua_State *L, int index, cJSON **json)
 
 		switch_assert(*json);
 
-		if (lua_isnumber(L, -2)) {
+		if (lua_type(L, -2) == LUA_TNUMBER) {
 			ADDITEM(*json, key, cJSON_CreateNumber(lua_tonumber(L, -2)));
-		} else if (lua_isstring(L, -2)) {
+		} else if (lua_type(L, -2) == LUA_TSTRING) {
 			ADDITEM(*json, key, cJSON_CreateString(lua_tostring(L, -2)));
-		} else if (lua_isboolean(L, -2)) {
+		} else if (lua_type(L, -2) == LUA_TBOOLEAN) {
 			ADDITEM(*json, key, cJSON_CreateBool(lua_toboolean(L, -2)));
 		} else if (lua_isnil(L, -2)) {
 			ADDITEM(*json, key, cJSON_CreateNull());

+ 6 - 0
src/mod/languages/mod_lua/test/test_json.lua

@@ -108,5 +108,11 @@ freeswitch.consoleLog("INFO", ret .. "\n")
 ret = json:execute2(json:encode(cmd))
 freeswitch.consoleLog("INFO", ret .. "\n")
 
+sn = json:encode({s = "1001", n = 1002})
+freeswitch.consoleLog("INFO", sn .. "\n")
+ret = json:decode(sn)
+assert(ret.s == "1001")
+assert(ret.n == 1002)
+
 -- assert(false)
 stream:write("+OK")