I am trying to use Jackson library for deserialization since I have a scenario wherein I have to validate for null
values in many JsonProperty if it is set as required=true
.
Here is the code snippet.
public class JacksonValidator {
private static final ObjectMapper MAPPER = new ObjectMapper();
public static void main(String[] args) {
// Should succeed since all properties have value and required=true holds good
validate("{\"id\": \"1\",\"age\": 26,\"name\": \"name1\"}");
// Should throw exception since name is null (because of required=true)
validate("{\"id\": \"2\",\"age\": 28,\"name\": null}");
// Should throw exception since id is null (because of required=true)
validate("{\"id\": null,\"age\": 27,\"name\": \"name2\"}");
}
public static void validate(String json) {
try {
Customer customer = MAPPER.readValue(json, Customer.class);
System.out.println(customer);
}
catch (IOException e) {
throw new DeserializationException(String.format("Validation failed. Unable to parse json %s", json), e);
}
}
@Setter
@Getter
@ToString
public static class Customer {
private String id;
private Integer age;
private String name;
@JsonCreator
public Customer(@JsonProperty(value = "id", required = true) String id,
@JsonProperty(value = "age", required = false) Integer age,
@JsonProperty(value = "name", required = true) String name) {
this.id = id;
this.age = age;
this.name = name;
}
}
}
As you can see in the above code, I am trying to deserialize JSON
into Customer
class. If the required
property is set to true
for a JsonProperty
and while deserialization if this property encounters null
(for id
and name
field in the above code), I have to throw a custom DeserializationException
.
And also I need to have null
values processed(shouldn't fail) for fields where required=false
is set in JsonProperty(for age
field).
Here I cannot use MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL)
since I need to process null
values for required=false
fields.
Please let me know how this could be achieved using ObjectMapper.readValue
method or any other method that holds good here.
Any suggestions would be appreciated.