Annotate

util/serialization.lua @ 988:8cb9a0d59e78

util.lsignal: Add lsignal library for catching POSIX signals
author Matthew Wild <mwild1@gmail.com>
date Sun, 12 Apr 2009 02:53:50 +0100
parent 896:2c0b9e3c11c3
child 1107:ebda1845ebc1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
896
2c0b9e3c11c3 0.3->0.4
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
1 -- Prosody IM v0.4
760
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
546
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 --
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
6 -- COPYING file in the source package for more information.
546
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 --
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local string_rep = string.rep;
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local type = type;
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local tostring = tostring;
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local t_insert = table.insert;
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local t_concat = table.concat;
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local error = error;
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local pairs = pairs;
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 module "serialization"
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 local indent = function(i)
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 return string_rep("\t", i);
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 end
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 local function basicSerialize (o)
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 if type(o) == "number" or type(o) == "boolean" then
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 return tostring(o);
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise.
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 return (("%q"):format(tostring(o)):gsub("\\\n", "\\n"));
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 end
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 end
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 local function _simplesave(o, ind, t, func)
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 if type(o) == "number" then
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 func(t, tostring(o));
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 elseif type(o) == "string" then
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 func(t, (("%q"):format(o):gsub("\\\n", "\\n")));
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 elseif type(o) == "table" then
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 func(t, "{\n");
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 for k,v in pairs(o) do
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 func(t, indent(ind));
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 func(t, "[");
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 func(t, basicSerialize(k));
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 func(t, "] = ");
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 if ind == 0 then
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 _simplesave(v, 0, t, func);
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 else
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 _simplesave(v, ind+1, t, func);
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 end
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 func(t, ",\n");
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 end
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 func(t, indent(ind-1));
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 func(t, "}");
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 elseif type(o) == "boolean" then
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 func(t, (o and "true" or "false"));
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 else
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 error("cannot serialize a " .. type(o))
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 end
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 end
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 function append(t, o)
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 _simplesave(o, 1, t, t.write or t_insert);
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 return t;
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 end
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 function serialize(o)
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 return t_concat(append({}, o));
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 end
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 function deserialize(str)
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 error("Not implemented");
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 end
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 return _M;