lua 5.3 to add integer
teahouse opened this issue · comments
local cjson = require "cjson"
local tab = {}
tab[10] = 199
tab.pp = "fan"
local estr = cjson.encode(tab)
print("-------------", estr)
local totab = cjson.decode(estr)
for k,v in pairs(totab) do
print("---fff-------", k,v)
end
the question : 199 change to 199.0, so bad when i use to json protocol to communicate with client!
how to solve it, please.
thanks!
i think of an clumsy solution.we can edit the file lua_cjson.c.
case T_NUMBER:
{
if(token->value.number>(int)(token->value.number))
lua_pushnumber(l, token->value.number);
else
lua_pushinteger(l, token->value.number);
break;;
}
The JSON numbers are floating point (a superset of integers). Any code consuming JSON needs to handle floating point numbers to avoid problems.
Attempting to auto-detect between floats and integers will reduce performance and is likely to cause surprises elsewhere.
Just tested it versus another JSON parsers. Great performance but sample result file 4% bigger due ugly ".0" after any number. Also lua 5.3 natively support int64 while I'm sure cjson returns floats.
So I'll refrain from using it. Fast but not correct.
Just tested it versus another JSON parsers. Great performance but sample result file 4% bigger due ugly ".0" after any number. Also lua 5.3 natively support int64 while I'm sure cjson returns floats.
So I'll refrain from using it. Fast but not correct.
the clone branch https://github.com/cloudwu/lua-cjson