Skip to content

Java parser: include the position in string decoding errors - #1130

Open
sferik wants to merge 1 commit into
ruby:masterfrom
sferik:java-string-decoding-error-position
Open

sferik wants to merge 1 commit into
ruby:masterfrom
sferik:java-string-decoding-error-position

Conversation

@sferik

@sferik sferik commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

On JRuby, errors raised while decoding a string had no position:

require 'json'
JSON.parse(%([1,\n"\\q"]))
# MRI:   invalid escape character in string: '\q"]' at line 2 column 2 (line: 2, column: 2)
# JRuby: invalid escape character in string: q (line: nil, column: nil)

The same went for incomplete \u escapes, broken surrogate pairs and raw control characters. StringDecoder built its own ParserError, so the parser's parseError, which adds the line, column, message suffix and json_path, was never used for these.

StringDecoder now throws a DecodeError with the message and the offset to report, and the parser turns it into a regular parse error at that offset. The offsets follow parser.c:

  • errors in an escape sequence are reported at its backslash
  • surrogate errors are reported at the start of the text after the previous escape (p in json_string_unescape)
  • raw control characters are reported where they are

The messages keep their current wording, except that surrogate error snippets now start at the reported offset, as in C. Porting the wording belongs with the other error message differences in #1123.

Remaining differences, left for later:

@byroot
byroot requested a review from headius November 5, 2026 20:39
StringDecoder built its own ParserError, so errors such as an invalid
escape, an incomplete \u escape, a broken surrogate pair or a raw
control character in a string had no line, column, message suffix or
json_path. The parser's parseError, which adds all of these, was never
used for them.

StringDecoder now throws a DecodeError that carries the message and the
offset to report, and the parser turns it into a regular parse error at
that offset. The offsets follow the C parser: errors in an escape
sequence are reported at its backslash, surrogate errors at the start
of the text after the previous escape, and raw control characters where
they are. The messages keep their current wording, apart from surrogate
errors, whose snippet now starts at that same offset.

This lets test_parse_error_position_at_newline run on JRuby, and adds
a test for errors in escape sequences.

Part of ruby#1123
@sferik
sferik force-pushed the java-string-decoding-error-position branch from d867a62 to e86a0aa Compare November 5, 2026 20:46

@headius headius left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick review on mobile looks good. I will approve after a more rigorous review shortly.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants