Custom Jackson Deserializer

1,813 views
Skip to first unread message

Simone Hinterseher

unread,
Jun 11, 2012, 8:25:34 PM6/11/12
to dropwiz...@googlegroups.com
I'd like to add a custom deserializer to my service. I can find the Json and JacksonMessageBodyProvider that might be related to this, but I still have no clue how I can add a custom deserializer to my service. Can somebody maybe give me a small example?
Thanks!!

Simone Hinterseher

unread,
Jun 11, 2012, 8:31:29 PM6/11/12
to dropwiz...@googlegroups.com
Also, I noted that my service cannot serialize/deserialize Guava Optional.
However, once there existed a class GuavaDeserializers within dropwizard. Is there still a custom serializer/deserializer for Guava classes?

Christopher Elkins

unread,
Jun 11, 2012, 9:01:05 PM6/11/12
to dropwiz...@googlegroups.com
On Jun 11, 2012, at 5:25 PM, Simone Hinterseher wrote:

I'd like to add a custom deserializer to my service. I can find the Json and JacksonMessageBodyProvider that might be related to this, but I still have no clue how I can add a custom deserializer to my service. Can somebody maybe give me a small example?
Thanks!!

You need to bundle your custom deserializer in a Jackson module:

Note especially the "Simple modules" section, where you can see how to avoid writing a bunch of boiler-plate code if your needs are basic.

Then, add the module in the constructor of your Dropwizard service:

    private HelloWorldService() {
        super("hello-world");
        addJacksonModule(new MyModule());
    }

or the simple way:

    private HelloWorldService() {
        super("hello-world");

SimpleModule module = new SimpleModule("MyModule", new Version(0, 1, 0, "alpha"));
        module.addDeserializer(MyBean.class, new MyCustomSerializer());
        addJacksonModule(module);
    }


On Jun 11, 2012, at 5:31 PM, Simone Hinterseher wrote:

Also, I noted that my service cannot serialize/deserialize Guava Optional.
However, once there existed a class GuavaDeserializers within dropwizard. Is there still a custom serializer/deserializer for Guava classes?

It was apparently lost in the switch to the official Jackson library for Guava:

It recently re-materialized, but unfortunately only for Jackson 2.0:

However, there's no reason you can't resurrect the old Dropwizard code in your own bundle.

-- 
Christopher Elkins

Tatu Saloranta

unread,
Jun 12, 2012, 2:03:24 AM6/12/12
to dropwiz...@googlegroups.com
There is actually 1.9 branch for jackson-datatype-guava, and published
artifact in Maven repo.
Although new code is mostly added for 2.0 version... but if anyone
wants to help maintain 1.9 version I can help (it's just matter of
time & resources -- but Jackson 1.9 will obviously be maintained for
quite some time still).

-+ Tatu +-

Christopher Elkins

unread,
Jun 12, 2012, 1:59:25 PM6/12/12
to dropwiz...@googlegroups.com
On Jun 11, 2012, at 11:03 PM, Tatu Saloranta wrote:

>> On Jun 11, 2012, at 5:31 PM, Simone Hinterseher wrote:
>>
>> Also, I noted that my service cannot serialize/deserialize Guava Optional.
>> However, once there existed a class GuavaDeserializers within dropwizard. Is
>> there still a custom serializer/deserializer for Guava classes?
>>
>>
>> It was apparently lost in the switch to the official Jackson library for
>> Guava:
>> https://github.com/codahale/dropwizard/commit/d358be05ab16cc13668661d21fef4e16109f8684
>>
>> It recently re-materialized, but unfortunately only for Jackson 2.0:
>> https://github.com/FasterXML/jackson-datatype-guava/commit/4c6ce933938b2029bf378728deebee245adef299
>>
>> However, there's no reason you can't resurrect the old Dropwizard code in
>> your own bundle.
>
> There is actually 1.9 branch for jackson-datatype-guava, and published
> artifact in Maven repo.
> Although new code is mostly added for 2.0 version... but if anyone
> wants to help maintain 1.9 version I can help (it's just matter of
> time & resources -- but Jackson 1.9 will obviously be maintained for
> quite some time still).


Good idea:
https://github.com/FasterXML/jackson-datatype-guava/pull/5

--
Christopher Elkins

Tatu Saloranta

unread,
Jun 12, 2012, 2:11:21 PM6/12/12
to dropwiz...@googlegroups.com
Perfect, thanks! I will release this later today or this week,

-+ Tatu +-

Tatu Saloranta

unread,
Jun 13, 2012, 12:46:57 PM6/13/12
to dropwiz...@googlegroups.com
1.9.1 release of jackson-datatype-guava should now be out, with
backported support for Optional.

Thanks!

-+ Tatu +-

Maciej Łuniewski

unread,
Feb 15, 2013, 5:07:48 AM2/15/13
to dropwiz...@googlegroups.com
I tried to use your solution, but I see that the addJacksonModule() method is gone in 0.6.1.
I could not find anything how to do it in 0.6.1.
Any clue?

Thanks.
Maciej

Maciej Łuniewski

unread,
Feb 15, 2013, 5:25:35 AM2/15/13
to dropwiz...@googlegroups.com
Nevermind. Just figured out:

SimpleModule module = new SimpleModule();
module.addSerializer(KeywordSynonym.class, new KeywordSynonymSerializer());
bootstrap.getObjectMapperFactory().registerModule(module);
Reply all
Reply to author
Forward
0 new messages