Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a jackson datatype module for JDK8 java.time?

I'm looking for a module for the new JDK8 java.time classes. I have looked through the FasterXML GitHub Project Listing and presently found none.

As I understand Jackson is still being compiled against JDK6 so can not use these classes directly and must have this built as a separate module, as was required with Joda.

I don't mind starting the project, though looking to see if any other efforts were already underway.

like image 379
Brett Ryan Avatar asked Jan 27 '14 15:01

Brett Ryan


People also ask

What is Jackson datatype JSR310 used for?

As already mentioned, Jackson-Datatype-JSR310 provides support for Java 8 Time.

Is Objectmapper thread safe?

Mapper instances are fully thread-safe provided that ALL configuration of the instance occurs before ANY read or write calls.


2 Answers

As already mentioned, Jackson-Datatype-JSR310 provides support for Java 8 Time.

Since Jackson 2.6.0 the "old" JSR310Module is deprecated. It is replaced by JavaTimeModule. Maven dependency is the same (you can find the current version in Maven Central):

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.6.0</version>
</dependency>

You have to register the module like this:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());

Or like this:

ObjectMapper mapper = new ObjectMapper(); 
mapper.findAndRegisterModules();

Or like this (since 2.10 possible):

ObjectMapper mapper = JsonMapper.builder()
        .findAndAddModules()
        .build();

Note that as of 2.6, this module does NOT support auto-registration, because of existence of legacy version, JSR310Module. Legacy version has the same functionality, but slightly different default configuration: see com.fasterxml.jackson.datatype.jsr310.JSR310Module for details.

JavaTimeModule Source at GitHub

JavaTimeModule Usage

like image 53
PhilippS Avatar answered Oct 23 '22 16:10

PhilippS


The most complete listing of datatype modules would be found from Jackson "portal" page at Github:

https://github.com/FasterXML/jackson

which does list "JSR-310: support for "Java 8 Dates". Naming is bit opaque, but it refers to the standardization process, via JSR that should be finalized to produce the new Java8 Date API (if it's not already final; process has taken a while).

Direct link is: https://github.com/FasterXML/jackson-datatype-jsr310

like image 16
StaxMan Avatar answered Oct 23 '22 14:10

StaxMan