diff options
author | Diego Martinez <kaeza@users.sf.net> | 2014-05-19 23:23:43 -0300 |
---|---|---|
committer | Diego Martinez <kaeza@users.sf.net> | 2014-05-19 23:23:43 -0300 |
commit | 8695476bebaea7469ea1aaf4c13ec8e020353d39 (patch) | |
tree | 4c5be6e71a9926881b8a932c9a7f5a3f1a9e3751 /serialize.lua |
First commit.
Diffstat (limited to 'serialize.lua')
-rw-r--r-- | serialize.lua | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/serialize.lua b/serialize.lua new file mode 100644 index 0000000..d9177d9 --- /dev/null +++ b/serialize.lua @@ -0,0 +1,31 @@ + +local function repr(x) + if type(x) == "string" then + return ("%q"):format(x) + else + return tostring(x) + end +end + +local function my_serialize_2(t, level) + level = level or 0 + local lines = { } + local indent = (" "):rep(level) + for k, v in pairs(t) do + local typ = type(v) + if typ == "table" then + table.insert(lines, + indent..("[%s] = {\n"):format(repr(k)) + ..my_serialize_2(v, level + 1).."\n" + ..indent.."},") + else + table.insert(lines, + indent..("[%s] = %s,"):format(repr(k), repr(v))) + end + end + return table.concat(lines, "\n") +end + +function xban.serialize(t) + return "return {\n"..my_serialize_2(t, 1).."\n}" +end |