diff --git a/lib/json/common.rb b/lib/json/common.rb index 01a4be18c..4b2d6d87f 100644 --- a/lib/json/common.rb +++ b/lib/json/common.rb @@ -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(/["\\]/, { '"' => '\\"', '\\' => '\\\\' }) diff --git a/test/json/json_parser_test.rb b/test/json/json_parser_test.rb index 47d84b398..3607be33a 100644 --- a/test/json/json_parser_test.rb +++ b/test/json/json_parser_test.rb @@ -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