one Jackson deserializer for multiple types (config by annotation)

I'm trying to change the (de)serialization of a list in one of my classes.

the objects in the list shall be serialised as int (their jpa id) and deserialised accordingly. serialization is simple.

for the deserialization i have a class that can translate the id into the object if id and class are known.

How do i get the necessary class from jackson? all default jackson serialisers have a constructor like this: protected StdDeserialiser(Class<?> vc) so the information is present somewhere.

is there a way to access it during deserialisation? or before the deserialiser is constructed by jackson? or inside the HandlerInstantiator?

I only want to overwrite the default deseriliser for certain references so i can't just write a provider or a custom module.

Answers


I made it work from inside the deserializer with the help of the ContextDeserializer interface as this supplies the deserializer with the target property.

public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException {
    Class<?> vc = null;
    if (property.getType().isCollectionLikeType()) {
        vc = property.getType().getContentType().getRawClass();
    } else {
        vc = property.getType().getRawClass();
    }
    return new ResourcePathDeserializer(vc, converter);
}

This solution is not perfect as I only get the raw class of the return type or the generic (which might be a parent class or an interface) but that is enough for my requirements. It would be better if I could access the "real" class that was resolved by Jackson, but for me this works.


Need Your Help

Trying to expire of a cookie within 3 minutes

jquery cookies

I'm trying to setup a cookie for my website that will be expire within 3 Minutes but no luck with that... so I found a code for expiration within 30 days that work perfect but I'm having hard time ...