在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
第一篇 有点内容的..
示例: 1 local asserteq = require 'pl.test'.asserteq 2 local t1, t2, t3, result 3 4 t1 = {f1 = 1, f2 = true, f3 = 'f3', 'i1', 'i2'} 5 asserteq(deserialize(serialize(t1)),t1) 6 --[[return function(externList) 7 local internList 8 internList = 9 { 10 { 11 "i1", "i2", 12 ["f3"]="f3", ["f2"]=true, ["f1"]=1, 13 }, 14 } 15 16 return internList[1] 17 end]] 18 19 --嵌套优化 20 t1, t2, t3 = {'T1'}, {'T2'}, {'T3'} 21 t1.t2 = t2 22 t2.t3 = t3 23 asserteq(deserialize(serialize(t1)),t1) 24 --[[return function(externList) 25 local internList 26 internList = 27 { 28 { 29 "T1", 30 ["t2"]={ 31 "T2", 32 ["t3"]={ 33 "T3", 34 }, 35 }, 36 }, 37 } 38 39 return internList[1] 40 end]] 41 42 t1, t2, t3 = {'T1'}, {'T2'}, {'T3'} 43 t1.t2 = t2 44 t3.t2 = t2 45 asserteq(deserialize(serialize{t1, t3}),{t1, t3}) 46 --[[return function(externList) 47 local internList 48 internList = 49 { 50 { 51 { 52 "T1", 53 ["t2"]="<internList[2]>", 54 }, { 55 "T3", 56 ["t2"]="<internList[2]>", 57 }, 58 }, { 59 "T2", 60 }, 61 } 62 63 internList[1][1]["t2"]=internList[2] 64 internList[1][2]["t2"]=internList[2] 65 66 return internList[1] 67 end]] 68 69 --对象做键,循环 70 t1, t2, t3 = {'T1'}, {'T2'}, {'T3'} 71 t1[t2] = t2 72 t3[t2] = t2 73 -- asserteq(deserialize(serialize{t1, t3}),{t1, t3}) 74 -- 'asserteq'没法比较,反序列化生成了新的't2'。 75 --[=[print(serialize{t1, t3}) 76 return function(externList) 77 local internList 78 internList = 79 { 80 { 81 "T2", 82 }, { 83 { 84 "T1", 85 --["<internList[1]>"]=nil, 86 }, { 87 "T3", 88 --["<internList[1]>"]=nil, 89 }, 90 }, 91 } 92 93 internList[2][2][internList[1]]=internList[1] 94 internList[2][1][internList[1]]=internList[1] 95 96 return internList[2] 97 end]=] 98 99 --循环 嵌套 100 t1, t2, t3 = {'T1'}, {'T2'}, {'T3'} 101 t1[t2] = t2 102 t2[t3] = t3 103 t3[t1] = t1 104 --[=[print(serialize{t1, t2, t3}) 105 return function(externList) 106 local internList 107 internList = 108 { 109 { 110 "T1", 111 --["<internList[3]>"]=nil, 112 }, { 113 "<internList[1]>", "<internList[3]>", "<internList[4]>", 114 }, { 115 "T2", 116 --["<internList[4]>"]=nil, 117 }, { 118 "T3", 119 --["<internList[1]>"]=nil, 120 }, 121 } 122 internList[3][internList[4]]=internList[4] 123 internList[2][1]=internList[1] 124 internList[2][3]=internList[4] 125 internList[2][2]=internList[3] 126 internList[1][internList[3]]=internList[3] 127 internList[4][internList[1]]=internList[1] 128 129 return internList[2] 130 end 131 ]=] 132 --asserteq(deserialize(serialize{t1, t2, t3}),{t1, t2, t3}) 133 -- 'asserteq'无法处理循环 134 135 --externList 136 t1, t2, t3 = {'T1'}, {'T2'}, {'T3'} 137 t1[t2] = t2 138 t3[t2] = t2 139 asserteq(deserialize(serialize({t1, t3},{t2}),{t2}),{t1, t3}) 140 --[=[return function(externList) 141 local internList 142 internList = 143 { 144 { 145 { 146 "T1", 147 [externList[1]]=externList[1], 148 }, { 149 "T3", 150 [externList[1]]=externList[1], 151 }, 152 }, 153 } 154 155 return internList[1] 156 end]=] 157 158 --metatable 159 setmetatable(t1, t2) 160 asserteq(deserialize(serialize({t1, t3},{t2}),{t2}),{t1, t3}) 161 --[=[return function(externList) 162 local internList 163 internList = 164 { 165 externList[1], { 166 "<internList[3]>", { 167 "T3", 168 [externList[1]]=externList[1], 169 }, 170 }, { 171 "T1", 172 [externList[1]]=externList[1], 173 }, 174 } 175 176 internList[2][1]=internList[3] 177 178 setmetatable(internList[3], externList[1]) 179 180 return internList[2] 181 end]=] 182 183 result = serialize({t1, t3},{t2}) 184 asserteq({string.len(result), string.len(binaryString(result))}, {337, 589}) 185 asserteq(deserialize(binaryString(result),{t2}),{t1, t3}) 186 187 print "test on 'serialize' OK"
代码整理中..
不喜欢这个编辑器..但没空折腾..阻碍发随笔啊.. 其他关键词: Lua 表 存储 保存 字符串 解析 |
请发表评论