01.local objects = {}
02.setmetatable(objects, {__index={["subset"]=function(object, proxies)
03.for _,o in ipairs(proxies) do
04.if object == o then return true end
05.end
06.end}})
07.
08.function _pickle(object, seen, indent)
09.-
10.if not indent then indent = "" end
11.
12.local serialize_key = function(key)
13.if type(key) == "string" then
14.return "[\""..key.."\"]"
15.elseif type(key) == "table" then
16.return "[".._pickle(key):gsub("\n"," ").."]"
17.else
18.return "["..key.."]"
19.end
20.return key
21.end
22.
23.local escape = function(o)
24.return o:gsub("\\","\\\\"):gsub("'","\\'"):gsub('"','\\"')
25.end
26.
27.-
28.if type(object) == "table" then
29.local serialize = "{\n"
30.for key, value in pairs(object) do
31.serialize = serialize .. indent.."\t" .. serialize_key(key) .. " = " ..tostring(_pickle(value, seen, indent.."\t")) .. ",\n"
32.end
33.serialize = serialize .. indent .. "}"
34.
35.return serialize
36.elseif type(object) == "string" then
37.return '"' .. escape(object) .. '"'
38.elseif type(object) == "function" then
39.return "loadstring([["..string.dump(object).."]])"
40.elseif objects.subset(object, {"userdata"}) then
41.return nil
42.end
43.return tostring(object)
44.end
45.
46.pickle = {}
47.
48.function pickle.dumps(object)
49.return "return ".. _pickle(object)
50.end
51.
52.function pickle.dump(object, filename)
53.local dump = pickle.dumps(object)
54.local _file = io.open(filename, "wb")
55._file:write(dump)
56._file:close()
57.return dump
58.end
59.
60.function pickle.loads(object)
61.local fn = loadstring(object)
62.if fn then
63.return fn()
64.end
65.end
66.
67.function pickle.load(filename)
68.local _file = io.open(filename, "rb")
69.local dump = _file:read("*all")
70.local object = pickle.loads(dump)
71._file:close()
72.return object
73.end