1

In the json of the post request I have several different date formats. I'm having troubled deserializing all at the same time. I've created a configuration class that will handle one or the other just fine. How do I add additional deserializers to handle the other formats?

I don't have access to the POJO to add any annotations there.

Here's an error I get for one of the dates I'm unable to deserialize JSON parse error: Cannot deserialize value of type java.time.LocalDateTime from String "09/03/2020 10:59:48": Failed to deserialize java.time.LocalDateTime:

@Configuration
public class JacksonConfig {

    
    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        JavaTimeModule module = new JavaTimeModule();
        LocalDateTimeDeserializer localDateTimeDeserializer = new LocalDateTimeDeserializer(
                DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss"));
        module.addDeserializer(LocalDateTime.class, localDateTimeDeserializer);
        return Jackson2ObjectMapperBuilder.json().modules(module)
                .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).build();
    }

}
Bob
  • 993
  • 2
  • 10
  • 21

1 Answers1

0

I was able to resolve my issue by overriding the LocalDateTimeDeserializer's deserialize method. I modified the solution from Configure Jackson to parse multiple date formats

public class MultiDateDeserializer extends LocalDateTimeDeserializer {

    public MultiDateDeserializer() {
        this(null);
    }

    public MultiDateDeserializer(DateTimeFormatter formatter) {
        super(formatter);
    }

    private static final long serialVersionUID = 1L;

    private static final String[] DATE_FORMATS = new String[] { "yyyy-MM-dd'T'HH:mm:ss", "MM/dd/yyyy HH:mm:ss" };

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

        for (String DATE_FORMAT : DATE_FORMATS) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT, Locale.ROOT);
            try {
                return LocalDateTime.parse(date, formatter);
            } catch (DateTimeParseException e) {
            
            }
        }
        throw new ParseException(0,
                "Unparseable date: \"" + date + "\". Supported formats: " + Arrays.toString(DATE_FORMATS));
    }
}

And then in my JacksonConfig I have...

@Configuration
public class JacksonConfig {
        @Bean
        @Primary
        public ObjectMapper objectMapper() {
            JavaTimeModule module = new JavaTimeModule();
            MultiDateDeserializer multiDateDeserializer = new MultiDateDeserializer();
            module.addDeserializer(LocalDateTime.class, multiDateDeserializer);
            return Jackson2ObjectMapperBuilder.json().modules(module)
                    .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).build();
        }
    }
Bob
  • 993
  • 2
  • 10
  • 21