Repository navigation
Expand file tree
/
Copy pathjson.lua
More file actions
139 lines (130 loc) · 3.07 KB
/
Copy pathjson.lua
File metadata and controls
139 lines (130 loc) · 3.07 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
-- Minimal JSON decoder (no dependencies, so it works without lualibs / texlive-luatex).
-- json.decode(text) -> Lua value. Arrays are 1..n tables, objects are string-keyed tables.
local json = {}
local escapes =
{ ['"'] = '"', ["\\"] = "\\", ["/"] = "/", b = "\b", f = "\f", n = "\n", r = "\r", t = "\t" }
local function skip(s, i)
return s:find("[^ \t\r\n]", i) or #s + 1
end
local parse_value
local function parse_string(s, i)
local out, j = {}, i + 1
while true do
local c = s:sub(j, j)
if c == "" then
error("json: unterminated string")
end
if c == '"' then
return table.concat(out), j + 1
end
if c == "\\" then
local e = s:sub(j + 1, j + 1)
if e == "u" then
local cp = tonumber(s:sub(j + 2, j + 5), 16)
j = j + 6
if cp >= 0xD800 and cp < 0xDC00 and s:sub(j, j + 1) == "\\u" then
local lo = tonumber(s:sub(j + 2, j + 5), 16)
cp = 0x10000 + (cp - 0xD800) * 0x400 + (lo - 0xDC00)
j = j + 6
end
out[#out + 1] = utf8.char(cp)
else
out[#out + 1] = escapes[e] or error("json: bad escape \\" .. e)
j = j + 2
end
else
local k = s:find('["\\]', j) or #s + 1
out[#out + 1] = s:sub(j, k - 1)
j = k
end
end
end
local function parse_array(s, i)
local arr, n = {}, 0
i = skip(s, i + 1)
if s:sub(i, i) == "]" then
return arr, i + 1
end
while true do
local v
v, i = parse_value(s, i)
n = n + 1
arr[n] = v
i = skip(s, i)
local c = s:sub(i, i)
if c == "]" then
return arr, i + 1
end
if c ~= "," then
error("json: expected , or ] at " .. i)
end
i = i + 1
end
end
local function parse_object(s, i)
local obj = {}
i = skip(s, i + 1)
if s:sub(i, i) == "}" then
return obj, i + 1
end
while true do
i = skip(s, i)
if s:sub(i, i) ~= '"' then
error("json: expected string key at " .. i)
end
local k
k, i = parse_string(s, i)
i = skip(s, i)
if s:sub(i, i) ~= ":" then
error("json: expected : at " .. i)
end
local v
v, i = parse_value(s, i + 1)
obj[k] = v
i = skip(s, i)
local c = s:sub(i, i)
if c == "}" then
return obj, i + 1
end
if c ~= "," then
error("json: expected , or } at " .. i)
end
i = i + 1
end
end
function parse_value(s, i)
i = skip(s, i)
local c = s:sub(i, i)
if c == "{" then
return parse_object(s, i)
end
if c == "[" then
return parse_array(s, i)
end
if c == '"' then
return parse_string(s, i)
end
if s:sub(i, i + 3) == "true" then
return true, i + 4
end
if s:sub(i, i + 4) == "false" then
return false, i + 5
end
if s:sub(i, i + 3) == "null" then
return nil, i + 4
end
local num = s:match("^-?%d+%.?%d*[eE]?[+-]?%d*", i)
if num and #num > 0 then
return tonumber(num), i + #num
end
error("json: unexpected character '" .. c .. "' at " .. i)
end
function json.decode(s)
local v, i = parse_value(s, 1)
i = skip(s, i)
if i <= #s then
error("json: trailing data at " .. i)
end
return v
end
return json