第一篇 有点内容的..
支持 表/函数/数值/布尔/字符串 做 键、值
支持 循环/嵌套
支持 元表(支持弱表分析)
支持 表被任意数量、位置引用
支持 扩展引用外部
支持 格式化输出
支持 嵌套优化(分析依赖情况)
示例:
local asserteq = require 'pl.test'.asserteq
local t1, t2, t3, result t1 = {f1 = , f2 = true, f3 = 'f3', 'i1', 'i2'}
asserteq(deserialize(serialize(t1)),t1)
--[[return function(externList)
local internList
internList =
{
{
"i1", "i2",
["f3"]="f3", ["f2"]=true, ["f1"]=1,
},
} return internList[1]
end]] --嵌套优化
t1, t2, t3 = {'T1'}, {'T2'}, {'T3'}
t1.t2 = t2
t2.t3 = t3
asserteq(deserialize(serialize(t1)),t1)
--[[return function(externList)
local internList
internList =
{
{
"T1",
["t2"]={
"T2",
["t3"]={
"T3",
},
},
},
} return internList[1]
end]] t1, t2, t3 = {'T1'}, {'T2'}, {'T3'}
t1.t2 = t2
t3.t2 = t2
asserteq(deserialize(serialize{t1, t3}),{t1, t3})
--[[return function(externList)
local internList
internList =
{
{
{
"T1",
["t2"]="<internList[2]>",
}, {
"T3",
["t2"]="<internList[2]>",
},
}, {
"T2",
},
} internList[1][1]["t2"]=internList[2]
internList[1][2]["t2"]=internList[2] return internList[1]
end]] --对象做键,循环
t1, t2, t3 = {'T1'}, {'T2'}, {'T3'}
t1[t2] = t2
t3[t2] = t2
-- asserteq(deserialize(serialize{t1, t3}),{t1, t3})
-- 'asserteq'没法比较,反序列化生成了新的't2'。
--[=[print(serialize{t1, t3})
return function(externList)
local internList
internList =
{
{
"T2",
}, {
{
"T1",
--["<internList[1]>"]=nil,
}, {
"T3",
--["<internList[1]>"]=nil,
},
},
} internList[2][2][internList[1]]=internList[1]
internList[2][1][internList[1]]=internList[1] return internList[2]
end]=] --循环 嵌套
t1, t2, t3 = {'T1'}, {'T2'}, {'T3'}
t1[t2] = t2
t2[t3] = t3
t3[t1] = t1
--[=[print(serialize{t1, t2, t3})
return function(externList)
local internList
internList =
{
{
"T1",
--["<internList[3]>"]=nil,
}, {
"<internList[1]>", "<internList[3]>", "<internList[4]>",
}, {
"T2",
--["<internList[4]>"]=nil,
}, {
"T3",
--["<internList[1]>"]=nil,
},
}
internList[3][internList[4]]=internList[4]
internList[2][1]=internList[1]
internList[2][3]=internList[4]
internList[2][2]=internList[3]
internList[1][internList[3]]=internList[3]
internList[4][internList[1]]=internList[1] return internList[2]
end
]=]
--asserteq(deserialize(serialize{t1, t2, t3}),{t1, t2, t3})
-- 'asserteq'无法处理循环 --externList
t1, t2, t3 = {'T1'}, {'T2'}, {'T3'}
t1[t2] = t2
t3[t2] = t2
asserteq(deserialize(serialize({t1, t3},{t2}),{t2}),{t1, t3})
--[=[return function(externList)
local internList
internList =
{
{
{
"T1",
[externList[1]]=externList[1],
}, {
"T3",
[externList[1]]=externList[1],
},
},
} return internList[1]
end]=] --metatable
setmetatable(t1, t2)
asserteq(deserialize(serialize({t1, t3},{t2}),{t2}),{t1, t3})
--[=[return function(externList)
local internList
internList =
{
externList[1], {
"<internList[3]>", {
"T3",
[externList[1]]=externList[1],
},
}, {
"T1",
[externList[1]]=externList[1],
},
} internList[2][1]=internList[3] setmetatable(internList[3], externList[1]) return internList[2]
end]=] result = serialize({t1, t3},{t2})
asserteq({string.len(result), string.len(binaryString(result))}, {, })
asserteq(deserialize(binaryString(result),{t2}),{t1, t3}) print "test on 'serialize' OK"
代码整理中..
不喜欢这个编辑器..但没空折腾..阻碍发随笔啊..
其他关键词: Lua 表 存储 保存 字符串 解析