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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Unreleased

* Fix `JSON::ParserError#json_path` raising `NoMethodError` when the document was parsed with `symbolize_names: true`.

### 2026-10-13 (3.1.0)

* Added `rfc8785` generator option to canonicalize the output. Replaces the `json-canonicalization` with numerous correctness fixes.
Expand Down
3 changes: 2 additions & 1 deletion lib/json/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,14 @@ def build_json_path(segments)
when Integer
"[#{segment}]"
when String, Symbol
segment = segment.to_s
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(/["\\]/, { '"' => '\\"', '\\' => '\\\\' })
segment = segment.gsub(/["\\]/, { '"' => '\\"', '\\' => '\\\\' })
%{["#{segment}"]}
end
else
Expand Down
12 changes: 12 additions & 0 deletions test/json/json_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,18 @@ def test_parse_error_json_path_key_escaping
assert_parse_error_at '$.foo["1x"]', '{"foo": {"1x": xyz}}'
end

def test_parse_error_json_path_symbolize_names
assert_parse_error_at "$.a[1].b" do
JSON.parse('{"a": [1, {"b": xyz}]}', symbolize_names: true)
end
assert_parse_error_at '$["hello world"]' do
JSON.parse('{"hello world": xyz}', symbolize_names: true)
end
assert_parse_error_at "$.x.a" do
JSON.parse('{"x": {"a": 1, "a": 2}}', symbolize_names: true)
end
end

def test_parse_error_json_path_duplicate_key
assert_parse_error_at "$.a", '{"a": 1, "a": 2}'
assert_parse_error_at "$.x.a", '{"x": {"a": 1, "b": 2, "a": 3}}'
Expand Down
Loading