--序列化成字符串
function table_serialize(tbl, visited)
visited = visited or {}
local con = "{"
for k, v in pairs(tbl) do
-- if not visited[k] or visited[k] ~= v then
visited[k] = v
if type(k) == "table" then
con = con.."["..table_serialize(k, visited).."] = "
elseif type(k) == "number" then
con = con.."["..tostring(k).."] = "
else
con = con..tostring(k).." = "
end
if type(v) == "table" then
con = con..table_serialize(v, visited)
else
con = con .. tostring(v)
end
con = con .. ", "
-- end
end
con = con .. "}"
return con
end
--表的deepCopy
function DeepCopy( obj )
local InTable = {};
local function Func(obj)
if type(obj) ~= "table" then --判斷表中是否有表
return obj;
end
local NewTable = {}; --定義一個(gè)新表
InTable[obj] = NewTable; --若表中有表,則先把表給InTable,再用NewTable去接收內(nèi)嵌的表
for k,v in pairs(obj) do --把舊表的key和Value賦給新表
NewTable[Func(k)] = Func(v);
end
return setmetatable(NewTable, getmetatable(obj))--賦值元表
end
return Func(obj) --若表中有表汞斧,則把內(nèi)嵌的表也復(fù)制了
end