mirror of
https://github.com/TangentFoxy/rxi-json.lua.git
synced 2025-07-28 02:52:17 +00:00
Fixed parse_string() to handle "\\" correctly, added tests
This commit is contained in:
11
json.lua
11
json.lua
@@ -214,11 +214,12 @@ local function parse_string(str, i, chr)
|
|||||||
has_unicode_escape = true
|
has_unicode_escape = true
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
if not escape_chars[x] then
|
||||||
|
decode_error(str, j, "invalid escape char '" .. x .. "' in string")
|
||||||
|
end
|
||||||
has_escape = true
|
has_escape = true
|
||||||
end
|
end
|
||||||
if not escape_chars[x] then
|
last = nil
|
||||||
decode_error(str, j, "invalid escape char '" .. x .. "' in string")
|
|
||||||
end
|
|
||||||
|
|
||||||
elseif x == '"' then
|
elseif x == '"' then
|
||||||
local s = str:sub(i + 1, j - 1)
|
local s = str:sub(i + 1, j - 1)
|
||||||
@@ -232,8 +233,10 @@ local function parse_string(str, i, chr)
|
|||||||
s = s:gsub("\\.", escape_char_map_inv)
|
s = s:gsub("\\.", escape_char_map_inv)
|
||||||
end
|
end
|
||||||
return s, j + 1
|
return s, j + 1
|
||||||
|
|
||||||
|
else
|
||||||
|
last = x
|
||||||
end
|
end
|
||||||
last = x
|
|
||||||
end
|
end
|
||||||
decode_error(str, i, "expected closing quote for string")
|
decode_error(str, i, "expected closing quote for string")
|
||||||
end
|
end
|
||||||
|
@@ -152,6 +152,8 @@ test("decode escape", function()
|
|||||||
[ [["\u263a"]] ] = '☺',
|
[ [["\u263a"]] ] = '☺',
|
||||||
[ [["\ud83d\ude02"]] ] = '😂',
|
[ [["\ud83d\ude02"]] ] = '😂',
|
||||||
[ [["\r\n\t\\\""]] ] = '\r\n\t\\"',
|
[ [["\r\n\t\\\""]] ] = '\r\n\t\\"',
|
||||||
|
[ [["\\"]] ] = '\\',
|
||||||
|
[ [["\\\\"]] ] = '\\\\',
|
||||||
}
|
}
|
||||||
for k, v in pairs(t) do
|
for k, v in pairs(t) do
|
||||||
local res = json.decode(k)
|
local res = json.decode(k)
|
||||||
|
Reference in New Issue
Block a user