Skip to content
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

Conversation

Marcono1234
Copy link
Collaborator

Fixes #847

Additionally adds validation for JsonWriter.value(Number) argument to avoid creation malformed JSON output by accident when Number.toString() does not return a valid JSON number.

Copy link
Member

@eamonnmcmanus eamonnmcmanus left a 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.

gson/src/main/java/com/google/gson/stream/JsonWriter.java Outdated Show resolved Hide resolved
@@ -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]+)?)");
Copy link
Member

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.

Copy link
Member

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.

Copy link
Collaborator Author

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.

Copy link
Member

@eamonnmcmanus eamonnmcmanus left a 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.

@eamonnmcmanus eamonnmcmanus merged commit e2e851c into google:master Jan 28, 2022
@Marcono1234 Marcono1234 deleted the marcono1234/LazilyParsedNumber-default-adapter branch January 28, 2022 19:32
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.

LazilyParsedNumber being serialised as JsonObject
2 participants