Skip to content

Conversation

Marcono1234
Copy link
Contributor

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.

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
Contributor 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