Skip to content

Commit 4994fe3

Browse files
ojuschugh1byroot
authored andcommitted
Add error position and json_path to the JRuby extension
1 parent bbc73fb commit 4994fe3

3 files changed

Lines changed: 84 additions & 19 deletions

File tree

‎CHANGES.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Unreleased
44

5+
* JRuby: parser errors now include the position (`line`, `column` and the message suffix) as well as `json_path`, matching the C extension.
6+
57
### 2026-09-09 (3.0.2)
68

79
* Fix `JSON.load_file` and `JSON.load_file!` to load on Ruby 2.7.0 through 2.7.2, which cannot parse a leading parameter before `...`.

‎java/src/json/ext/Parser.java‎

Lines changed: 72 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/json/json_parser_test.rb‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,6 @@ def test_parse_error_snippet
852852
end
853853

854854
def test_parse_error_json_path
855-
omit "JRuby errors don't contain positions" if RUBY_ENGINE == "jruby"
856-
857855
assert_parse_error_at "$", "xyz"
858856
assert_parse_error_at "$.a", '{"a": xyz}'
859857
assert_parse_error_at "$[3]", '[1, 2, "hi", xyz]'
@@ -870,9 +868,17 @@ def test_parse_error_json_path
870868
assert_parse_error_at "$[5]", '[1,2,3,4,5,]'
871869
end
872870

873-
def test_parse_error_json_path_on_load
874-
omit "JRuby errors don't contain positions" if RUBY_ENGINE == "jruby"
871+
def test_parse_error_position
872+
error = assert_raise(JSON::ParserError) { JSON.parse("[1,\n@") }
873+
assert_equal 2, error.line
874+
assert_equal 1, error.column
875+
876+
error = assert_raise(JSON::ParserError) { JSON.parse('{"a": {"b": [1, {"c": 1, "c": 2}]}}') }
877+
assert_equal 1, error.line
878+
assert_equal 17, error.column
879+
end
875880

881+
def test_parse_error_json_path_on_load
876882
assert_parse_error_at "$" do
877883
JSON.load('{"a": {"b": {"c":', -> (obj) {
878884
if String === obj
@@ -895,8 +901,6 @@ def test_parse_error_json_path_on_load
895901
end
896902

897903
def test_parse_error_json_path_key_escaping
898-
omit "JRuby errors don't contain positions" if RUBY_ENGINE == "jruby"
899-
900904
assert_parse_error_at '$["hello world"]', '{"hello world": xyz}'
901905
assert_parse_error_at '$["a\"b"]', '{"a\"b": xyz}'
902906
assert_parse_error_at '$[""]', '{"": xyz}'
@@ -905,8 +909,6 @@ def test_parse_error_json_path_key_escaping
905909
end
906910

907911
def test_parse_error_json_path_duplicate_key
908-
omit "JRuby errors don't contain positions" if RUBY_ENGINE == "jruby"
909-
910912
assert_parse_error_at "$.a", '{"a": 1, "a": 2}'
911913
assert_parse_error_at "$.x.a", '{"x": {"a": 1, "b": 2, "a": 3}}'
912914
assert_parse_error_at "$.arr[0].a", '{"arr": [{"a": 1, "a": 2}]}'

0 commit comments

Comments
 (0)