From 33ef3a1da2533be6a0ce1d425d80a7b911c406d9 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Thu, 28 Aug 2008 16:40:51 +0000 Subject: [PATCH] * luci/libs: util: further enhancements to table serialisation --- libs/core/luasrc/util.lua | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/libs/core/luasrc/util.lua b/libs/core/luasrc/util.lua index 2a7e70453..bf463ae70 100644 --- a/libs/core/luasrc/util.lua +++ b/libs/core/luasrc/util.lua @@ -436,20 +436,27 @@ function _serialize_table(t, seen) assert(not seen[t], "Recursion detected.") seen[t] = true - local data = "" - for i = 1, #t do - local v = serialize_data(t[i], seen) - data = data .. ( #data > 0 and ", " or "" ) .. v - end + local data = "" + local idata = "" + local ilen = 0 + for k, v in pairs(t) do - if type(k) ~= "number" then + if type(k) ~= "number" or k < 1 or math.floor(k) ~= k or ( k - #t ) > 3 then k = serialize_data(k, seen) v = serialize_data(v, seen) data = data .. ( #data > 0 and ", " or "" ) .. '[' .. k .. '] = ' .. v + elseif k > ilen then + ilen = k end end - return data + + for i = 1, ilen do + local v = serialize_data(t[i], seen) + idata = idata .. ( #idata > 0 and ", " or "" ) .. v + end + + return idata .. ( #data > 0 and ", " or "" ) .. data end --- Recursively serialize given data to lua code, suitable for restoring -- 2.11.0