1

I have class defining one field:

class Dog {
    int legs;
}

and JSON that contains one field (heads) not defined in the class.

{
  "legs": 5,
  "heads": 3
}

Now, when I try to parse this with Gson, no exception about undefined field is thrown.

new Gson().fromJson(json, Dog.class);

How to I force this check?

This is opposite to similar questions like that Strict JSON parsing with Google's Gson?, which is about JSON not defining values for fields in class, whereas mine is about class not defining fields in JSON document.

It seems duplicate of How to make GSON fail on unknown properties, but I cannot mark my question as a duplicate because the other one no upvoted answer...

There's also discussion on Gson GitHub: https://github.com/google/gson/issues/188

dzieciou
  • 4,049
  • 8
  • 41
  • 85

0 Answers0