Skip to content

Commit b618da3

Browse files
committed
改进几个错误提示
1 parent 9344627 commit b618da3

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

json-beautify.lua

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,26 @@ function encode_map.table(t)
6868
end
6969
statusVisited[t] = true
7070
if type(first_val) == "string" then
71-
local key = {}
71+
local keys = {}
7272
for k in next, t do
7373
if type(k) ~= "string" then
74-
error("invalid table: mixed or invalid key types: "..k)
74+
error("invalid table: mixed or invalid key types: "..tostring(k))
7575
end
76-
key[#key+1] = k
76+
keys[#keys+1] = k
7777
end
78-
table_sort(key)
78+
table_sort(keys)
7979
statusBuilder[#statusBuilder+1] = "{"
8080
statusDep = statusDep + 1
8181
encode_newline()
8282
do
83-
local k = key[1]
83+
local k = keys[1]
8484
statusBuilder[#statusBuilder+1] = '"'
8585
statusBuilder[#statusBuilder+1] = encode_string(k)
8686
statusBuilder[#statusBuilder+1] = '": '
8787
encode(t[k])
8888
end
89-
for i = 2, #key do
90-
local k = key[i]
89+
for i = 2, #keys do
90+
local k = keys[i]
9191
statusBuilder[#statusBuilder+1] = ","
9292
encode_newline()
9393
statusBuilder[#statusBuilder+1] = '"'
@@ -103,7 +103,7 @@ function encode_map.table(t)
103103
local max = 0
104104
for k in next, t do
105105
if math_type(k) ~= "integer" or k <= 0 then
106-
error("invalid table: mixed or invalid key types: "..k)
106+
error("invalid table: mixed or invalid key types: "..tostring(k))
107107
end
108108
if max < k then
109109
max = k
@@ -126,6 +126,11 @@ function encode_map.table(t)
126126
if t[1] == nil then
127127
error("invalid table: sparse array is not supported")
128128
end
129+
---@diagnostic disable-next-line: undefined-global
130+
if jit and t[0] ~= nil then
131+
-- 0 is the first index in luajit
132+
error("invalid table: mixed or invalid key types: "..0)
133+
end
129134
statusBuilder[#statusBuilder+1] = "["
130135
statusDep = statusDep + 1
131136
encode_newline()
@@ -142,7 +147,7 @@ function encode_map.table(t)
142147
if type(k) == "number" then
143148
error("invalid table: sparse array is not supported")
144149
else
145-
error("invalid table: mixed or invalid key types: "..k)
150+
error("invalid table: mixed or invalid key types: "..tostring(k))
146151
end
147152
end
148153
statusDep = statusDep - 1

json.lua

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,10 @@ if string_match(tostring(1 / 2), "%p") == "," then
145145
end
146146

147147
function encode_map.number(v)
148-
if v ~= v or v <= tiny or v >= huge then
149-
error("unexpected number value '"..tostring(v).."'")
148+
if v ~= v then
149+
error("NaN is not supported in JSON")
150+
elseif v <= tiny or v >= huge then
151+
error("Inf is not supported in JSON")
150152
end
151153
if math_type(v) == "integer" then
152154
return string_format("%d", v)
@@ -179,7 +181,7 @@ function encode_map.table(t)
179181
local keys = {}
180182
for k in next, t do
181183
if type(k) ~= "string" then
182-
error("invalid table: mixed or invalid key types: "..k)
184+
error("invalid table: mixed or invalid key types: "..tostring(k))
183185
end
184186
keys[#keys+1] = k
185187
end
@@ -204,7 +206,7 @@ function encode_map.table(t)
204206
local max = 0
205207
for k in next, t do
206208
if math_type(k) ~= "integer" or k <= 0 then
207-
error("invalid table: mixed or invalid key types: "..k)
209+
error("invalid table: mixed or invalid key types: "..tostring(k))
208210
end
209211
if max < k then
210212
max = k
@@ -240,7 +242,7 @@ function encode_map.table(t)
240242
if type(k) == "number" then
241243
error("invalid table: sparse array is not supported")
242244
else
243-
error("invalid table: mixed or invalid key types: "..k)
245+
error("invalid table: mixed or invalid key types: "..tostring(k))
244246
end
245247
end
246248
statusVisited[t] = nil
@@ -545,6 +547,9 @@ function json.decode(str)
545547
statusBuf = str
546548
statusPos = 1
547549
statusTop = 0
550+
if str == "" then
551+
decode_error("empty string is not a valid JSON value")
552+
end
548553
local res = decode()
549554
while statusTop > 0 do
550555
decode_item()

jsonc.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ function json.decode_jsonc(str)
359359
statusBuf = str
360360
statusPos = 1
361361
statusTop = 0
362+
if str == "" then
363+
decode_error("empty string is not a valid JSON value")
364+
end
362365
if next_byte() == -1 then
363366
return json.null
364367
end

0 commit comments

Comments
 (0)