2022-06-28

Jackson-databind deserializes JSON with missing property as "null" instead of default value

When deserializing a JSON string, missing properties are being set as "null" when they shouldn't be. Below is a POJO class:

    @Builder
    @Getter
    @Setter
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    @EqualsAndHashCode
    static class MyPojo {
        
        @JsonProperty(value = "OptionalProp", required = false, defaultValue = "")
        @Builder.Default
        @Nullable
        @JsonSetter(value = "", nulls = Nulls.AS_EMPTY)
        private String optionalProp = "";
    
        @JsonProperty(value = "RequiredProp", required = false, defaultValue = "")
        @Builder.Default
        @Nullable
        @JsonSetter(value = "", nulls = Nulls.AS_EMPTY)
        private String requiredProp = "";
    }

JSON String to deserialize:

{
  "RequiredProp" : "test"
}

Here is the deserialization:

private final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private final myPojo = OBJECT_MAPPER.readValue(inputStream, MyPojo.class);

And here is the output:

MyPojo(optionalProp=null, requiredProp=test)

BUT creating the POJO with builder:

        final MyPojo myPojo = MyPojo.builder()
            .requiredProp("test")
            .build();

Results in the following POJO:

MyPojo(optionalProp=, requiredProp=test)

I'm using:

Jackson-databind 2.12.x
Jackson-annotation 2.12.x
Jackson-core 2.12.x

Is there a minor version change from one of these packages that changes the behavior?



No comments:

Post a Comment