How to serialize Joda DateTime with Jackson JSON processor?

Haywood Jablomey picture Haywood Jablomey · Jul 17, 2010 · Viewed 142.8k times · Source

How do I get Jackson to serialize my Joda DateTime object according to a simple pattern (like "dd-MM-yyyy")?

I've tried:

@JsonSerialize(using=DateTimeSerializer.class)
private final DateTime date;

I've also tried:

ObjectMapper mapper = new ObjectMapper()
    .getSerializationConfig()
    .setDateFormat(df);

Thanks!

Answer

Kimble picture Kimble · Mar 25, 2013

This has become very easy with Jackson 2.0 and the Joda module.

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

Maven dependency:

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-joda</artifactId>
  <version>2.1.1</version>
</dependency>  

Code and documentation: https://github.com/FasterXML/jackson-datatype-joda

Binaries: http://repo1.maven.org/maven2/com/fasterxml/jackson/datatype/jackson-datatype-joda/