타고난 성격 탓으로 화를 자초하는 거야 어쩔 수 없지만 남까지 못살게 할 필요는 없는 법이다. ―루드야드 키플링(인도 태생의 英 작가)
1 개요 #
- 원저자 링크 :
http://www.dekorte.com/Software/Lua/LuaPickle/
- 원본은 4.0기준으로 작성되어있어, 5.0alpha용으로 다소 수정을 했습니다.
2 pickle 소스 #
----------------------------------------------
-- Pickle.lua
-- An table serialization utility for lua
-- Steve Dekorte, http://www.dekorte.com, Apr 2000
-- Freeware
----------------------------------------------
function pickle(t)
return Pickle:clone():pickle_(t)
end
Pickle = {
clone = function (t) local nt={}; for i, v in t do nt[i]=v end return nt end
}
function Pickle:pickle_(root)
if type(root) ~= "table" then
error("can only pickle tables, not ".. type(root).."s")
end
self._tableToRef = {}
self._refToTable = {}
local savecount = 0
self:ref_(root)
local s = ""
while table.getn(self._refToTable) > savecount do
savecount = savecount + 1
local t = self._refToTable[savecount]
s = s.."{\n"
for i, v in t do
s = string.format("%s[%s]=%s,\n", s, self:value_(i), self:value_(v))
end
s = s.."},\n"
end
return string.format("{%s}", s)
end
function Pickle:value_(v)
local vtype = type(v)
if vtype == "string" then return string.format("%q", v)
elseif vtype == "number" then return v
elseif vtype == "table" then return "{"..self:ref_(v).."}"
else --error("pickle a "..type(v).." is not supported")
end
end
function Pickle:ref_(t)
local ref = self._tableToRef[t]
if not ref then
if t == self then error("can't pickle the pickle class") end
table.insert(self._refToTable, t)
ref = table.getn(self._refToTable)
self._tableToRef[t] = ref
end
return ref
end
----------------------------------------------
-- unpickle
----------------------------------------------
function unpickle(s)
if type(s) ~= "string" then
error("can't unpickle a "..type(s)..", only strings")
end
local gentables = loadstring("return "..s)
local tables = gentables()
for tnum = 1, table.getn(tables) do
local t = tables[tnum]
local tcopy = {}; for i, v in t do tcopy[i] = v end
for i, v in tcopy do
local ni, nv
if type(i) == "table" then ni = tables[i[1]] else ni = i end
if type(v) == "table" then nv = tables[v[1]] else nv = v end
t[ni] = nv
end
end
return tables[1]
end
3 테스트 프로그램 #
----------------------------------------------
-- PickleTest.lua
-- Testing code for Pickle.lua
-- Steve Dekorte, http://www.dekorte.com, Apr 2000
----------------------------------------------
dofile("Pickle.lua")
function test()
local t = {
name = "foo",
ssn=123456789,
contact = { phone = "555-1212", email = "foo@foo.com"},
}
t.contact.loop = t
t["a b"] = "zzz"
t[10] = 11
t[t] = 5
local s = pickle(t)
print("pickled string:\n\n"..s)
local ut = unpickle(s)
print("loop test: "); eq(ut == ut.contact.loop)
print("subitem test:"); eq(ut.contact.phone == t.contact.phone)
print("number value:"); eq(ut.ssn == t.ssn)
print("number index:"); eq(ut[10] == 11)
print("table index: "); eq(ut[ut] == 5)
end
function eq(b)
if b then print(" succeeded") else print(" failed") end
end
test()









