19

I am working on a project where the date formats returned in JSON payloads aren't consistent (that's another issue all together). The project I'm working on uses Jackson to parse the JSON responses. Right now I've written a few de/serializers to handle it but it's not elegant.

I want to know whether there's a way to configure Jackson with a set of possible date formats to parse for a particular response rather than writing several separate deserializers for each format. Similar to how GSON handles the problem in this question

Community
  • 1
  • 1
Kurian Vithayathil
  • 807
  • 2
  • 7
  • 20

4 Answers4

36

Here is a Jackson Multi Date Format Serializer.

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * https://stackoverflow.com/a/42567051/11152683
 */
public class MultiDateDeserializer extends StdDeserializer<Date> {
    private static final long serialVersionUID = 1L;

    private static final SimpleDateFormat[] DATE_FORMATTERS = new SimpleDateFormat[]{
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z"),
            new SimpleDateFormat("yyyy-MM-dd HH:mm' UTC'")
    };

    public MultiDateDeserializer() {
        this(null);
    }

    public MultiDateDeserializer(Class<?> vc) {
        super(vc);
    }

    @Override
    public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        JsonNode node = jp.getCodec().readTree(jp);
        final String date = node.textValue();

        for (SimpleDateFormat formatter : DATE_FORMATTERS) {
            try {
                return formatter.parse(date); //.toInstant();
            } catch (ParseException e) {
            }
        }
        throw new JsonParseException(jp, "Unparseable date: \"" + date + "\". Supported formats: " +
                Arrays.stream(DATE_FORMATTERS).map(SimpleDateFormat::toPattern).collect(Collectors.joining("; ")));
    }
}

You can use this simply by annotating a field as follows:

@JsonProperty("date") @JsonDeserialize(using = MultiDateDeserializer.class) final Date date,
Lubo
  • 1,621
  • 14
  • 33
rouble
  • 16,364
  • 16
  • 107
  • 102
  • 1
    can also be implemented as follows: SimpleModule module = new SimpleModule(); module.addDeserializer(Date.class,new MultiDateDeserializer()); mapper.registerModule(module); – WhiteWolfza Aug 11 '22 at 07:38
  • DO NOT create instance of SimpleDateFormat on each parsing. Save them pre-created similarly like DATE_FORMATS. (I would edit code, if queue was not full). – Lubo Oct 19 '22 at 21:20
  • This is an awesome answer, but if someone needs to do this but with LocalDateTime, there are some great options. You can use a DateTimeFormatterBuilder to add optional conditions. More info on that here: https://stackoverflow.com/a/40175568/6477292 – fudge Mar 01 '23 at 16:57
5

In the meanwhile, an annotation became available for a much simpler solution:

public class DateStuff {
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd,HH:00", timezone="CET")
    public Date creationTime;
}
Keno
  • 307
  • 3
  • 11
  • 6
    @Keno right, but as of now (jackson 2.9.9), this does neither support muliple `@JsonFormat` annotations nor [DateTimeFormatter](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html)-style optional parts. @rouble 's answer is currently the way to go for multiple formats :) – bernard paulus Jul 10 '19 at 15:13
5

You can use the optional section to define multiple formats:

public class DateStuff {
    @JsonFormat(pattern="[MMM dd, yyyy HH:mm:ss][MMM dd, yyyy][yyyy-MM-dd'T'HH:mm:ssZ]")
    public Date creationTime;
    @JsonFormat(pattern="MMM dd, yyyy[ HH:mm:ss]")
    public Date anotherCreationTime;
}

The first defines two completely different formats. The second where only a small portion is optional.

SPee
  • 656
  • 4
  • 5
1

A better solution is to use StdDateFormat instead. It's Jackson's built-in Date formatter and supports most of the variations of Date formats. Use it like below

StdDateFormat isoDate = new StdDateFormat();
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(isoDate);
Code_Worm
  • 4,069
  • 2
  • 30
  • 35