2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 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;
18 local debug_traceback = debug.traceback;
19 local log = require "util.logger".init("serialization");
20 module "serialization"
22 local indent = function(i)
23 return string_rep("\t", i);
25 local function basicSerialize (o)
26 if type(o) == "number" or type(o) == "boolean" then
28 else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise.
29 return (("%q"):format(tostring(o)):gsub("\\\n", "\\n"));
32 local function _simplesave(o, ind, t, func)
33 if type(o) == "number" then
35 elseif type(o) == "string" then
36 func(t, (("%q"):format(o):gsub("\\\n", "\\n")));
37 elseif type(o) == "table" then
38 if next(o) ~= nil then
40 for k,v in pairs(o) do
43 func(t, basicSerialize(k));
46 _simplesave(v, 0, t, func);
48 _simplesave(v, ind+1, t, func);
52 func(t, indent(ind-1));
57 elseif type(o) == "boolean" then
58 func(t, (o and "true" or "false"));
60 log("error", "cannot serialize a %s: %s", type(o), debug_traceback())
66 _simplesave(o, 1, t, t.write or t_insert);
71 return t_concat(append({}, o));
74 function deserialize(str)
75 error("Not implemented");