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

perf: using nil replace userdata: (nil) while decoding string which c…

Open JieTrancender opened this issue 4 years ago • 2 comments

…ontains null

A null pointer substitution is currently used while parsing a string contains null, which is not expected by those who are unaware of this rule. Is it better to keep the semantics as they are?


/* In Lua, setting "t[k] = nil" will delete k from the table.
 * Hence a NULL pointer lightuserdata object is used instead */
// lua_pushlightuserdata(l, NULL);

The changed test code and output are shown below, perhaps more as expected

local tbl = {1, nil, "hello world"}
local encodeStr = json.encode(tbl)
print("encodeStr", encodeStr)
local decodeTbl = json.decode(encodeStr)
for k, v in pairs(decodeTbl) do
    print(k, v)
end
encodeStr = json.encode(decodeTbl)
print("encodeStr", encodeStr)
decodeTbl = json.decode(encodeStr)
for k, v in pairs(decodeTbl) do
    print(k, v)
end

local tbl = {a = "a", b = "b", c = "c"}
encodeStr = json.encode(tbl)
print("encodeStr", encodeStr)
decodeTbl = json.decode(encodeStr)
for k, v in pairs(decodeTbl) do
    print(k, v)
end

encodeStr = json.encode(decodeTbl)
print("encodeStr", encodeStr)
decodeTbl = json.decode(encodeStr)
for k, v in pairs(decodeTbl) do
    print(k, v)
end

输出:
encodeStr	[1,null,"hello world"]
1	1
3	hello world
encodeStr	[1,null,"hello world"]
1	1
3	hello world
encodeStr	{"c":"c","b":"b","a":"a"}
c	c
b	b
a	a
encodeStr	{"c":"c","b":"b","a":"a"}
c	c
b	b
a	a

JieTrancender avatar Dec 03 '21 09:12 JieTrancender

CC please. @mpx

JieTrancender avatar Dec 09 '21 01:12 JieTrancender