474b5d655c285248330b06ae673843c3bfdc6662
[prosody.git] / util / serialization.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local string_rep = string.rep;
10 local type = type;
11 local tostring = tostring;
12 local t_insert = table.insert;
13 local t_concat = table.concat;
14 local error = error;
15 local pairs = pairs;
16 local next = next;
17
18 local loadstring = loadstring;
19 local setfenv = setfenv;
20 local pcall = pcall;
21
22 local debug_traceback = debug.traceback;
23 local log = require "util.logger".init("serialization");
24 module "serialization"
25
26 local indent = function(i)
27         return string_rep("\t", i);
28 end
29 local function basicSerialize (o)
30         if type(o) == "number" or type(o) == "boolean" then
31                 return tostring(o);
32         else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise.
33                 return (("%q"):format(tostring(o)):gsub("\\\n", "\\n"));
34         end
35 end
36 local function _simplesave(o, ind, t, func)
37         if type(o) == "number" then
38                 func(t, tostring(o));
39         elseif type(o) == "string" then
40                 func(t, (("%q"):format(o):gsub("\\\n", "\\n")));
41         elseif type(o) == "table" then
42                 if next(o) ~= nil then
43                         func(t, "{\n");
44                         for k,v in pairs(o) do
45                                 func(t, indent(ind));
46                                 func(t, "[");
47                                 func(t, basicSerialize(k));
48                                 func(t, "] = ");
49                                 if ind == 0 then
50                                         _simplesave(v, 0, t, func);
51                                 else
52                                         _simplesave(v, ind+1, t, func);
53                                 end
54                                 func(t, ";\n");
55                         end
56                         func(t, indent(ind-1));
57                         func(t, "}");
58                 else
59                         func(t, "{}");
60                 end
61         elseif type(o) == "boolean" then
62                 func(t, (o and "true" or "false"));
63         else
64                 log("error", "cannot serialize a %s: %s", type(o), debug_traceback())
65                 func(t, "nil");
66         end
67 end
68
69 function append(t, o)
70         _simplesave(o, 1, t, t.write or t_insert);
71         return t;
72 end
73
74 function serialize(o)
75         return t_concat(append({}, o));
76 end
77
78 function deserialize(str)
79         if type(str) ~= "string" then return nil; end
80         str = "return "..str;
81         local f, err = loadstring(str, "@data");
82         if not f then return nil, err; end
83         setfenv(f, {});
84         local success, ret = pcall(f);
85         if not success then return nil, ret; end
86         return ret;
87 end
88
89 return _M;