Skip to content

JsonReader not strict about JSON document consumption #835

@NightlyNexus

Description

@NightlyNexus

Say I have a class:
final class MyType {}

and a TypeAdapter:

final class MyTypeAdapter extends TypeAdapter<MyType> {
  // ...
  @Override public MyType read(JsonReader in) throws IOException {
    in.beginObject();
    return new MyType();
    }
}

Note the missing in.endObject() call.

My calling code:

String json = "{}";
Gson gson = new GsonBuilder().registerTypeAdapter(MyType.class, new MyTypeAdapter()).create();
StringReader stringReader = new StringReader(json);
MyType myType = gson.fromJson(stringReader, MyType.class);

The above will correctly fail (JsonIOException: JSON document was not fully consumed.).

However, using a JsonReader instead (gson.fromJson(new JsonReader(stringReader), MyType.class)) will not fail.
Is this working as intended?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions