aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2019-03-17 21:16:27 +0100
committerKim Alvefur <zash@zash.se>2019-03-17 21:16:27 +0100
commit94ceae0f0b8560fe26ec148cbb8d8237739efc3e (patch)
treef7b397ef078972c58c678feb7da8c45acbef8aec
parentcffb6e6e7bf6c2178ab8dafd092f59c0b521d69b (diff)
downloadprosody-94ceae0f0b8560fe26ec148cbb8d8237739efc3e.tar.gz
prosody-94ceae0f0b8560fe26ec148cbb8d8237739efc3e.zip
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().
-rw-r--r--util/serialization.lua7
1 files changed, 6 insertions, 1 deletions
diff --git a/util/serialization.lua b/util/serialization.lua
index c64bfec1..2ead8c12 100644
--- a/util/serialization.lua
+++ b/util/serialization.lua
@@ -33,6 +33,10 @@ local function to_hex(s)
return (s_gsub(s, ".", char_to_hex));
end
+local function rawpairs(t)
+ return next, t, nil;
+end
+
local function fatal_error(obj, why)
error("Can't serialize "..type(obj) .. (why and ": ".. why or ""));
end
@@ -122,6 +126,7 @@ local function new(opt)
local freeze = opt.freeze;
local maxdepth = opt.maxdepth or 127;
local multirefs = opt.multiref;
+ local table_pairs = opt.table_iterator or rawpairs;
-- serialize one table, recursively
-- t - table being serialized
@@ -164,7 +169,7 @@ local function new(opt)
local numkey = 1;
local ktyp, vtyp;
local had_items = false;
- for k,v in next,t do
+ for k,v in table_pairs(t) do
had_items = true;
o[l], l = itemstart, l + 1;
o[l], l = indent, l + 1;