redis2json
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

redis2json.lua 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. -- redis2json
  2. -- debugging function
  3. local function logit(str)
  4. -- add line to log file
  5. redis.log(redis.LOG_WARNING, "LUA > " .. str)
  6. end -- function logit
  7. -- return Type + debug it
  8. local function getType(k)
  9. -- get type of input key
  10. local t = redis.call("TYPE", k)
  11. -- concat string for logging
  12. -- logit("Key: " .. k .. "; Type: " .. t["ok"] )
  13. return t["ok"]
  14. end -- function getType
  15. -- simple key for json string
  16. local function key2json(key, value)
  17. if tonumber(value) ~= nil then
  18. return ' "' .. key .. '": ' .. tonumber(value)
  19. else
  20. return ' "' .. key .. '": "' .. value .. '" '
  21. end
  22. end -- function key2json
  23. -- --
  24. -- programm
  25. -- --
  26. -- get all keys .. the ugly way
  27. local k = redis.call("KEYS", "*")
  28. -- jsonstring
  29. local js = '{'
  30. local var = {}
  31. -- iterate over all keys
  32. for n = 1,(#k) do
  33. local t = getType(k[n])
  34. if t == "string" then
  35. -- get key value
  36. var = redis.call("GET", k[n])
  37. -- serialize it into jsonstring
  38. js = js .. key2json(k[n], var)
  39. elseif t == "list" then
  40. -- get values of a list
  41. var = redis.call("LRANGE", k[n], "0", "-1")
  42. js = js .. ' "' .. k[n] .. '":['
  43. for l = 1,(#var) do
  44. if tonumber(var[l]) ~= nil then
  45. js = js .. ' ' .. tonumber(var[l])
  46. else
  47. js = js .. ' "' .. var[l] .. '"'
  48. end -- if
  49. -- separate list values
  50. if l < (#var) then
  51. js = js .. ','
  52. else
  53. js = js .. '] '
  54. end -- if
  55. end -- for l
  56. end -- if
  57. -- separate key values
  58. if n < (#k) then
  59. js = js .. ','
  60. else
  61. js = js .. ' }'
  62. end
  63. end -- for
  64. -- return jsonstring
  65. return js