Software /
code /
prosody
Comparison
util/serialization.lua @ 9864:9d80e679c371
util.serialization: Allow overriding table iterator
Could be useful to eg swap it out with sorted_pairs to get a stable
serialization.
Default to next() wrapper to avoid metatable tricks from pairs().
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 17 Mar 2019 21:16:27 +0100 |
parent | 9863:aebfd1601ae2 |
child | 9865:37278b420c74 |
comparison
equal
deleted
inserted
replaced
9863:aebfd1601ae2 | 9864:9d80e679c371 |
---|---|
29 char_to_hex[s_char(i)] = s_format("%02x", i); | 29 char_to_hex[s_char(i)] = s_format("%02x", i); |
30 end | 30 end |
31 | 31 |
32 local function to_hex(s) | 32 local function to_hex(s) |
33 return (s_gsub(s, ".", char_to_hex)); | 33 return (s_gsub(s, ".", char_to_hex)); |
34 end | |
35 | |
36 local function rawpairs(t) | |
37 return next, t, nil; | |
34 end | 38 end |
35 | 39 |
36 local function fatal_error(obj, why) | 40 local function fatal_error(obj, why) |
37 error("Can't serialize "..type(obj) .. (why and ": ".. why or "")); | 41 error("Can't serialize "..type(obj) .. (why and ": ".. why or "")); |
38 end | 42 end |
120 local unquoted = opt.unquoted == true and "^[%a_][%w_]*$" or opt.unquoted; | 124 local unquoted = opt.unquoted == true and "^[%a_][%w_]*$" or opt.unquoted; |
121 local hex = opt.hex; | 125 local hex = opt.hex; |
122 local freeze = opt.freeze; | 126 local freeze = opt.freeze; |
123 local maxdepth = opt.maxdepth or 127; | 127 local maxdepth = opt.maxdepth or 127; |
124 local multirefs = opt.multiref; | 128 local multirefs = opt.multiref; |
129 local table_pairs = opt.table_iterator or rawpairs; | |
125 | 130 |
126 -- serialize one table, recursively | 131 -- serialize one table, recursively |
127 -- t - table being serialized | 132 -- t - table being serialized |
128 -- o - array where tokens are added, concatenate to get final result | 133 -- o - array where tokens are added, concatenate to get final result |
129 -- - also used to detect cycles | 134 -- - also used to detect cycles |
162 o[l], l = tstart, l + 1; | 167 o[l], l = tstart, l + 1; |
163 local indent = s_rep(indentwith, d); | 168 local indent = s_rep(indentwith, d); |
164 local numkey = 1; | 169 local numkey = 1; |
165 local ktyp, vtyp; | 170 local ktyp, vtyp; |
166 local had_items = false; | 171 local had_items = false; |
167 for k,v in next,t do | 172 for k,v in table_pairs(t) do |
168 had_items = true; | 173 had_items = true; |
169 o[l], l = itemstart, l + 1; | 174 o[l], l = itemstart, l + 1; |
170 o[l], l = indent, l + 1; | 175 o[l], l = indent, l + 1; |
171 ktyp, vtyp = type(k), type(v); | 176 ktyp, vtyp = type(k), type(v); |
172 if k == numkey then | 177 if k == numkey then |