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