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

6

98

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.

Leviticus answered 27/1, 2014 at 15:30 Comment(0)
E
226

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

Embankment answered 25/8, 2015 at 10:56 Comment(2)
Especially helpful answer because it mentions the name change, and the extra warning about auto-regsitration.Trillby
Note that as of today the latest version of jsr310 is 2.16.1Gallia
E
17

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

Edmondedmonda answered 7/2, 2014 at 4:8 Comment(2)
Thanks, I did manage to find it immediately after posting the question.Leviticus
Note that you also have to register the modules i.e. like so: ` private static final ObjectMapper jsonMapper = new ObjectMapper().findAndRegisterModules();`Afternoon
C
9

You just need to import the jsr310 module.

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

If module auto scan is not enable in your project, you will need to register it:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JSR310Module());
Cocky answered 23/1, 2015 at 18:17 Comment(1)
For jackson version > 2.6 i would recommend: new JavaTimeModule() instead of new JSR310Module()Monorail
M
6

If you use Spring Boot 2 and depends on the spring-boot-starter-web starter, you would have as dependencies both : the deprecated (jackson-datatype-jsr310) and the actual (jackson-datatype-jdk8).

[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.0.0.M3:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.0.0.M3:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.0.pr4:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.0.pr4:compile
Moving answered 2/3, 2018 at 13:0 Comment(6)
Hi David, what the good day to read your answer, I have the same problem and I try to solve it :D I got java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonCreator$ModeInexperience
I solved my issue by only checking Include dependencies with "Provided Scope" in Intellij congiguration :DInexperience
Hello Youcef. And that is good day for me too to read you :) IntelliJ, our savoir : ) And finally how did you solve the issue ? :)Moving
Hi David, Yes, as I said I just checked Edit Configuration > Include dependencies with "Provided Scope" that solve the issue ;)Inexperience
Oh yes, Youcef : "check" for marking and not "check" for verifying :)Moving
Ah sorry, yes I mean click on the checkbox ;)Inexperience
L
2

My bad, I was looking for a jackson-datatype-jdk8 as was for jackson-datatype-jdk7, however the project was actually created under the JSR name jackson-datatype-jsr310.

Leviticus answered 27/1, 2014 at 15:35 Comment(0)
C
1

You should be able to use

ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
Chockablock answered 4/6, 2019 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.