Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/json/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ def build_json_path(segments)
when Integer
"[#{segment}]"
when String, Symbol
if segment.match?(/\A[a-zA-Z\$\_][a-zA-Z\$\_0-9]*\z/)
if !segment.valid_encoding?
segment = segment.b.gsub(/["\\]/n, { '"' => '\\"', '\\' => '\\\\' }).force_encoding(Encoding::UTF_8)
%{["#{segment}"]}
elsif segment.match?(/\A[a-zA-Z\$\_][a-zA-Z\$\_0-9]*\z/)
".#{segment}"
else
segment = segment.to_s.gsub(/["\\]/, { '"' => '\\"', '\\' => '\\\\' })
Expand Down
4 changes: 4 additions & 0 deletions test/json/json_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,10 @@ def test_parse_error_json_path
assert_parse_error_at "$[4]", '[1,2,3,4,5'
assert_parse_error_at "$[5]", '[1,2,3,4,5,'
assert_parse_error_at "$[5]", '[1,2,3,4,5,]'

if RUBY_ENGINE != "jruby"
assert_parse_error_at %($["\xff"][1]), %({"\xff":[1,x]})
end
end

def test_parse_error_position
Expand Down
Loading