-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializer.lua
More file actions
46 lines (42 loc) · 891 Bytes
/
serializer.lua
File metadata and controls
46 lines (42 loc) · 891 Bytes
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
--By matheus
function serializer(val, carry)
carry = carry or {}
local t = type(val)
if t == "table" then
--avoid double ref
if carry[val] then
return "{}"
end
carry[val] = true
local str = ""
local seq = 1
for i,b in pairs(val) do
local vparse = serializer(b,carry)
if vparse then
local ind = ""
if seq ~= i then
ind = "[\""..tostring(i):gsub('"',"\\\"").."\"]="
end
str = str ..ind..vparse..","
end
seq = seq +1
end
if str:sub(-1,-1) == "," then
str = str:sub(1,-2)
end
return "{"..str.."}"
elseif t == 'userdata' or t == 'function' then
return nil, "[invalid type]"
elseif t == 'string' then
return "\""..val:gsub('"',"\\\"").."\""
end
return tostring(val), true
end
function unserialize(str)
local f, err = loadstring("return "..str)
if not f then
error(err)
return nil
end
return f()
end