table.tostring = function(t)
local mark={}
local assign={}
local function ser_table(tbl,parent)
mark[tbl]=parent
local tmp={}
for k,v in pairs(tbl) do
local key= type(k)=="number" and "["..k.."]" or "[".. string.format("%q", k) .."]"
if type(v)=="table" then
local dotkey= parent.. key
if mark[v] then
table.insert(assign,dotkey.."='"..mark[v] .."'")
else
table.insert(tmp, key.."="..ser_table(v,dotkey))
end
elseif type(v) == "string" then
table.insert(tmp, key.."=".. string.format('%q', v))
elseif type(v) == "number" or type(v) == "boolean" then
table.insert(tmp, key.."=".. tostring(v))
end
end
return "{"..table.concat(tmp,",").."}"
end
return "do local ret="..ser_table(t,"ret")..table.concat(assign," ").." return ret end"
end
table.loadstring = function(strData)
local f = loadstring(strData)
if f then
return f()
end
endlua Table的序列化与反序列化函数
最新推荐文章于 2024-03-16 09:42:48 发布
本文深入探讨了表转换函数与字符串操作的具体实现,包括如何使用表加载字符串、表到字符串转换以及序列化表内容的方法。文章详细介绍了代码逻辑、函数调用流程以及在实际应用中的潜在用途。
1259

被折叠的 条评论
为什么被折叠?



