Skip to content

Commit

Permalink
Fixed parse_string() to handle "\\" correctly, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rxi committed Aug 19, 2015
1 parent 50f4512 commit 7f823ab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 7 additions & 4 deletions json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,12 @@ local function parse_string(str, i, chr)
has_unicode_escape = true
end
else
if not escape_chars[x] then
decode_error(str, j, "invalid escape char '" .. x .. "' in string")
end
has_escape = true
end
if not escape_chars[x] then
decode_error(str, j, "invalid escape char '" .. x .. "' in string")
end
last = nil

elseif x == '"' then
local s = str:sub(i + 1, j - 1)
Expand All @@ -232,8 +233,10 @@ local function parse_string(str, i, chr)
s = s:gsub("\\.", escape_char_map_inv)
end
return s, j + 1

else
last = x
end
last = x
end
decode_error(str, i, "expected closing quote for string")
end
Expand Down
2 changes: 2 additions & 0 deletions test/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ test("decode escape", function()
[ [["\u263a"]] ] = '',
[ [["\ud83d\ude02"]] ] = '😂',
[ [["\r\n\t\\\""]] ] = '\r\n\t\\"',
[ [["\\"]] ] = '\\',
[ [["\\\\"]] ] = '\\\\',
}
for k, v in pairs(t) do
local res = json.decode(k)
Expand Down

0 comments on commit 7f823ab

Please sign in to comment.