Repository navigation
Expand file tree
/
Copy pathserializer.lua
More file actions
87 lines (80 loc) · 2.3 KB
/
Copy pathserializer.lua
File metadata and controls
87 lines (80 loc) · 2.3 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
local concat = table.concat
local sFormat=string.format
local function internalSerialize(table, tC, t)
t[tC] = "{"
tC = tC + 1
if #table == 0 then
local hasValue = false
for key, value in pairs(table) do
hasValue = true
local keyType = type(key)
if keyType == "string" then
t[tC] = sFormat("[%q]=", key)
elseif keyType == "number" then
t[tC] = "[" .. key .. "]="
elseif keyType == "boolean" then
t[tC] = "[" .. tostring(key) .. "]="
else
t[tC] = "notsupported="
end
tC = tC + 1
local check = type(value)
if check == "table" then
tC = internalSerialize(value, tC, t)
elseif check == "string" then
t[tC] = sFormat("%q", value)
elseif check == "number" then
t[tC] = value
elseif check == "boolean" then
t[tC] = tostring(value)
else
t[tC] = '"Not Supported"'
end
t[tC + 1] = ","
tC = tC + 2
end
if hasValue then
tC = tC - 1
end
else
for i = 1, #table do
local value = table[i]
local check = type(value)
if check == "table" then
tC = internalSerialize(value, tC, t)
elseif check == "string" then
t[tC] = sFormat("%q", value)
elseif check == "number" then
t[tC] = value
elseif check == "boolean" then
t[tC] = tostring(value)
else
t[tC] = '"Not Supported"'
end
t[tC + 1] = ","
tC = tC + 2
end
tC = tC - 1
end
t[tC] = "}"
return tC
end
function serialize(value)
local t = {}
local check = type(value)
if check == "table" then
internalSerialize(value, 1, t)
elseif check == "string" then
return sFormat("%q", value)
elseif check == "number" then
return value
elseif check == "boolean" then
return tostring(value)
else
return '"Not Supported"'
end
return concat(t)
end
function deserialize(s)
return load("return " .. s)()
end