Serialize nullalbe value with ZonedDateTimeSerializer

36 views Asked by At

I have a nullable ZonedDateTime value that should be serialized to JSON using com.fasterxml.jackson.

I am currently doing it like so:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ElasticSearchConstants.ZONED_DATE_TIME_FORMAT)
@JsonSerialize(using = ZonedDateTimeSerializer.class)
@JsonDeserialize(using = ZonedDateTimeDeserializerFromIsoText.class)
private ZonedDateTime myVariable;

What I would expect to happen if myVariable == null is this:

{
    "myVariable": null
}

What I get is:

{}

This seems about right since the ZonedDateTimeSerializer expects a non-null input. Is there a way to get it to work with null values? If not, is there another serializer that I could use for this that does? Or do I have to write my own serializer?

0

There are 0 answers