-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjson.lua
472 lines (366 loc) · 12.5 KB
/
json.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
--[[
The MIT License (MIT)
Copyright (c) 2014 Ted Dobyns
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
local tokens = {}
tokens.quote = '\"'
tokens.comma = ','
tokens.colon = ':'
tokens.leftBracket = '['
tokens.rightBracket = ']'
tokens.leftBrace = '{'
tokens.rightBrace = '}'
local types = {
none = 0,
syntax = 1,
number = 2,
string = 3,
boolean = 4
}
local table_types = {
array = 0,
object = 1
}
local function gen_error(err, index)
return string.format("%s at index %d.", err, index)
end
local function toboolean(v)
return (type(v) == "string" and v == "true") or
(type(v) == "number" and v ~= 0) or
(type(v) == "boolean" and v)
end
local function is_whitespace(str)
local c = 0
for w in str:gmatch("%S+") do c = c + 1 end
return c == 0
end
local function is_token(str)
for k, v in pairs(tokens) do
if str == v then
return true, v
end
end
return false, nil
end
local function is_char(str)
local s = str:lower()
local alphabet = "abcdefghijklmnopqrstuvwxyz"
return alphabet:find(s) ~= nil
end
local function is_num(str)
local num_chars = "0123456789-."
return num_chars:find(str) ~= nil
end
local function is_valid_string_char(str)
return str ~= tokens.quote and str ~= tokens.comma and not is_whitespace(str)
end
local function next_token(str, index, forceStr)
local len = string.len(str)
local frontIndex = index
local backIndex = index
local is_char_func = is_char
if forceStr then
is_char_func = is_valid_string_char
end
while frontIndex <= len do
local front = str:sub(frontIndex, backIndex)
if is_token(front) then
return { value = front, __type = types.syntax }, frontIndex + 1
elseif is_char_func(front) then
while backIndex <= len and
is_valid_string_char(str:sub(backIndex, backIndex)) do
backIndex = backIndex + 1
end
backIndex = backIndex - 1
local token = str:sub(frontIndex, backIndex)
local tokenType = types.string
if token == "true" or token == "false" then
token = toboolean(token)
tokenType = types.boolean
end
return { value = token, __type = tokenType }, backIndex + 1
elseif is_num(front) then
while backIndex <= len and is_num(str:sub(backIndex, backIndex)) do
backIndex = backIndex + 1
end
backIndex = backIndex - 1
local token = tonumber(str:sub(frontIndex, backIndex))
return { value = token, __type = types.number }, backIndex + 1
elseif is_whitespace(front) then
frontIndex = frontIndex + 1
backIndex = backIndex + 1
else
return nil, nil, gen_error("Unexpected character", front)
end
end
return nil, frontIndex
end
function parse_string(str, index)
local lquote, index, err = next_token(str, index)
if err ~= nil then return nil, nil, err end
if lquote.value ~= tokens.quote then
return nil, nil, gen_error("Expected opening quote", index)
end
local strToken, index, err = next_token(str, index, true)
if err ~= nil then return nil, nil, err end
if strToken.__type ~= types.string then
return nil, nil, gen_error("Expected string", index)
end
local rquote, index, err = next_token(str, index)
if err ~= nil then return nil, nil, err end
if rquote.value ~= tokens.quote then
return nil, nil, gen_error("Expected closing quote", index)
end
return strToken.value, index
end
function parse_value(str, index)
local token, i, err = next_token(str, index)
if err ~= nil then return nil, nil, err end
if token.__type == types.syntax then
if token.value == tokens.quote then
token, i, err = parse_string(str, index)
if err ~= nil then return nil, nil, err end
else
return nil, nil, gen_error("Not a valid value token", index)
end
end
return token.value, i
end
function parse_array(str, index)
local result = {}
local foundComma = true
while true do
local token, i, err = next_token(str, index)
if err ~= nil then return nil, nil, err end
if token.value == tokens.comma then
index = i
foundComma = true
elseif token.value == tokens.rightBracket then
-- REMOVE THIS TO IGNORE TRALING COMMAS
if foundComma then
return nil, nil, gen_error("Trailing comma", index)
end
index = i
return result, i
else
if not foundComma then
return nil, nil, gen_error("Expected comma", index)
end
foundComma = false
local next, i, err = next_token(str, index)
local value = nil
if next.__type == types.syntax then
if next.value == tokens.leftBracket then
value, index, err = parse_array(str, i)
elseif next.value == tokens.leftBrace then
value, index, err = parse_object(str, i)
else
return nil, nil, gen_error("Unexpected symbol", index)
end
else
value, index, err = parse_value(str, index)
end
if err ~= nil then return nil, nil, err end
table.insert(result, value)
end
end
end
function parse_object(str, index)
local result = {}
local foundComma = true
while true do
local token, i, err = next_token(str, index)
if err ~= nil then return nil, nil, err end
deep_log(token)
if token.value == tokens.comma then
index = i
foundComma = true
elseif token.value == tokens.rightBrace then
-- REMOVE THIS TO IGNORE TRALING COMMAS
if foundComma then
return nil, nil, gen_error("Trailing comma", index)
end
index = i
return result, i
else
if not foundComma then
return nil, nil, gen_error("Expected comma", index)
end
foundComma = false
local key = nil
key, index, err = parse_string(str, index)
if err ~= nil then return nil, nil, err end
local colon = nil
colon, index, err = next_token(str, index)
if err ~= nil then return nil, nil, err end
if colon.value ~= tokens.colon then
return nil, nil, gen_error("Expected colon", index)
end
local next, i, err = next_token(str, index)
local value = nil
if next.__type == types.syntax then
if next.value == tokens.leftBracket then
value, index, err = parse_array(str, i)
elseif next.value == tokens.leftBrace then
value, index, err = parse_object(str, i)
elseif next.value == tokens.quote then
value, index, err = parse_string(str, index)
else
return nil, nil, gen_error("Unexpected symbol "..next.value, index)
end
else
value, index, err = parse_value(str, index)
end
if err ~= nil then return nil, nil, err end
result[key] = value
end
end
end
local function count_table_items(table)
local count = 0
for _ in pairs(table) do count = count + 1 end
return count
end
local function encode_spaces(output, count, minify)
if minify then return output end
for i = 1, count do
output = output .. ' '
end
return output
end
local function encode_return(output, minify, tabIndex)
if minify then return output end
output = output .. '\n'
output = encode_spaces(output, tabIndex * 4, minify)
return output
end
local function encode_number(output, number, minify, tabIndex)
output = output .. tostring(number)
return output
end
local function encode_boolean(output, boolean, minify, tabIndex)
output = output .. tostring(boolean)
return output
end
local function encode_string(output, string, minify, tabIndex)
output = output .. '\"' .. string .. '\"'
return output
end
local function encode_keyvalue(output, key, value, minify, tabIndex)
output = encode_string(output, key, minify, tabIndex)
output = encode_spaces(output, 1, minify)
output = output .. ':'
output = encode_spaces(output, 1, minify)
output = encode_value(output, value, minify, tabIndex)
return output
end
local function encode_object(output, object, minify, tabIndex)
output = output .. '{'
tabIndex = tabIndex + 1
output = encode_return(output, minify, tabIndex)
local count = count_table_items(object)
local encoded = 0
for k, value in pairs(object) do
output = encode_keyvalue(output, k, value, minify, tabIndex)
encoded = encoded + 1
if encoded < count then
output = output .. ','
else
tabIndex = tabIndex - 1
end
output = encode_return(output, minify, tabIndex)
end
output = output .. '}'
return output
end
local function encode_array(output, array, minify, tabIndex)
output = output .. '['
tabIndex = tabIndex + 1
output = encode_return(output, minify, tabIndex)
for i, value in ipairs(array) do
output = encode_value(output, value, minify, tabIndex)
if i < #array then
output = output .. ','
else
tabIndex = tabIndex - 1
end
output = encode_return(output, minify, tabIndex)
end
output = output .. ']'
return output
end
local function table_type(table)
if #table > 0 then
return table_types.array
else
return table_types.object
end
end
local function encode_table(output, table, minify, tabIndex)
if table_type(table) == table_types.array then
output = encode_array(output, table, minify, tabIndex)
else
output = encode_object(output, table, minify, tabIndex)
end
return output
end
function encode_value(output, value, minify, tabIndex)
local t = type(value)
if t == "table" then
output = encode_table(output, value, minify, tabIndex)
elseif t == "number" then
output = encode_number(output, value, minify, tabIndex)
elseif t == "string" then
output = encode_string(output, value, minify, tabIndex)
elseif t == "boolean" then
output = encode_boolean(output, value, minify, tabIndex)
end
return output
end
local function decode(jsonstr)
token, index, err = next_token(jsonstr, 1)
if err ~= nil then
return nil, err
end
result = nil
if token.__type == types.syntax then
if token.value == tokens.leftBrace then
result, index, err = parse_object(jsonstr, index)
elseif token.value == tokens.leftBracket then
result, index, err = parse_array(jsonstr, index)
end
else
return nil, gen_error("Not a valid JSON object.")
end
if err ~= nil then
return nil, err
end
return result, nil
end
local function encode(table, minify)
assert(type(table) == "table", "JSON encoding requires a table.")
local output = ""
output = encode_table(output, table, minify, 0)
return output
end
return {
encode = encode,
decode = decode,
-- aliases
parse = decode
}