Software /
code /
prosody
Annotate
util/serialization.lua @ 546:1e65f64dfabf
Added module util.serialization
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Thu, 04 Dec 2008 23:27:54 +0500 |
child | 615:4ae3e81513f3 |
rev | line source |
---|---|
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 -- Prosody IM v0.1 |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 -- Copyright (C) 2008 Matthew Wild |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 -- Copyright (C) 2008 Waqas Hussain |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 -- |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 -- This program is free software; you can redistribute it and/or |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 -- modify it under the terms of the GNU General Public License |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 -- as published by the Free Software Foundation; either version 2 |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 -- of the License, or (at your option) any later version. |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 -- |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 -- This program is distributed in the hope that it will be useful, |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 -- GNU General Public License for more details. |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 -- |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 -- You should have received a copy of the GNU General Public License |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 -- along with this program; if not, write to the Free Software |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
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 |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 local string_rep = string.rep; |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 local type = type; |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 local tostring = tostring; |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 local t_insert = table.insert; |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 local t_concat = table.concat; |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 local error = error; |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 local pairs = pairs; |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 module "serialization" |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 local indent = function(i) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 return string_rep("\t", i); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 local function basicSerialize (o) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 if type(o) == "number" or type(o) == "boolean" then |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 return tostring(o); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 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
|
37 return (("%q"):format(tostring(o)):gsub("\\\n", "\\n")); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 local function _simplesave(o, ind, t, func) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 if type(o) == "number" then |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 func(t, tostring(o)); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 elseif type(o) == "string" then |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 func(t, (("%q"):format(o):gsub("\\\n", "\\n"))); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 elseif type(o) == "table" then |
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 for k,v in pairs(o) do |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 func(t, indent(ind)); |
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 func(t, basicSerialize(k)); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 func(t, "] = "); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 if ind == 0 then |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 _simplesave(v, 0, t, func); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 else |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 _simplesave(v, ind+1, t, func); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 func(t, ",\n"); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 func(t, indent(ind-1)); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 func(t, "}"); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 elseif type(o) == "boolean" then |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 func(t, (o and "true" or "false")); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 else |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 error("cannot serialize a " .. type(o)) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 function append(t, o) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 _simplesave(o, 1, t, t.write or t_insert); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 return t; |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 function serialize(o) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 return t_concat(append({}, o)); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 function deserialize(str) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 error("Not implemented"); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 return _M; |