先上代码
function luautil.serialize(t, sort_parent, sort_child)
local mark={}
local assign={}
local function ser_table(tbl,parent)
mark[tbl]=parent
local tmp={}
local sortList = {};
for k,v in pairs(tbl) do
sortList[#sortList + 1] = {key=k, value=v};
end
if tostring(parent) == "ret" then
if sort_parent then table.sort(sortList, sort_parent); end
else
if sort_child then table.sort(sortList, sort_child); end
end
for i = 1, #sortList do
local info = sortList[i];
local k = info.key;
local v = info.value;
local key= type(k)=="number" and "["..k.."]" or k;
if type(v)=="table" then
local dotkey= parent..(type(k)=="number" and key or "."..key)
if mark[v] then
table.insert(assign,dotkey.."="..mark[v])
else
table.insert(t