1
1
--
2
2
-- json.lua
3
3
--
4
- -- Copyright (c) 2015 rxi
4
+ -- Copyright (c) 2018 rxi
5
5
--
6
- -- This library is free software; you can redistribute it and/or modify it
7
- -- under the terms of the MIT license. See LICENSE for details.
6
+ -- Permission is hereby granted, free of charge, to any person obtaining a copy of
7
+ -- this software and associated documentation files (the "Software"), to deal in
8
+ -- the Software without restriction, including without limitation the rights to
9
+ -- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10
+ -- of the Software, and to permit persons to whom the Software is furnished to do
11
+ -- so, subject to the following conditions:
12
+ --
13
+ -- The above copyright notice and this permission notice shall be included in all
14
+ -- copies or substantial portions of the Software.
15
+ --
16
+ -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ -- SOFTWARE.
8
23
--
9
24
10
25
local json = { _version = " 0.1.0" }
38
53
39
54
local function encode_nil (val )
40
55
return " null"
41
- end
56
+ end
42
57
43
58
44
59
local function encode_table (val , stack )
127
142
128
143
local parse
129
144
130
- local function create_set (...)
145
+ local function create_set (...)
131
146
local res = {}
132
147
for i = 1 , select (" #" , ... ) do
133
148
res [ select (i , ... ) ] = true
@@ -234,17 +249,17 @@ local function parse_string(str, i)
234
249
235
250
elseif x == 34 then -- '"' (end of string)
236
251
local s = str :sub (i + 1 , j - 1 )
237
- if has_surrogate_escape then
252
+ if has_surrogate_escape then
238
253
s = s :gsub (" \\ u[dD][89aAbB]..\\ u...." , parse_unicode_escape )
239
254
end
240
- if has_unicode_escape then
255
+ if has_unicode_escape then
241
256
s = s :gsub (" \\ u...." , parse_unicode_escape )
242
257
end
243
258
if has_escape then
244
259
s = s :gsub (" \\ ." , escape_char_map_inv )
245
260
end
246
261
return s , j + 1
247
-
262
+
248
263
else
249
264
last = x
250
265
end
@@ -282,15 +297,15 @@ local function parse_array(str, i)
282
297
local x
283
298
i = next_char (str , i , space_chars , true )
284
299
-- Empty / end of array?
285
- if str :sub (i , i ) == " ]" then
300
+ if str :sub (i , i ) == " ]" then
286
301
i = i + 1
287
302
break
288
303
end
289
304
-- Read token
290
305
x , i = parse (str , i )
291
306
res [n ] = x
292
307
n = n + 1
293
- -- Next token
308
+ -- Next token
294
309
i = next_char (str , i , space_chars , true )
295
310
local chr = str :sub (i , i )
296
311
i = i + 1
@@ -308,7 +323,7 @@ local function parse_object(str, i)
308
323
local key , val
309
324
i = next_char (str , i , space_chars , true )
310
325
-- Empty / end of object?
311
- if str :sub (i , i ) == " }" then
326
+ if str :sub (i , i ) == " }" then
312
327
i = i + 1
313
328
break
314
329
end
0 commit comments