I'm attempting to read a JSON file using Jackson and store one of the fields that is stored as a epoch milliseconds as a Java Instant
, however deserialization is not behaving as expected.
Here is what I am seeing when trying to read the timestamp:
1503115200000
Jackson is setting the Instant
field as +49601-10-28T16:00:00Z
.
This appears to be occurring because Jackson's default is to read the timestamp with Instant.ofEpochSecond(Long l)
instead of Instant.ofEpochMilli(Long l)
.
Is there a way to set the Jackson ObjectMapper
to use the ofEpochMilli
method instead? This is what I currently have for my ObjectMapper
:
ObjectMapper om = new ObjectMapper()
.registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setSerializationInclusion(Include.NON_NULL);
Note
If I change the input JSON to ISO date such as 2017-08-19T04:00:00Z
or to epoch seconds such as 1503115200
the Instant
field is able to set properly.
Unfortunately the JSON input must be epoch milliseconds e.g. 1503115200000
.
Solution was to add .configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
to the ObjectMapper. Complete ObjectMapper looks like:
ObjectMapper om = new ObjectMapper()
.registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
.setSerializationInclusion(Include.NON_NULL);