I am trying to deserialize my JSON and everything is working fine but I would like to add few conditions to make it better.
Following is my Parent class based on which deserialize happen:
public class ParentJSON{
@NotNull
private String name;
private ChildJSON type;
}
The field type
is optional is JSON. However if the field type
is present in JSON then I would like to make the fields in ChildJSON
mandatory:
public class ChildJSON{
private String childName;
private String childType;
}
If I directly add @NotNull
to my ChildJSON
fields then it would throw error if type
is not present in JSON.
Here is my client file which will read the JSONFILE:
public class Client {
public static void main(String args[]) {
final ObjectMapper objectMapper = new ObjectMapper();
ParentJSON json = objectMapper.readValue(ApplicationMain.class.getResourceAsStream("/JSONFile.json"), ParentJSON.class);
}
}
My json would look something like this:
{
{
"name":"Hello"
},
{
"name":"Bye",
"type":{
"childName":"childByeName",
"childType":"childByeType"
}
}
}