question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Override default formatter in JavaTimeModule

See original GitHub issue

Is there a way to override the default formatter used in the JavaTimeModule? Using the @JsonFormat-annotation works fine, but I would like to setup the formatter where I instantiate my ObjectMapper.

I know I can simply register my own JsonDeserializer, but I would prefere to keep using the already existing implementation, so I could still use @JsonFormat where necessary.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
cowtowncodercommented, Jul 28, 2020

I would actually recommend a different mechanism, so-called “Config overrides”, since use of actual (de)serializers directly is actually not part of supported API (it may work, but might not and may break with newer versions).

Usage with 2.x is like so:

ObjectMapper mapper = ...;
mapper.configOverride(LocalDateTime.class)
    .setFormat(JsonFormat.Value.forPattern("yyyy+MM+dd");   

The main reason why this is preferred is simply that the mechanism of construction of (de)serializers may change as it is considered part of internal API.

1reaction
chkpntcommented, Jul 24, 2020

Yes, I can confirm, it’s working fine. My ObjectMapper is now using a custom default but still be able to use the @JsonFormat-Annotations. Thanks for the hint!

public class ObjectMapperFactory
{
  
  private static final DateTimeFormatter GERMAN_DATE_FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy");

  public ObjectMapper create()
  {
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(GERMAN_DATE_FORMATTER));
    
    return new ObjectMapper()
        .registerModule(new Jdk8Module())
        .registerModule(javaTimeModule)
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        .configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true);
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set format of string for java.time.Instant using ...
One solution is to use jackson-modules-java8. Then you can add a JavaTimeModule to your object mapper:
Read more >
Spring Boot: Customize the Jackson ObjectMapper - Baeldung
When using Spring Boot, we have the option to customize the default ObjectMapper or to override it. We'll cover both options in the...
Read more >
Formatting Java Time with Spring Boot using JSON - TouK
The aim of this post is to summarize and review ways of formatting Java Time objects using Spring Boot and Jackson library.
Read more >
DurationDeserializer should use @JsonFormat.pattern (and ...
When deserializing DurationDeserializer treats all durations of number values as seconds and delegates string values to Duration.parse.
Read more >
JavaTimeModule (Jackson datatype: JSR310 2.9.0 API)
Legacy version has the same functionality, but slightly different default ... Period , which always results in an ISO-8601 format because Periods must...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found