Software /
code /
prosody
Annotate
util/serialization.lua @ 8555:4f0f5b49bb03
vairious: Add annotation when an empty environment is set [luacheck]
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 28 Feb 2018 20:06:26 +0100 |
parent | 6777:5de6b93d0190 |
child | 9008:ae3c52419ec1 |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1135
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2222
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2222
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5021
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 pairs = pairs; |
2148
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
15 local next = next; |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 |
3736
73399dd525e8
util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
17 local pcall = pcall; |
73399dd525e8
util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
18 |
1107
ebda1845ebc1
util.serialization: Log a warning when trying to serialize something we can't
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
19 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
|
20 local log = require "util.logger".init("serialization"); |
5021
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
3745
diff
changeset
|
21 local envload = require"util.envload".envload; |
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
3745
diff
changeset
|
22 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6670
diff
changeset
|
23 local _ENV = nil; |
8555
4f0f5b49bb03
vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
24 -- luacheck: std none |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 local indent = function(i) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 return string_rep("\t", i); |
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 basicSerialize (o) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 if type(o) == "number" or type(o) == "boolean" then |
3745
87f6eabd90c9
util.serialization: Proper serialization of Infinity, -Infinity and NaN.
Waqas Hussain <waqas20@gmail.com>
parents:
3736
diff
changeset
|
31 -- no need to check for NaN, as that's not a valid table index |
87f6eabd90c9
util.serialization: Proper serialization of Infinity, -Infinity and NaN.
Waqas Hussain <waqas20@gmail.com>
parents:
3736
diff
changeset
|
32 if o == 1/0 then return "(1/0)"; |
87f6eabd90c9
util.serialization: Proper serialization of Infinity, -Infinity and NaN.
Waqas Hussain <waqas20@gmail.com>
parents:
3736
diff
changeset
|
33 elseif o == -1/0 then return "(-1/0)"; |
87f6eabd90c9
util.serialization: Proper serialization of Infinity, -Infinity and NaN.
Waqas Hussain <waqas20@gmail.com>
parents:
3736
diff
changeset
|
34 else return tostring(o); end |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 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
|
36 return (("%q"):format(tostring(o)):gsub("\\\n", "\\n")); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 end |
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 local function _simplesave(o, ind, t, func) |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 if type(o) == "number" then |
3745
87f6eabd90c9
util.serialization: Proper serialization of Infinity, -Infinity and NaN.
Waqas Hussain <waqas20@gmail.com>
parents:
3736
diff
changeset
|
41 if o ~= o then func(t, "(0/0)"); |
87f6eabd90c9
util.serialization: Proper serialization of Infinity, -Infinity and NaN.
Waqas Hussain <waqas20@gmail.com>
parents:
3736
diff
changeset
|
42 elseif o == 1/0 then func(t, "(1/0)"); |
87f6eabd90c9
util.serialization: Proper serialization of Infinity, -Infinity and NaN.
Waqas Hussain <waqas20@gmail.com>
parents:
3736
diff
changeset
|
43 elseif o == -1/0 then func(t, "(-1/0)"); |
87f6eabd90c9
util.serialization: Proper serialization of Infinity, -Infinity and NaN.
Waqas Hussain <waqas20@gmail.com>
parents:
3736
diff
changeset
|
44 else func(t, tostring(o)); end |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 elseif type(o) == "string" then |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 func(t, (("%q"):format(o):gsub("\\\n", "\\n"))); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 elseif type(o) == "table" then |
2222
81b4e738e4d3
util.serialization: Correctly serialize tables with 'false' as a key, fixes an issue with rosters not saving (thanks mathias, Tobias)
Matthew Wild <mwild1@gmail.com>
parents:
2149
diff
changeset
|
48 if next(o) ~= nil then |
2148
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
49 func(t, "{\n"); |
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
50 for k,v in pairs(o) do |
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
51 func(t, indent(ind)); |
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
52 func(t, "["); |
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
53 func(t, basicSerialize(k)); |
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
54 func(t, "] = "); |
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
55 if ind == 0 then |
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
56 _simplesave(v, 0, t, func); |
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
57 else |
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
58 _simplesave(v, ind+1, t, func); |
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
59 end |
2149
603134825bdb
util.serialization: Replaced commas with semi-colons between table fields.
Waqas Hussain <waqas20@gmail.com>
parents:
2148
diff
changeset
|
60 func(t, ";\n"); |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 end |
2148
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
62 func(t, indent(ind-1)); |
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
63 func(t, "}"); |
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
64 else |
5590c13552ab
util.serialization: Concise output for empty tables.
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
65 func(t, "{}"); |
546
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 elseif type(o) == "boolean" then |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 func(t, (o and "true" or "false")); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 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
|
70 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
|
71 func(t, "nil"); |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6670
diff
changeset
|
75 local function append(t, o) |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 _simplesave(o, 1, t, t.write or t_insert); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 return t; |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6670
diff
changeset
|
80 local function serialize(o) |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 return t_concat(append({}, o)); |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6670
diff
changeset
|
84 local function deserialize(str) |
3736
73399dd525e8
util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
85 if type(str) ~= "string" then return nil; end |
73399dd525e8
util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
86 str = "return "..str; |
5021
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
3745
diff
changeset
|
87 local f, err = envload(str, "@data", {}); |
3736
73399dd525e8
util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
88 if not f then return nil, err; end |
73399dd525e8
util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
89 local success, ret = pcall(f); |
73399dd525e8
util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
90 if not success then return nil, ret; end |
73399dd525e8
util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
91 return ret; |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6670
diff
changeset
|
94 return { |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6670
diff
changeset
|
95 append = append; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6670
diff
changeset
|
96 serialize = serialize; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6670
diff
changeset
|
97 deserialize = deserialize; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6670
diff
changeset
|
98 }; |