Closed grantamos closed 2 months ago
beginObject
specifies a checked exception of typeIOException
here [...] Note thatunexpectedTokenError
says itthrows IOException
but the IDE warns this throws is unused.
They have throws IOException
because reading (which occurs when doPeek()
/ peek()
is called) can throw an IOException.
To me it seems it is intentional that an IllegalStateException is thrown. Whether it would have been more consistent to throw an IOException or a custom subclass since a JSON syntax error causes an IOException (more specifically MalformedJsonException) is a different question. But this can probably not be changed easily anymore either since it would be backward incompatible for users who expect an IllegalStateException respectively don't expect an IOException in that case.
For some of the JsonReader
methods the documentation explicitly mentions the IllegalStateException: nextString()
, nextBoolean()
, nextNull()
, nextDouble()
, nextLong()
, nextInt()
So to me the issue here seems to rather be that the other JsonReader
methods (such as beginObject
you mentioned) don't have the IllegalStateException
documented.
Would you like to create a pull request for this (see also the contributing guide)? That would probably involve:
@throws IllegalStateException
to all JsonReader
methods which throw this exception on token mismatchJsonReader
method documentation and instead add something generic as Using the reader after {@link #close()} has been called will cause an {@link IllegalStateException}.
to the class documentation right above the "Parsing JSON" header (or alternatively / additionally to the close()
documentation).
Because this affects more than just these value reading methods, and documenting it separately for all affected methods might be a bit verbose.Have created #2670 now to improve the JsonReader
documentation.
Gson version
2.9.0
Java / Android version
Unsure - if this is needed please let me know & I can dig this up, though it should be of no consequence.
Used tools
Nothing of consequence to this bug.
Description
When parsing an JSON object that fails due to the value being an incorrect type, an unchecked exception is thrown.
beginObject
specifies a checked exception of typeIOException
here: https://github.com/google/gson/blob/18b08922537c16b0de1d503c82307d613aae0138/gson/src/main/java/com/google/gson/stream/JsonReader.java#L454When it fails to find an object, however, it throws an
IllegalStateException
here: https://github.com/google/gson/blob/18b08922537c16b0de1d503c82307d613aae0138/gson/src/main/java/com/google/gson/stream/JsonReader.java#L463 and here: https://github.com/google/gson/blob/18b08922537c16b0de1d503c82307d613aae0138/gson/src/main/java/com/google/gson/stream/JsonReader.java#L1744This isn't caught at compile time because
IllegalStateException
is aRuntimeException
. Note thatunexpectedTokenError
says itthrows IOException
but the IDE warns this throws is unused.Expected behavior
We expected an
IOException
to be thrown. Else, thebeginObject
method should check any other exceptions.Actual behavior
An unchecked runtime exception was thrown.
Reproduction steps
Attempt to parse a JSON payload expecting an object but finding another type.
e.g.
Exception stack trace