forked from Middlerun/lovecraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TSerial.lua
35 lines (33 loc) · 1.22 KB
/
TSerial.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
-- TSerial v1.2, a simple table serializer which turns tables into Lua script
-- by Taehl (SelfMadeSpirit@gmail.com)
-- Usage: table = TSerial.unpack( TSerial.pack(table) )
TSerial = {}
function TSerial.pack(t)
assert(type(t) == "table", "Can only TSerial.pack tables.")
if not t then return nil end
local s = "{"
for k, v in pairs(t) do
local tk, tv = type(k), type(v)
if tk == "boolean" then k = k and "[true]" or "[false]"
elseif tk == "number" then k = "["..k.."]"
elseif tk == "string" then -- no transform needed
elseif tk == "table" then k = "["..TSerial.pack(k).."]"
else error("Attempted to Tserialize a table with an invalid key: "..tostring(k))
end
if tv == "boolean" then v = v and "true" or "false"
elseif tv == "number" then -- no transform needed
elseif tv == "string" then v = string.format("%q", v)
elseif tv == "table" then v = TSerial.pack(v)
else error("Attempted to Tserialize a table with an invalid value: "..tostring(v))
end
s = s..k.."="..v..","
end
return s.."}"
end
function TSerial.unpack(s)
assert(type(s) == "string", "Can only TSerial.unpack strings.")
loadstring("TSerial.table="..s)()
local t = TSerial.table
TSerial.table = nil
return t
end