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

Malformed Unicode escape sequence causes NumberFormatException instead of MalformedJsonException #2334

Closed
Marcono1234 opened this issue Mar 4, 2023 · 1 comment · Fixed by #2337
Labels

Comments

@Marcono1234
Copy link
Collaborator

Gson version

2.10.1

Description

JsonReader throws a NumberFormatException instead of a MalformedJsonException when it encounters a malformed Unicode escape sequence in the JSON data.
This actually works as designed:

* @throws NumberFormatException if any unicode escape sequences are
* malformed.

However, it is questionable whether that design is really a good choice because a MalformedJsonException seems to fit better here, especially since other malformed escape sequences do cause a MalformedJsonException.

Note that NumberFormatException being thrown is apparently not publicly documented, so changing this should be rather safe to do.

Expected behavior

A MalformedJsonException is thrown for malformed Unicode escape sequences.

Actual behavior

A NumberFormatException is thrown.

Reproduction steps

Reader reader = new StringReader("\"\\uXYZ\"");
JsonReader jsonReader = new JsonReader(reader);
jsonReader.nextString();
@eamonnmcmanus
Copy link
Member

I agree. It's weird to throw NumberFormatException here yet MalformedJsonException when for example there are fewer than 4 characters after the \u. I also agree that this is unlikely to affect anyone in practice, other than tests that might have been expecting the current exception.

MaicolAntali added a commit to MaicolAntali/gson that referenced this issue Mar 6, 2023
This commit replaces the `NumberFormatException` with `MalformedJsonException` in the `JsonReader#readEscapeCharacter()` and also fixes the tests.
@MaicolAntali MaicolAntali mentioned this issue Mar 6, 2023
9 tasks
eamonnmcmanus pushed a commit that referenced this issue Mar 6, 2023
* Fix #2334

This commit replaces the `NumberFormatException` with `MalformedJsonException` in the `JsonReader#readEscapeCharacter()` and also fixes the tests.

* Removes white-space
eamonnmcmanus pushed a commit that referenced this issue May 31, 2023
* Fix #2334

This commit replaces the `NumberFormatException` with `MalformedJsonException` in the `JsonReader#readEscapeCharacter()` and also fixes the tests.

* Removes white-space
tibor-universe pushed a commit to getuniverse/gson that referenced this issue Sep 14, 2024
* Fix google#2334

This commit replaces the `NumberFormatException` with `MalformedJsonException` in the `JsonReader#readEscapeCharacter()` and also fixes the tests.

* Removes white-space
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants