2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local string_rep = string.rep;
11 local tostring = tostring;
12 local t_insert = table.insert;
13 local t_concat = table.concat;
17 module "serialization"
19 local indent = function(i)
20 return string_rep("\t", i);
22 local function basicSerialize (o)
23 if type(o) == "number" or type(o) == "boolean" then
25 else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise.
26 return (("%q"):format(tostring(o)):gsub("\\\n", "\\n"));
29 local function _simplesave(o, ind, t, func)
30 if type(o) == "number" then
32 elseif type(o) == "string" then
33 func(t, (("%q"):format(o):gsub("\\\n", "\\n")));
34 elseif type(o) == "table" then
36 for k,v in pairs(o) do
39 func(t, basicSerialize(k));
42 _simplesave(v, 0, t, func);
44 _simplesave(v, ind+1, t, func);
48 func(t, indent(ind-1));
50 elseif type(o) == "boolean" then
51 func(t, (o and "true" or "false"));
53 error("cannot serialize a " .. type(o))
58 _simplesave(o, 1, t, t.write or t_insert);
63 return t_concat(append({}, o));
66 function deserialize(str)
67 error("Not implemented");