Software /
code /
prosody
Comparison
util/serialization.lua @ 9567:dbfa286cfa88
util.serialization: Add option for allowing multiple references to the same table (but not cycles)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 27 Oct 2018 12:43:03 +0200 |
parent | 9565:9a1e2f5f674f |
child | 9568:69f589c888e7 |
comparison
equal
deleted
inserted
replaced
9566:dad29508d0f2 | 9567:dbfa286cfa88 |
---|---|
118 local equals = opt.equals or " = "; | 118 local equals = opt.equals or " = "; |
119 local unquoted = opt.unquoted == nil and "^[%a_][%w_]*$" or opt.unquoted; | 119 local unquoted = opt.unquoted == nil and "^[%a_][%w_]*$" or opt.unquoted; |
120 local hex = opt.hex; | 120 local hex = opt.hex; |
121 local freeze = opt.freeze; | 121 local freeze = opt.freeze; |
122 local maxdepth = opt.maxdepth or 127; | 122 local maxdepth = opt.maxdepth or 127; |
123 local multirefs = opt.multiref; | |
123 | 124 |
124 -- serialize one table, recursively | 125 -- serialize one table, recursively |
125 -- t - table being serialized | 126 -- t - table being serialized |
126 -- o - array where tokens are added, concatenate to get final result | 127 -- o - array where tokens are added, concatenate to get final result |
127 -- - also used to detect cycles | 128 -- - also used to detect cycles |
134 elseif d > maxdepth then | 135 elseif d > maxdepth then |
135 o[l], l = fallback(t, "max table depth reached"), l + 1; | 136 o[l], l = fallback(t, "max table depth reached"), l + 1; |
136 return l; | 137 return l; |
137 end | 138 end |
138 | 139 |
140 -- Keep track of table loops | |
141 local ot = t; -- reference pre-freeze | |
139 o[t] = true; | 142 o[t] = true; |
143 o[ot] = true; | |
140 | 144 |
141 if freeze == true then | 145 if freeze == true then |
142 -- opportunity to do pre-serialization | 146 -- opportunity to do pre-serialization |
143 local mt = getmetatable(t); | 147 local mt = getmetatable(t); |
144 if type(mt) == "table" then | 148 if type(mt) == "table" then |
198 end | 202 end |
199 if next(t) ~= nil then | 203 if next(t) ~= nil then |
200 o[l], l = s_rep(indentwith, d-1), l + 1; | 204 o[l], l = s_rep(indentwith, d-1), l + 1; |
201 end | 205 end |
202 o[l], l = tend, l +1; | 206 o[l], l = tend, l +1; |
207 | |
208 if multirefs then | |
209 o[t] = nil; | |
210 o[ot] = nil; | |
211 end | |
212 | |
203 return l; | 213 return l; |
204 end | 214 end |
205 | 215 |
206 function types.table(t) | 216 function types.table(t) |
207 local o = {}; | 217 local o = {}; |