FasterXML / FasterXML/jackson-modules-java8

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

Aperta
#374 31 commenti 1 reazione 0 assegnatari Vedi su GitHub
date-time-config
Lingua principale
Java
Stelle
425
Fork
124
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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));
}
}
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.