-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializer.lua
More file actions
247 lines (232 loc) · 6.16 KB
/
serializer.lua
File metadata and controls
247 lines (232 loc) · 6.16 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
local os = require("os")
local function a(t,...)
local arg = {...}
for i=1,#arg do
table.insert(t, arg[i])
end
end
local function f(file,...)
file:write(...)
end
local function serializeToFileRec(file, obj, indent, asArray)
indent = indent or ''
if asArray then
for _,v in ipairs(obj) do
local t = type(v)
if t == "table" then
-- what kind of table, one like a list or a like a map?
if v[1] == nil then
-- map
f(file, indent, t, "={\n")
serializeToFileRec(file, v, indent .. ' ')
f(file, indent, "}\n")
else
-- list
f(file, indent, "list=", "[\n")
serializeToFileRec(file, v, indent .. ' ', true)
f(file, indent, "]\n")
end
elseif t == "boolean" then
f(file, indent, t, "=")
if v then
f(file, "true\n")
else
f(file, "false\n")
end
else
f(file, indent, t, "=", v, "\n")
end
end
else
for k,v in pairs(obj) do
if string.sub(k,1,1) ~= "_" then
local t = type(v)
if t == "table" then
-- what kind of table, one like a list or a like a map?
if v[1] == nil then
-- map
f(file, indent, k, ":", t, "={\n")
serializeToFileRec(file, v, indent .. ' ')
f(file, indent, "}\n")
else
-- list
f(file, indent, k, ":list=[\n")
serializeToFileRec(file, v, indent .. ' ', true)
f(file, indent, "]\n")
end
elseif t == "boolean" then
f(file, indent, k, ":", t, "=")
if v then
f(file, "true\n")
else
f(file, "false\n")
end
else
f(file, indent, k, ":", t, "=", v, "\n")
end
end
end
end
return true
end
local function serializeToFile(filePath, obj)
local file = io.open(filePath, "w")
if not file then
return false
end
serializeToFileRec(file, obj)
file:flush()
file:close()
return true
end
local function serialize(obj, indent, asArray, toTable)
local s = toTable or {}
indent = indent or ''
if asArray then
for _,v in ipairs(obj) do
local t = type(v)
if t == "table" then
-- what kind of table, one like a list or a like a map?
if v[1] == nil then
-- map
a(s, indent, t, "={\n")
serialize(v, indent .. ' ', nil, s)
a(s, indent, "}\n")
else
-- list
a(s, indent, "list=", "[\n")
serialize(v, indent .. ' ', true, s)
a(s, indent, "]\n")
end
elseif t == "boolean" then
a(s, indent, t, "=")
if v then
a(s, "true\n")
else
a(s, "false\n")
end
else
a(s, indent, t, "=", v, "\n")
end
end
else
for k,v in pairs(obj) do
if string.sub(k,1,1) ~= "_" then
local t = type(v)
if t == "table" then
-- what kind of table, one like a list or a like a map?
if v[1] == nil then
-- map
a(s, indent, k, ":", t, "={\n")
serialize(v, indent .. ' ', nil, s)
a(s, indent, "}\n")
else
-- list
a(s, indent, k, ":list=[\n")
serialize(v, indent .. ' ', true, s)
a(s, indent, "]\n")
end
elseif t == "boolean" then
a(s, indent, k, ":", t, "=")
if v then
a(s, "true\n")
else
a(s, "false\n")
end
else
a(s, indent, k, ":", t, "=", v, "\n")
end
end
end
end
if toTable then
return
else
return table.concat(s, "")
end
end
local function deserializeFromLinesRec(lines, asArray, untilClose, readCount)
local result = {}
readCount = readCount or { c = 0 }
local line
if type(lines) == "table" then
line = table.remove(lines, 1)
else
line = lines()
readCount.c = readCount.c + 1
if readCount.c % 1000 == 0 then
-- this is taking a while, we need to yield so OC doesn't kill us!
if os.sleep then
os.sleep(0)
end
end
end
local isDone = false
while not isDone and line ~= nil and (not untilClose or line ~= untilClose) do
if string.len(line) > 0 then
local indent, k, t, v
if asArray then
indent, t, v = string.match(line, '(%s*)([^=]+)=(.*)')
else
indent, k, t, v = string.match(line, '(%s*)([^%s]+):(.+)=(.*)')
end
if t == "string" then
v = v
elseif t == "number" then
v = tonumber(v)
elseif t == "boolean" then
v = v == "true"
elseif t == "table" then
-- find all the lines containing this nested table
v, isDone = deserializeFromLinesRec(lines, false, indent .. "}", readCount)
elseif t == "list" then
-- find all the lines containing this nested list
v, isDone = deserializeFromLinesRec(lines, true, indent .. "]", readCount)
end
if asArray then result[#result+1] = v else result[k] = v end
end
if not isDone then
if type(lines) == "table" then
line = table.remove(lines, 1)
else
line = lines()
readCount.c = readCount.c + 1
if readCount.c % 500 == 0 then
-- this is taking a while, we need to yield so OC doesn't kill us!
if os.sleep then
os.sleep(0)
end
end
end
end
end
return result, isDone or line == nil
end
local function deserializeFromLines(lines)
return deserializeFromLinesRec(lines)
end
local function deserialize(str)
local lines = {}
for line in string.gmatch(str, "([^\n]+)") do
lines[#lines + 1] = line
end
return deserializeFromLines(lines, false)
end
local function clone(o)
return deserialize(serialize(o))
end
local function deserializeFile(path)
local l = io.lines(path)
if not l then
return nil, "could not open file"
end
return deserializeFromLines(l)
end
return {
serialize = serialize,
serializeToFile = serializeToFile,
deserialize = deserialize,
deserializeLines = deserializeFromLines,
deserializeFile = deserializeFile,
clone = clone
}