Redis Lua Differentiating Empty Array and Object

I ran into this error in cjson lua when I used a script in redis 3.2 to set a specific value in a json object.

Currently lua in redis does not distinguish between empty json array or empty json object. This causes serious problems when serializing json objects with arrays inside them.

eval "local json_str = '{\"items\":[],\"properties\":{}}' return cjson.encode(cjson.decode(json_str))" 0

      

Result:

"{\"items\":{},\"properties\":{}}"

      

I found this solution https://github.com/mpx/lua-cjson/issues/11 but I was unable to implement it in the redis script.

This is a failed attempt:

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

      

Any help or pointer is appreciated.

+3


source to share





All Articles