Software /
code /
prosody
Annotate
util/serialization.lua @ 1801:71fc110b256d
Merge with 0.5
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 20 Sep 2009 18:46:35 +0100 |
parent | 1523:841d61be198f |
child | 2148:5590c13552ab |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1135
diff
changeset
|
1 -- Prosody IM |
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 | 5 -- This project is MIT/X11 licensed. Please see the |
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 |
1107
ebda1845ebc1
util.serialization: Log a warning when trying to serialize something we can't
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
17 local debug_traceback = debug.traceback; |
ebda1845ebc1
util.serialization: Log a warning when trying to serialize something we can't
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
18 local log = require "util.logger".init("serialization"); |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 module "serialization" |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 local indent = function(i) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 return string_rep("\t", i); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 local function basicSerialize (o) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 if type(o) == "number" or type(o) == "boolean" then |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 return tostring(o); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 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
|
28 return (("%q"):format(tostring(o)):gsub("\\\n", "\\n")); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 local function _simplesave(o, ind, t, func) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 if type(o) == "number" then |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 func(t, tostring(o)); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 elseif type(o) == "string" then |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 func(t, (("%q"):format(o):gsub("\\\n", "\\n"))); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 elseif type(o) == "table" then |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 func(t, "{\n"); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 for k,v in pairs(o) do |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 func(t, indent(ind)); |
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 func(t, basicSerialize(k)); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 func(t, "] = "); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 if ind == 0 then |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 _simplesave(v, 0, t, func); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 else |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 _simplesave(v, ind+1, t, func); |
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, ",\n"); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 func(t, indent(ind-1)); |
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 elseif type(o) == "boolean" then |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 func(t, (o and "true" or "false")); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 else |
1128
b2e548344d61
util.serialization: Write nil for non-serializable data types, and bump the log level to 'error'
Matthew Wild <mwild1@gmail.com>
parents:
1107
diff
changeset
|
55 log("error", "cannot serialize a %s: %s", type(o), debug_traceback()) |
1135
355f9487ab38
util/serialization: Fixed serialization formatting
Waqas Hussain <waqas20@gmail.com>
parents:
1128
diff
changeset
|
56 func(t, "nil"); |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 end |
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 |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 function append(t, o) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 _simplesave(o, 1, t, t.write or t_insert); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 return t; |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 function serialize(o) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 return t_concat(append({}, o)); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 function deserialize(str) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 error("Not implemented"); |
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 return _M; |