fix argument to json.decode
This commit is contained in:
parent
df9e0cb21b
commit
1867b30d68
@ -72,9 +72,9 @@ function json.encode(data)
|
||||
return "null" -- Unsupported type
|
||||
end
|
||||
|
||||
function json.decode(json)
|
||||
function json.decode(data)
|
||||
local pos = 1
|
||||
local len = #json
|
||||
local len = #data
|
||||
|
||||
-- Pre-compute byte values
|
||||
local b_space = string.byte(' ')
|
||||
@ -102,7 +102,7 @@ function json.decode(json)
|
||||
local function skip()
|
||||
local b
|
||||
while pos <= len do
|
||||
b = json:byte(pos)
|
||||
b = data:byte(pos)
|
||||
if b > b_space or (b ~= b_space and b ~= b_tab and b ~= b_cr and b ~= b_lf) then
|
||||
break
|
||||
end
|
||||
@ -128,12 +128,12 @@ function json.decode(json)
|
||||
local c, b
|
||||
|
||||
while pos <= len do
|
||||
b = json:byte(pos)
|
||||
b = data:byte(pos)
|
||||
|
||||
if b == b_backslash then
|
||||
-- Add the chunk before the escape character
|
||||
if pos > start then
|
||||
result[result_pos] = json:sub(start, pos - 1)
|
||||
result[result_pos] = data:sub(start, pos - 1)
|
||||
result_pos = result_pos + 1
|
||||
end
|
||||
|
||||
@ -142,7 +142,7 @@ function json.decode(json)
|
||||
error("Unterminated string escape")
|
||||
end
|
||||
|
||||
c = json:byte(pos)
|
||||
c = data:byte(pos)
|
||||
if c == b_quote then
|
||||
result[result_pos] = '"'
|
||||
elseif c == b_backslash then
|
||||
@ -160,7 +160,7 @@ function json.decode(json)
|
||||
elseif c == string.byte('t') then
|
||||
result[result_pos] = '\t'
|
||||
else
|
||||
result[result_pos] = json:sub(pos, pos)
|
||||
result[result_pos] = data:sub(pos, pos)
|
||||
end
|
||||
|
||||
result_pos = result_pos + 1
|
||||
@ -169,7 +169,7 @@ function json.decode(json)
|
||||
elseif b == b_quote then
|
||||
-- Add the final chunk
|
||||
if pos > start then
|
||||
result[result_pos] = json:sub(start, pos - 1)
|
||||
result[result_pos] = data:sub(start, pos - 1)
|
||||
result_pos = result_pos + 1
|
||||
end
|
||||
|
||||
@ -186,7 +186,7 @@ function json.decode(json)
|
||||
-- Parse a number more efficiently
|
||||
parse_number = function()
|
||||
local start = pos
|
||||
local b = json:byte(pos)
|
||||
local b = data:byte(pos)
|
||||
|
||||
-- Skip any sign
|
||||
if b == b_minus then
|
||||
@ -194,7 +194,7 @@ function json.decode(json)
|
||||
if pos > len then
|
||||
error("Malformed number")
|
||||
end
|
||||
b = json:byte(pos)
|
||||
b = data:byte(pos)
|
||||
end
|
||||
|
||||
-- Integer part
|
||||
@ -205,20 +205,20 @@ function json.decode(json)
|
||||
repeat
|
||||
pos = pos + 1
|
||||
if pos > len then break end
|
||||
b = json:byte(pos)
|
||||
b = data:byte(pos)
|
||||
until b < b_0 or b > b_9
|
||||
|
||||
-- Fractional part
|
||||
if pos <= len and b == b_dot then
|
||||
pos = pos + 1
|
||||
if pos > len or json:byte(pos) < b_0 or json:byte(pos) > b_9 then
|
||||
if pos > len or data:byte(pos) < b_0 or data:byte(pos) > b_9 then
|
||||
error("Malformed number")
|
||||
end
|
||||
|
||||
repeat
|
||||
pos = pos + 1
|
||||
if pos > len then break end
|
||||
b = json:byte(pos)
|
||||
b = data:byte(pos)
|
||||
until b < b_0 or b > b_9
|
||||
end
|
||||
|
||||
@ -229,13 +229,13 @@ function json.decode(json)
|
||||
error("Malformed number")
|
||||
end
|
||||
|
||||
b = json:byte(pos)
|
||||
b = data:byte(pos)
|
||||
if b == b_plus or b == b_minus then
|
||||
pos = pos + 1
|
||||
if pos > len then
|
||||
error("Malformed number")
|
||||
end
|
||||
b = json:byte(pos)
|
||||
b = data:byte(pos)
|
||||
end
|
||||
|
||||
if b < b_0 or b > b_9 then
|
||||
@ -245,11 +245,11 @@ function json.decode(json)
|
||||
repeat
|
||||
pos = pos + 1
|
||||
if pos > len then break end
|
||||
b = json:byte(pos)
|
||||
b = data:byte(pos)
|
||||
until b < b_0 or b > b_9
|
||||
end
|
||||
|
||||
return tonumber(json:sub(start, pos - 1))
|
||||
return tonumber(data:sub(start, pos - 1))
|
||||
end
|
||||
|
||||
-- Parse an object more efficiently
|
||||
@ -258,7 +258,7 @@ function json.decode(json)
|
||||
local obj = {}
|
||||
|
||||
skip()
|
||||
if pos <= len and json:byte(pos) == b_rcurly then
|
||||
if pos <= len and data:byte(pos) == b_rcurly then
|
||||
pos = pos + 1
|
||||
return obj
|
||||
end
|
||||
@ -266,14 +266,14 @@ function json.decode(json)
|
||||
while pos <= len do
|
||||
skip()
|
||||
|
||||
if json:byte(pos) ~= b_quote then
|
||||
if data:byte(pos) ~= b_quote then
|
||||
error("Expected string key")
|
||||
end
|
||||
|
||||
local key = parse_string()
|
||||
skip()
|
||||
|
||||
if json:byte(pos) ~= b_colon then
|
||||
if data:byte(pos) ~= b_colon then
|
||||
error("Expected colon")
|
||||
end
|
||||
pos = pos + 1
|
||||
@ -281,7 +281,7 @@ function json.decode(json)
|
||||
obj[key] = parse_value()
|
||||
skip()
|
||||
|
||||
local b = json:byte(pos)
|
||||
local b = data:byte(pos)
|
||||
if b == b_rcurly then
|
||||
pos = pos + 1
|
||||
return obj
|
||||
@ -303,7 +303,7 @@ function json.decode(json)
|
||||
local index = 1
|
||||
|
||||
skip()
|
||||
if pos <= len and json:byte(pos) == b_rbracket then
|
||||
if pos <= len and data:byte(pos) == b_rbracket then
|
||||
pos = pos + 1
|
||||
return arr
|
||||
end
|
||||
@ -314,7 +314,7 @@ function json.decode(json)
|
||||
|
||||
skip()
|
||||
|
||||
local b = json:byte(pos)
|
||||
local b = data:byte(pos)
|
||||
if b == b_rbracket then
|
||||
pos = pos + 1
|
||||
return arr
|
||||
@ -337,7 +337,7 @@ function json.decode(json)
|
||||
error("Unexpected end of input")
|
||||
end
|
||||
|
||||
local b = json:byte(pos)
|
||||
local b = data:byte(pos)
|
||||
|
||||
if b == b_quote then
|
||||
return parse_string()
|
||||
@ -345,13 +345,13 @@ function json.decode(json)
|
||||
return parse_object()
|
||||
elseif b == b_lbracket then
|
||||
return parse_array()
|
||||
elseif b == string.byte('n') and pos + 3 <= len and json:sub(pos, pos + 3) == "null" then
|
||||
elseif b == string.byte('n') and pos + 3 <= len and data:sub(pos, pos + 3) == "null" then
|
||||
pos = pos + 4
|
||||
return nil
|
||||
elseif b == string.byte('t') and pos + 3 <= len and json:sub(pos, pos + 3) == "true" then
|
||||
elseif b == string.byte('t') and pos + 3 <= len and data:sub(pos, pos + 3) == "true" then
|
||||
pos = pos + 4
|
||||
return true
|
||||
elseif b == string.byte('f') and pos + 4 <= len and json:sub(pos, pos + 4) == "false" then
|
||||
elseif b == string.byte('f') and pos + 4 <= len and data:sub(pos, pos + 4) == "false" then
|
||||
pos = pos + 5
|
||||
return false
|
||||
elseif b == b_minus or (b >= b_0 and b <= b_9) then
|
||||
|
Loading…
x
Reference in New Issue
Block a user