2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 local string_rep = string.rep;
22 local tostring = tostring;
23 local t_insert = table.insert;
24 local t_concat = table.concat;
28 module "serialization"
30 local indent = function(i)
31 return string_rep("\t", i);
33 local function basicSerialize (o)
34 if type(o) == "number" or type(o) == "boolean" then
36 else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise.
37 return (("%q"):format(tostring(o)):gsub("\\\n", "\\n"));
40 local function _simplesave(o, ind, t, func)
41 if type(o) == "number" then
43 elseif type(o) == "string" then
44 func(t, (("%q"):format(o):gsub("\\\n", "\\n")));
45 elseif type(o) == "table" then
47 for k,v in pairs(o) do
50 func(t, basicSerialize(k));
53 _simplesave(v, 0, t, func);
55 _simplesave(v, ind+1, t, func);
59 func(t, indent(ind-1));
61 elseif type(o) == "boolean" then
62 func(t, (o and "true" or "false"));
64 error("cannot serialize a " .. type(o))
69 _simplesave(o, 1, t, t.write or t_insert);
74 return t_concat(append({}, o));
77 function deserialize(str)
78 error("Not implemented");