forked from chipsams/rack-thing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialize.lua
72 lines (60 loc) · 1.98 KB
/
serialize.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
local function serialize_value(__s,__t,v)
print(v,__s[v])
if type(v) == "table" then
if __s[v] then return "__s["..__s[v].."]",__s[v] end
if getmetatable(v) then
print("metatable")
table.insert(__s,'"[placeholder'..(#__s+1)..']"')
local index=#__s
__s[v]=index
local text="setmetatable({"
for k,value in pairs(v) do
if type(v)=="table" then
local table_index=serialize_value(__s,__t,value)
table.insert(__t,{k=serialize_value(__s,__t,k),i=index,ti=table_index})
text=text..("["..serialize_value(__s,__t,k).."]".."= '[placeholder"..table_index.."]',") --trailing , are fine in lua.
end
end
text=text.."},"..serialize_value(__s,__t,getmetatable(v))..")"
__s[index]=text
return "__s["..__s[v].."]",index
else
table.insert(__s,'"[placeholder'..(#__s+1)..']"')
local index=#__s
__s[v]=index
local text="{"
for k,value in pairs(v) do
if type(value)=="table" then
local table_index=serialize_value(__s,__t,value)
table.insert(__t,{k=serialize_value(__s,__t,k),i=index,ti=table_index})
else
text=text..("["..serialize_value(__s,__t,k).."]".."="..serialize_value(__s,__t,value)).."," --trailing , are fine in lua.
end
end
text=text.."}"
__s[index]=text
return "__s["..__s[v].."]",index
end
elseif type(v)=="number" then
return tostring(v)
elseif type(v)=="boolean" then
return v and "true" or "false"
elseif type(v)=="string" then
return string.format("%q",v)
elseif type(v)=="function" then
return "load("..string.format("%q",string.dump(v))..")"
else
return "\"unfinished ("..type(v)..")\""
end
end
local function serialize(v)
print(v)
local __s={}
local __t={}
local text=serialize_value(__s,__t,v)
local link_text=table.concat(__t)
text="__s={"..table.concat(__s,",").."}\nreturn "..text
print(text)
return text
end
return serialize