当我在redis 3.2中使用脚本设置JSON对象中的特定值时,遇到了cjson lua中的一个bug。
目前,redis中的lua无法区分空的JSON数组和空的JSON对象。这会在序列化具有数组的JSON对象时导致严重问题。
eval "local json_str = '{\"items\":[],\"properties\":{}}' return cjson.encode(cjson.decode(json_str))" 0
结果:
"{\"items\":{},\"properties\":{}}"
我找到了这个解决方案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
任何帮助或指针都将不胜感激。