jackson 2.0 joda datetime serialization/deserialization (0.6.0-SNAPSHOT)

3,927 views
Skip to first unread message

David Morgantini

unread,
Nov 20, 2012, 5:14:00 AM11/20/12
to dropwiz...@googlegroups.com
Hey,

I'm testing the 0.6.0-SNAPSHOT and I'm running into issues with joda time serialization.  In 0.5.1, datetimes serialized as milliseconds (and worked just fine), now they serialize as fully fledged objects with every little detail of the time involved.  This now leads to issues with deserialization:

Caused by: ! com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.joda.time.Chronology, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
 at [Source: java.util.zip.GZIPInputStream@2947640e; line: 1, column: 376] (through reference chain: uk.gov.ida.hub.shared.dto.IdentityProviderMetadataDto["expires"]->org.joda.time.DateTime["chronology"]).

Any ideas?

Thanks,

David

0.6.0-SNAPSHOT serialization:

expires
{
  • year2012,
  • era1,
  • dayOfYear325,
  • dayOfWeek2,
  • dayOfMonth20,
  • weekOfWeekyear47,
  • weekyear2012,
  • monthOfYear11,
  • yearOfEra2012,
  • yearOfCentury12,
  • centuryOfEra20,
  • millisOfSecond275,
  • millisOfDay35220275,
  • secondOfMinute0,
  • secondOfDay35220,
  • minuteOfHour47,
  • minuteOfDay587,
  • hourOfDay9,
  • zone
    {
    • fixedtrue,
    • id"UTC"
    },
  • millis1353404820275,
  • chronology
    {
    • zone
      {
      • fixedtrue,
      • id"UTC"
      }
    },
  • beforeNowfalse,
  • afterNowtrue,
  • equalNowfalse
},

0.5.1 serialization:

expires1353406674345,

David Morgantini

unread,
Nov 20, 2012, 6:04:15 AM11/20/12
to dropwiz...@googlegroups.com
I've solved this problem using this gist https://gist.github.com/4117302 but I'm pretty sure that this isn't the right way to handle this - or that dropwizard should perhaps handle this out of the box.

Derek Stainer

unread,
Nov 20, 2012, 11:07:06 AM11/20/12
to dropwiz...@googlegroups.com
Hi David-

You are on the right track... Except you don't have to build any of the JodaDateTime serialization. The Joda module can be found here: 


Once you have it as a dependency you can register the module as well as the format that you want like so:

In the initialize method (in 0.6.0-SNAPSHOT)

bootstrap.getObjectMapperFactory().registerModule(new JodaModule());
bootstrap.getObjectMapperFactory().setDateFormat(new ISO8601DateFormat());

We use the ISO8601DateFormat object for the DateFormat but you can provide your own if you want by extending the DateFormat class itself.

Hopefully this helps.
Derek

David Morgantini

unread,
Nov 20, 2012, 3:19:23 PM11/20/12
to dropwiz...@googlegroups.com
Cool thanks.

Coda Hale

unread,
Nov 20, 2012, 7:30:56 PM11/20/12
to dropwiz...@googlegroups.com
You know, enough services at Yammer use Joda Time that it's worth adding first-class support for it.

0.6.0-SNAPSHOT now includes Joda Time 2.1, with Jackson support, Hibernate support, and DateTimeParam.

---
Coda Hale
http://codahale.com


Diego Magalhães

unread,
Nov 20, 2012, 9:04:45 PM11/20/12
to dropwiz...@googlegroups.com
Great to hear Coda, does 0.6.0 includes latest slf4j too?

Diego Magalhães
http://diegomagalhaes.com
claro @ +55 21 9411 2823

Coda Hale

unread,
Nov 20, 2012, 9:55:10 PM11/20/12
to dropwiz...@googlegroups.com
Yes.


---
Coda Hale
http://codahale.com



Reply all
Reply to author
Forward
0 new messages