lua-cjson icon indicating copy to clipboard operation
lua-cjson copied to clipboard

lua 5.3 to add integer

Open teahouse opened this issue 8 years ago • 4 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!

teahouse avatar Jun 01 '16 09:06 teahouse

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;; }

teahouse avatar Jun 02 '16 06:06 teahouse

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.

mpx avatar Aug 24 '16 11:08 mpx

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.

martin-eden avatar Jan 29 '17 16:01 martin-eden

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

jason-fan avatar Aug 10 '19 08:08 jason-fan