Jeff M Jeff M - 1 year ago 208
JSON Question

Jackson automatic formatting of Joda DateTime to ISO 8601 format

According to, “DateTime can be automatically serialized/deserialized similar to how java.util.Date is handled.” However, I am not able to accomplish this automatic functionality. There are StackOverflow discussions related to this topic yet most involve a code-based solution, but based upon the quote above I should be able to accomplish this via simple configuration.

Per I have my configuration set so that writing dates as timestamps is false. The result is that java.util.Date types are serialized to ISO 8601 format, but org.joda.time.DateTime types are serialized to a long object representation.

My environment is this:

Jackson 2.1

Joda time 2.1

Spring 3.2

Java 1.6

My Spring configuration for the jsonMapper bean is

public ObjectMapper jsonMapper() {
ObjectMapper objectMapper = new ObjectMapper();

//Fully qualified path shows I am using latest enum

return objectMapper;

My test code snippet is this

Date d = new Date();
DateTime dt = new DateTime(d); //Joda time
Map<String, Object> link = new LinkedHashMap<String, Object>();
link.put("date", d);
link.put("createdDateTime", dt);

The resulting snippet of JSON output is this:


{"createdDateTime": {"year":2012,"dayOfMonth":24,"dayOfWeek":1,"era":1,"dayOfYear":359,"centuryOfEra":20,"yearOfEra":2012,"yearOfCentury":12,"weekyear":2012,"monthOfYear":12 *... remainder snipped for brevity*}}

My expectation is that the DateTime object should matche that of the Date object based upon the configuration. What am I doing wrong, or what am I misunderstanding? Am I reading too much into the word automatically from the Jackson documentation and the fact that a string representation was produced, albeit not ISO 8601, is producing the advertised automatic functionality?

Answer Source

I was able to get the answer to this from the Jackson user mailing list, and wanted to share with you since it is a newbie issue. From reading the Jackson Date FAQ, I did not realize that extra dependencies and registration are required, but that is the case. It is documented at the git hub project page here

Essentially, I had to add another dependency to a Jackson jar specific to the Joda data type, and then I had to register the use of that module on the object mapper. The code snippets are below.

For my Jackson Joda data type Maven dependency setup I used this:


To register the Joda serialization/deserialization feature I used this:

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