diff options
author | Jo-Philipp Wich <jow@openwrt.org> | 2008-08-28 16:40:51 +0000 |
---|---|---|
committer | Jo-Philipp Wich <jow@openwrt.org> | 2008-08-28 16:40:51 +0000 |
commit | 33ef3a1da2533be6a0ce1d425d80a7b911c406d9 (patch) | |
tree | 524bff78b68a0720bc86d48f9bdbb04faee3643d | |
parent | 2975bb66a61e2f5c86c8ee9701830e40d8c1ab82 (diff) |
* luci/libs: util: further enhancements to table serialisation
-rw-r--r-- | libs/core/luasrc/util.lua | 21 |
1 files 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 |