mirror of
https://github.com/bjc/prosody.git
synced 2025-04-01 20:27:39 +03:00
util.serialization: Add option for allowing multiple references to the same table (but not cycles)
This commit is contained in:
parent
2801e1f100
commit
fdbc23fab6
2 changed files with 25 additions and 1 deletions
|
@ -25,13 +25,27 @@ describe("util.serialization", function ()
|
|||
t[t] = { t };
|
||||
serialization.serialize(t)
|
||||
end);
|
||||
-- also with multirefs allowed
|
||||
assert.has_error(function ()
|
||||
local t = {}
|
||||
t[t] = { t };
|
||||
serialization.serialize(t, { multirefs = true })
|
||||
end);
|
||||
end);
|
||||
|
||||
it("rejects multiple references to same table", function ()
|
||||
assert.has_error(function ()
|
||||
local t1 = {};
|
||||
local t2 = { t1, t1 };
|
||||
serialization.serialize(t2);
|
||||
serialization.serialize(t2, { multirefs = false });
|
||||
end);
|
||||
end);
|
||||
|
||||
it("optionally allows multiple references to same table", function ()
|
||||
assert.has_error(function ()
|
||||
local t1 = {};
|
||||
local t2 = { t1, t1 };
|
||||
serialization.serialize(t2, { multirefs = true });
|
||||
end);
|
||||
end);
|
||||
|
||||
|
|
|
@ -120,6 +120,7 @@ local function new(opt)
|
|||
local hex = opt.hex;
|
||||
local freeze = opt.freeze;
|
||||
local maxdepth = opt.maxdepth or 127;
|
||||
local multirefs = opt.multiref;
|
||||
|
||||
-- serialize one table, recursively
|
||||
-- t - table being serialized
|
||||
|
@ -136,7 +137,10 @@ local function new(opt)
|
|||
return l;
|
||||
end
|
||||
|
||||
-- Keep track of table loops
|
||||
local ot = t; -- reference pre-freeze
|
||||
o[t] = true;
|
||||
o[ot] = true;
|
||||
|
||||
if freeze == true then
|
||||
-- opportunity to do pre-serialization
|
||||
|
@ -200,6 +204,12 @@ local function new(opt)
|
|||
o[l], l = s_rep(indentwith, d-1), l + 1;
|
||||
end
|
||||
o[l], l = tend, l +1;
|
||||
|
||||
if multirefs then
|
||||
o[t] = nil;
|
||||
o[ot] = nil;
|
||||
end
|
||||
|
||||
return l;
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue