Software /
code /
prosody
Comparison
util/serialization.lua @ 11200:bf8f2da84007
Merge 0.11->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 05 Nov 2020 22:31:25 +0100 |
parent | 11093:ec14d4fce855 |
child | 12781:22066b02887f |
comparison
equal
deleted
inserted
replaced
11199:6c7c50a4de32 | 11200:bf8f2da84007 |
---|---|
14 local s_rep = string.rep; | 14 local s_rep = string.rep; |
15 local s_char = string.char; | 15 local s_char = string.char; |
16 local s_match = string.match; | 16 local s_match = string.match; |
17 local t_concat = table.concat; | 17 local t_concat = table.concat; |
18 | 18 |
19 local to_hex = require "util.hex".to; | |
20 | |
19 local pcall = pcall; | 21 local pcall = pcall; |
20 local envload = require"util.envload".envload; | 22 local envload = require"util.envload".envload; |
21 | 23 |
22 local pos_inf, neg_inf = math.huge, -math.huge; | 24 local pos_inf, neg_inf = math.huge, -math.huge; |
23 -- luacheck: ignore 143/math | |
24 local m_type = math.type or function (n) | 25 local m_type = math.type or function (n) |
25 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float"; | 26 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float"; |
26 end; | 27 end; |
27 | 28 |
28 local char_to_hex = {}; | 29 local function rawpairs(t) |
29 for i = 0,255 do | 30 return next, t, nil; |
30 char_to_hex[s_char(i)] = s_format("%02x", i); | |
31 end | |
32 | |
33 local function to_hex(s) | |
34 return (s_gsub(s, ".", char_to_hex)); | |
35 end | 31 end |
36 | 32 |
37 local function fatal_error(obj, why) | 33 local function fatal_error(obj, why) |
38 error("Can't serialize "..type(obj) .. (why and ": ".. why or "")); | 34 error("Can't serialize "..type(obj) .. (why and ": ".. why or "")); |
39 end | 35 end |
121 local unquoted = opt.unquoted == true and "^[%a_][%w_]*$" or opt.unquoted; | 117 local unquoted = opt.unquoted == true and "^[%a_][%w_]*$" or opt.unquoted; |
122 local hex = opt.hex; | 118 local hex = opt.hex; |
123 local freeze = opt.freeze; | 119 local freeze = opt.freeze; |
124 local maxdepth = opt.maxdepth or 127; | 120 local maxdepth = opt.maxdepth or 127; |
125 local multirefs = opt.multiref; | 121 local multirefs = opt.multiref; |
122 local table_pairs = opt.table_iterator or rawpairs; | |
126 | 123 |
127 -- serialize one table, recursively | 124 -- serialize one table, recursively |
128 -- t - table being serialized | 125 -- t - table being serialized |
129 -- o - array where tokens are added, concatenate to get final result | 126 -- o - array where tokens are added, concatenate to get final result |
130 -- - also used to detect cycles | 127 -- - also used to detect cycles |
151 local tag = mt.__name; | 148 local tag = mt.__name; |
152 local fr = mt.__freeze; | 149 local fr = mt.__freeze; |
153 | 150 |
154 if type(fr) == "function" then | 151 if type(fr) == "function" then |
155 t = fr(t); | 152 t = fr(t); |
153 if type(t) == "string" then | |
154 o[l], l = t, l + 1; | |
155 return l; | |
156 end | |
156 if type(tag) == "string" then | 157 if type(tag) == "string" then |
157 o[l], l = tag, l + 1; | 158 o[l], l = tag, l + 1; |
158 end | 159 end |
159 end | 160 end |
160 end | 161 end |
162 | 163 |
163 o[l], l = tstart, l + 1; | 164 o[l], l = tstart, l + 1; |
164 local indent = s_rep(indentwith, d); | 165 local indent = s_rep(indentwith, d); |
165 local numkey = 1; | 166 local numkey = 1; |
166 local ktyp, vtyp; | 167 local ktyp, vtyp; |
167 for k,v in next,t do | 168 local had_items = false; |
169 for k,v in table_pairs(t) do | |
170 had_items = true; | |
168 o[l], l = itemstart, l + 1; | 171 o[l], l = itemstart, l + 1; |
169 o[l], l = indent, l + 1; | 172 o[l], l = indent, l + 1; |
170 ktyp, vtyp = type(k), type(v); | 173 ktyp, vtyp = type(k), type(v); |
171 if k == numkey then | 174 if k == numkey then |
172 -- next index in array part | 175 -- next index in array part |
193 if vtyp == "table" then | 196 if vtyp == "table" then |
194 l = serialize_table(v, o, l, d+1); | 197 l = serialize_table(v, o, l, d+1); |
195 else | 198 else |
196 o[l], l = ser(v), l + 1; | 199 o[l], l = ser(v), l + 1; |
197 end | 200 end |
198 -- last item? | 201 o[l], l = itemsep, l + 1; |
199 if next(t, k) ~= nil then | 202 end |
200 o[l], l = itemsep, l + 1; | 203 if had_items then |
201 else | 204 o[l - 1] = itemlast; |
202 o[l], l = itemlast, l + 1; | |
203 end | |
204 end | |
205 if next(t) ~= nil then | |
206 o[l], l = s_rep(indentwith, d-1), l + 1; | 205 o[l], l = s_rep(indentwith, d-1), l + 1; |
207 end | 206 end |
208 o[l], l = tend, l +1; | 207 o[l], l = tend, l +1; |
209 | 208 |
210 if multirefs then | 209 if multirefs then |