Jackson - How can I not ignore null values for a joda.time.DateTime field when doing conversion?

322 views Asked by At

I am using Jackson from Dropwizard. My class has some fields like

public class SubscriptionInfo{
        @JsonProperty private Integer subscriptionId;
        @JsonProperty private Integer packageId;
        @JsonProperty private DateTime startDate;
        @JsonProperty private DateTime endDate;
        // other fields, getters, setters
        ...
}

When startDate or endDate is null, I would still want the key exists in the converted JSON. I did not do any annotations like @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL). The converted JSON looks like

        {
            "subscriptionId": 111,
            "packageId": null,
            "startDate": 1477908963000
        }

As you can see "packageId" is present but "endDate" is not. I am wondering whether I can have the keys preserved for "startDate" and "endDate" when they are null.

Update:

I am using 0.9.2 and jackson 2.6.1. Seems like there is a dependency for jackson-datatype-joda 2.6.1. I am wondering whether that's the default behaviour from JodaModule. If so, how can I change it?

1

There are 1 answers

0
André Barbosa On

This seems to be a known issue which was fixed in Jackson 2.8+ and 2.7.6+:

https://github.com/dropwizard/dropwizard/issues/1627

Incrementing the DW version to 1.0.0+ seems to solve the problem. You could also manipulate the Jackson dependencies that DW is using to point at one where the issue has been fixed but you might run into several other issues.