serialize.lua 646 B

1234567891011121314151617181920212223242526272829303132
  1. local function repr(x)
  2. if type(x) == "string" then
  3. return ("%q"):format(x)
  4. else
  5. return tostring(x)
  6. end
  7. end
  8. local function my_serialize_2(t, level)
  9. level = level or 0
  10. local lines = { }
  11. local indent = ("\t"):rep(level)
  12. for k, v in pairs(t) do
  13. local typ = type(v)
  14. if typ == "table" then
  15. table.insert(lines,
  16. indent..("[%s] = {\n"):format(repr(k))
  17. ..my_serialize_2(v, level + 1).."\n"
  18. ..indent.."},")
  19. else
  20. table.insert(lines,
  21. indent..("[%s] = %s,"):format(repr(k), repr(v)))
  22. end
  23. end
  24. return table.concat(lines, "\n")
  25. end
  26. function xban.serialize(t)
  27. return "return {\n"..my_serialize_2(t, 1).."\n}"
  28. end