Software /
code /
prosody
Comparison
tools/serialize.lua @ 482:b86082df0bc0
ejabberd db dump importer for Prosody
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sat, 29 Nov 2008 23:59:27 +0500 |
child | 519:cccd610a0ef9 |
comparison
equal
deleted
inserted
replaced
478:3abf90751a8f | 482:b86082df0bc0 |
---|---|
1 | |
2 local indent = function(i) | |
3 return string.rep("\t", i); | |
4 end | |
5 local function basicSerialize (o) | |
6 if type(o) == "number" or type(o) == "boolean" then | |
7 return tostring(o); | |
8 else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise. | |
9 return (string.format("%q", tostring(o)):gsub("\\\n", "\\n")); | |
10 end | |
11 end | |
12 local function _simplesave (o, ind, t) | |
13 if type(o) == "number" then | |
14 table.insert(t, tostring(o)); | |
15 elseif type(o) == "string" then | |
16 table.insert(t, (string.format("%q", o):gsub("\\\n", "\\n"))); | |
17 elseif type(o) == "table" then | |
18 table.insert(t, "{\n"); | |
19 for k,v in pairs(o) do | |
20 table.insert(t, indent(ind)); | |
21 table.insert(t, "["); | |
22 table.insert(t, basicSerialize(k)); | |
23 table.insert(t, "] = "); | |
24 _simplesave(v, ind+1, t); | |
25 table.insert(t, ",\n"); | |
26 end | |
27 table.insert(t, indent(ind-1)); | |
28 table.insert(t, "}"); | |
29 elseif type(o) == "boolean" then | |
30 table.insert(t, (o and "true" or "false")); | |
31 else | |
32 error("cannot serialize a " .. type(o)) | |
33 end | |
34 end | |
35 local t_concat = table.concat; | |
36 | |
37 module "serialize" | |
38 | |
39 function serialize(o) | |
40 local t = {}; | |
41 _simplesave(o, 1, t); | |
42 return t_concat(t); | |
43 end | |
44 | |
45 return _M; |