Lua Table的序列化与反序列化函数
| [日期:2012-02-18] |
来源:Linux社区
作者:chencong112 |
[字体:大 中 小]
|
Lua Table的序列化与反序列化函数
- 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
- end
更多关于Lua的详细信息,或者下载地址请点这里 
评论声明
- 尊重网上道德,遵守中华人民共和国的各项有关法律法规
- 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
- 本站管理人员有权保留或删除其管辖留言中的任意内容
- 本站有权在网站内转载或引用您的评论
- 参与本评论即表明您已经阅读并接受上述条款
|
|
|
|