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

JsonReader not strict about JSON document consumption #835

Closed
NightlyNexus opened this issue Apr 22, 2016 · 2 comments
Closed

JsonReader not strict about JSON document consumption #835

NightlyNexus opened this issue Apr 22, 2016 · 2 comments

Comments

@NightlyNexus
Copy link
Contributor

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?

@JakeWharton
Copy link
Contributor

If you are passing a JsonReader instance then it's your responsibility to do the assertion.

The implementation of fromJson(String, Class<?>) is:

JsonReader jsonReader = newJsonReader(json);
Object object = fromJson(jsonReader, classOfT);
assertFullConsumption(object, jsonReader);
return object;

In the case where you are the one creating the JsonReader there's no opportunity to assert completion since you may just be deserializing part of the whole document.

@NightlyNexus
Copy link
Contributor Author

Oh, yes, I should have dug one step deeper into that. Thanks. Makes sense.

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

No branches or pull requests

2 participants