Problem: How to identify that my Model object (POJO) members amount is not equal to the JSON members.
Notes:
- Fields name mismatch - Gson build-in feature;
- I have found few custom adapters implementations but they solve other issues like let Gson throw exceptions on wrong types but it required to annotate each filed with the annotation "JsonRequired", smells as overhead for Models within 20 members and at least 100 classes; and this let Gson throw exceptions on wrong types is more about the handling the problem of the time-to-time member appearing in a Model; and this one Detect if deserialized object is missing a field with the JsonConvert class in Json.NET from .Net is looking like a solution but I looking for Java suggestion/implementation;
Purpose: It is necessary for the integration level testing needs. I have an expected JSON string, getting the actual JSON string (for example from service) in case if the actual JSON has more members than my model I would like to be aware of this issue, let it be an exception throwing.
Code:
ActualPayLoad.json
{
"num1": 1,
"bool1": true,
"str1": "firstMissedInModel",
"nested": {
"num2": 1,
"bool2": true,
"str2": "secondMissedInModel"
}
}
TopModel.class
public class TopModel {
private Boolean bool1;
private NestedModel nested;
private Long num1;
// private String str1;
}
NestedModel.class
public class NestedModel {
private Boolean bool2;
private Long num2;
// private String str2;
}
Thank you for an eforts.