serialization.lua

Fri, 29 Mar 2013 15:01:47 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 29 Mar 2013 15:01:47 +0000
changeset 3
c16d25785df1
parent 0
11a0cbd6c216
permissions
-rw-r--r--

Perform line unfolding

0
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Prosody IM v0.4
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local string_rep = string.rep;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local type = type;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local tostring = tostring;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local t_insert = table.insert;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local t_concat = table.concat;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local error = error;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local pairs = pairs;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 module "serialization"
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local indent = function(i)
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 return string_rep("\t", i);
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local function basicSerialize (o)
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 if type(o) == "number" or type(o) == "boolean" then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return tostring(o);
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise.
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 return (("%q"):format(tostring(o)):gsub("\\\n", "\\n"));
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local function _simplesave(o, ind, t, func)
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 if type(o) == "number" then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 func(t, tostring(o));
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 elseif type(o) == "string" then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 func(t, (("%q"):format(o):gsub("\\\n", "\\n")));
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 elseif type(o) == "table" then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 func(t, "{\n");
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 for k,v in pairs(o) do
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 func(t, indent(ind));
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 func(t, "[");
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 func(t, basicSerialize(k));
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 func(t, "] = ");
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 if ind == 0 then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 _simplesave(v, 0, t, func);
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 else
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 _simplesave(v, ind+1, t, func);
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 func(t, ",\n");
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 func(t, indent(ind-1));
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 func(t, "}");
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 elseif type(o) == "boolean" then
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 func(t, (o and "true" or "false"));
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 else
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 error("cannot serialize a " .. type(o))
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 function append(t, o)
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 _simplesave(o, 1, t, t.write or t_insert);
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 return t;
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 function serialize(o)
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 return t_concat(append({}, o));
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 function deserialize(str)
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 error("Not implemented");
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
11a0cbd6c216 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 return _M;

mercurial