I want to deserialize a field and be sure that no errors occurred, every field was read. If something is wrong with json file, I want to know.
private static class A {
int x;
}
public static void main(String[] args) {
Gson gson = new Gson();
A a;
try {
Reader reader = new StringReader("{\"y\":10}");
a = gson.getAdapter(A.class).fromJson(reader);
} catch (IOException | JsonSyntaxException e) {
System.out.println(e.getMessage());
return;
}
}
In this example I changed field name in json string from x
to y
. I expected to get an exception. It doesn't occur, but a.x
is initialized to zero.
How do I check that every field has been read?