mpx / lua-cjson

Lua CJSON is a fast JSON encoding/parsing module for Lua
https://kyne.au/~mark/software/lua-cjson.php
MIT License
924 stars 474 forks source link

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

Open JieTrancender opened 2 years ago

JieTrancender commented 2 years ago

…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 commented 2 years ago

CC please. @mpx

onlytherailgun commented 1 month ago

It seem that the pr have not merge into master branches?