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.

Disabling WRITE_DATES_AS_TIMESTAMPS not working?

See original GitHub issue

According to the /datetime/ summary, java.time.offsetDateTime objects should serialize out to ISO-8601 strings simply by disabling the SerializationFeature#WRITE_DATES_AS_TIMESTAMPS feature?

I’ve tried to do this in two different ways in the JacksonConfig class:

ObjectMapper mapper = new ObjectMapper()
            .registerModule(new ParameterNamesModule())
            .registerModule(new Jdk8Module())
            .registerModule(new JavaTimeModule())
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        objectMapper
                .registerModule(new ParameterNamesModule())
                .registerModule(new Jdk8Module())
                .registerModule(new JavaTimeModule())
                .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        return objectMapper;
}

In both cases, serialized offsetDateTime objects continue to be printed in nanosecond timestamp format (i.e. “expiresOn”:1486138656.853000000).

Only by actually declaring @JsonFormat can I get them to print out in ISO-8601 string.

Is anyone else having this issue?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:6
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
cogito-abhijeetcommented, Sep 5, 2021

If you are using swagger with spring boot and your date is Date is always getting serialised as long, 
and SerializationFeature.WRITE_DATES_AS_TIMESTAMPS & spring.jackson.serialization.write-dates-as-timestamps=false are not helping, below solution worked for me. Add it to your class annotated with @SpringBootApplication:

@Autowired
private RequestMappingHandlerAdapter handlerAdapter;

@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
    handlerAdapter
            .getMessageConverters()
            .stream()
            .forEach(c -> {
                if (c instanceof MappingJackson2HttpMessageConverter) {
                    MappingJackson2HttpMessageConverter jsonMessageConverter = (MappingJackson2HttpMessageConverter) c;
                    ObjectMapper objectMapper = jsonMessageConverter.getObjectMapper();
                    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
                }
            });
}

Actual issue: SerializationFeature.WRITE_DATES_AS_TIMESTAMPS value is not read from spring configuration file which need to be set false in order 
to covert long value while serialisation.

4reactions
Dienrycommented, Jan 3, 2018

Where do you put this in your code?

ObjectMapper mapper = new ObjectMapper() .registerModule(new ParameterNamesModule()) .registerModule(new Jdk8Module()) .registerModule(new JavaTimeModule()) .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

json - Jackson SerializationFeature ...
I'm not sure if there is an easier way to access the Jackson MVC message converter and configure ... WRITE_DATES_AS_TIMESTAMPS is disabled.
Read more >
Date is always serialised as long | by Valery Yakovlev - Medium
I tried to temporarily remove this configuration and run the app. The dates were serialised properly in the ISO standard, as expected.
Read more >
Jackson Date - Baeldung
This problem occurs because JSON doesn't natively have a date format, so it represents dates as String. The String representation of a date...
Read more >
SerializationFeature (jackson-databind 2.6.0 API) - FasterXML
Feature that determines whether Date s (and sub-types) used as Map keys are serialized as timestamps or not (if not, will be serialized...
Read more >
Jackson and SerializationFeature ...
Hi,. is there a deeper reason why the SerializationFeature WRITE_DATES_AS_TIMESTAMPS is not disabled by default? Currently, a LocalDate is ...
Read more >

github_iconTop Related Medium Post

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