I created a new Custom Deserializer, which when an empty string is present in a json this is returned as a null
public class CustomDeserializer extends JsonDeserializer<User> {
@Override
public User deserialize(JsonParser jsonParser, DeserializationContext context) throws
IOException {
JsonNode node = jsonParser.readValueAsTree();
String firstName = null;
String lastName = null;
String age = null;
String address = null;
if(node.has("firstName") && !node.get("firstName").asText().isEmpty()) {
firstName = node.get("firstName").asText();
}
if(node.has("lastName") && !node.get("lastName").asText().isEmpty()) {
lastName = node.get("lastName").asText();
}
if(node.has("age") && !node.get("age").asText().isEmpty()) {
age = node.get("age").asText();
}
if(node.has("address") && !node.get("address").asText().isEmpty()) {
address = node.get("address").asText();
}
if(firstName == null && lastName == null && age == null && address == null) {
return null;
}
return new User(firstName, lastName, age, address);
}
My problem is from the performance point of view. I would like to optimize the fact of having to change the Deserializer at every possible change of field. Is it possible to optimize yours? I was thinking about using Reflaction but I don't know how to do it. It's possible ?