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