当我在 redis 3.2 中使用脚本在 json 对象中设置特定值时,我在 cjson lua 中遇到了这个错误。
目前,redis 中的 lua 不区分空的 json 数组或空的 json 对象。在序列化包含数组的 json 对象时,这会导致严重的问题。
eval "local json_str = '{\"items\":[],\"properties\":{}}' return cjson.encode(cjson.decode(json_str))" 0
Run Code Online (Sandbox Code Playgroud)
结果:
"{\"items\":{},\"properties\":{}}"
Run Code Online (Sandbox Code Playgroud)
我找到了这个解决方案https://github.com/mpx/lua-cjson/issues/11但我无法在 redis 脚本中实现。
这是一次失败的尝试:
eval
"function cjson.mark_as_array(t)
local mt = getmetatable(t) or {}
mt.__is_cjson_array = true
return setmetatable(t, mt)
end
function cjson.is_marked_as_array(t)
local mt = getmetatable(t)
return mt and mt.__is_cjson_array end
local json_str = '{\"items\":[],\"properties\":{}}'
return cjson.encode(cjson.decode(json_str))"
0
Run Code Online (Sandbox Code Playgroud)
任何帮助或指针表示赞赏。