FasterXML / FasterXML/jackson-modules-java8

`WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS`/`READ_DATE_TIMESTAMPS_AS_NANOSECONDS` only work on numeric timestamps

Open
#374 31 comments 1 reaction 0 assignees View on GitHub
date-time-config
Dominant language
Java
Stars
425
Forks
124
PR merge metrics
No merged PRs in 30d

Description

Hello.

I'm using Spring Boot `3.5.6` with Java `25` and I need a way to have Jackson to serialize/deserialize date-time values up to milliseconds precision and not higher (nanoseconds, actually).

I tried setting `write-date-timestamps-as-nanoseconds` and `read-date-timestamps-as-nanoseconds` to `false`, but it looks like they're simply ignored if I don't have `write-dates-as-timestamps` to `true`.
Indeed, I would like to have my JSON contain a date-time in the ISO standard format and not as a long value, but I wasn't able to find anything in the documentation for this.
I wonder why such a feature is provided only for numeric timestamps. It should work for both IMHO.

Below a small test case to reproduce the issue:
```java
@SpringBootTest(
webEnvironment = MOCK,
properties = {
//"spring.jackson.serialization.write-dates-as-timestamps=true",
"spring.jackson.serialization.write-date-timestamps-as-nanoseconds=false",
"spring.jackson.deserialization.read-date-timestamps-as-nanoseconds=false"
}
)
public class ObjectMapperTest {
@Autowired
private ObjectMapper objectMapper;

@Test
public void shouldHandleDateTimeUpToMillis() throws JsonProcessingException {
// given
Instant instant = Instant.now().with(NANO_OF_SECOND, 1);
assertThat(instant.getNano()).isNotZero();

// when
String serialized = objectMapper.writeValueAsString(instant);

// then
Instant deserialized = objectMapper.readValue(serialized, Instant.class);
assertThat(deserialized).isEqualTo(instant.truncatedTo(MILLIS));
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.