"null" will be decoded as ngx.null
mike07026 opened this issue · 4 comments
mike07026 commented
local cjson = require "cjson.safe"
local r = cjson.decode("null")
ngx.say(r == ngx.null) -- true here
I think "null" is not a valid json string, and expect decode result is nil, but r == ngx.null here.
Maybe it is better to document this behavior or just fix it in cjson source code?
bungle commented
I don't agree. JSON literal null
has to be decoded as ngx.null
, because Lua nil
cannot be present in Lua tables. This is absolutely correct behavior, and consistent with null
literal appearing in JSON arrays and objects (otherwise those can not be presented in Lua).
mike07026 commented
bungle commented
Well, this is a library, why remove possibility to decode JSON values, and only support JSON documents? You can easily test if it was a document by checking the return type of decode with type(json) == "table"
.