Is it possible to optimize my Custom Deserializer?

Viewed 103

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 ?

1 Answers

I would suggest another approach. Instead of using a CustomDeserializer for User, I would rather write a custom deserializer for String. Then you can use this deserializer for the string properties of your User class, where you want to treat an empty string as null. This also makes maintenance easier whenever you need to change the properties of your User class.

public class User {

    @JsonDeserialize(using = CustomStringDeserializer.class)
    private String firstName;

    @JsonDeserialize(using = CustomStringDeserializer.class)
    private String lastName;

    @JsonDeserialize(using = CustomStringDeserializer.class)
    private String age;

    @JsonDeserialize(using = CustomStringDeserializer.class)
    private String address;
    
    // getters and setters omitted here for brevity
}

The custom string deserializer can be implemented very easy by extending Jackson's StringDeserializer.

public class CustomStringDeserializer extends StringDeserializer {

    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String value = super.deserialize(p, ctxt);
        if (value != null && value.isEmpty())
            value = null;     // replace empty string by null
        return value;
    }
}
Related