Search code examples
javaandroidjacksonjackson-modulesjackson-databind

How do i add a Jackson custom deserialiser of a list to a module?


I have a custom deserializer

class MyListDeserialiser extends JsonDeserializer<List<MyObject>>{

@Override
public List< MyObject > deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {

 ....
    return myListOfObjects; }

}

then i need to add this to a Jackson mapper module

I tried

public class MyModule extends SimpleModule {

public MyModule() {
    super(new Version(1, 0, 0, "", null, null));
    addDeserializer(TypeFactory.defaultInstance().constructCollectionType(List.class, MyObject.class).getClass(),
            new MyListDeserialiser());
}

But that did not work.

Is TypeFactory the right way or is the problem with the deserialiser definition ?

I don't want to use annotations so this is the only other method i know. I don't want to add the functionality directly to the object mapper since i will have a lot of custom deserialisations and i want to keep all that code hidden away in a module.

Thanks in advance


Solution

  • You need to add your module to a mapper, otherwise it won't be used for (de)serialization.