Diff

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
line wrap: on
line diff
--- a/util/serialization.lua	Sat Oct 27 12:40:47 2018 +0200
+++ b/util/serialization.lua	Sat Oct 27 12:43:03 2018 +0200
@@ -120,6 +120,7 @@
 	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 @@
 			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 @@
 			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