-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add LazilyParsedNumber default adapter #2060
Add LazilyParsedNumber default adapter #2060
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I'm trying this against all of Google's tests and if there are no problems I think it is a good fix.
@@ -160,6 +166,9 @@ | |||
HTML_SAFE_REPLACEMENT_CHARS['&'] = "\\u0026"; | |||
HTML_SAFE_REPLACEMENT_CHARS['='] = "\\u003d"; | |||
HTML_SAFE_REPLACEMENT_CHARS['\''] = "\\u0027"; | |||
|
|||
// Syntax as defined by https://datatracker.ietf.org/doc/html/rfc8259#section-6 | |||
VALID_JSON_NUMBER_PATTERN = Pattern.compile("-?(?:0|[1-9][0-9]*(?:\\.[0-9]+)?(?:[eE][-+]?[0-9]+)?)"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Google's tests found a problem with this: the string 0.8252564
is rejected. I think it should be:
-?(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][-+]?[0-9]+)?
I removed the outermost group because it doesn't seem necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, could you just make this an initializer on VALID_JSON_NUMBER_PATTERN
? Unlike the other fields here it's just a plain initialization. You might want to move it before those other fields so they remain next to the static {}
block that initializes them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, the pattern was incorrect, thanks a lot for noticing this! I have fixed it and also added more tests for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's great, thanks! I had preemptively retried this change against Google's internal tests with the regex fix, and everything passed.
Fixes #847
Additionally adds validation for
JsonWriter.value(Number)
argument to avoid creation malformed JSON output by accident whenNumber.toString()
does not return a valid JSON number.