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 local debug_traceback = debug.traceback;
18 local log = require "util.logger".init("serialization");
19 module "serialization"
21 local indent = function(i)
22 return string_rep("\t", i);
24 local function basicSerialize (o)
25 if type(o) == "number" or type(o) == "boolean" then
27 else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise.
28 return (("%q"):format(tostring(o)):gsub("\\\n", "\\n"));
31 local function _simplesave(o, ind, t, func)
32 if type(o) == "number" then
34 elseif type(o) == "string" then
35 func(t, (("%q"):format(o):gsub("\\\n", "\\n")));
36 elseif type(o) == "table" then
38 for k,v in pairs(o) do
41 func(t, basicSerialize(k));
44 _simplesave(v, 0, t, func);
46 _simplesave(v, ind+1, t, func);
50 func(t, indent(ind-1));
52 elseif type(o) == "boolean" then
53 func(t, (o and "true" or "false"));
55 log("error", "cannot serialize a %s: %s", type(o), debug_traceback())
61 _simplesave(o, 1, t, t.write or t_insert);
66 return t_concat(append({}, o));
69 function deserialize(str)
70 error("Not implemented");