Pregunta

I'm trying to extend the com.fasterxml.jackson.databind.ser.std.MapSerializer to include some of my own defined functions to be used in serialize() method.

public class MyMapSerializer extends MapSerializer{
    //have constructors
    //override method
    @Override
     public void serialize(Map<?,?> value, JsonGenerator jgen, SerializerProvider provider){}
}

Then I add the annotation to my map as folllowing:

@JsonSerialize(using = org.entities.generator.MyMapSerializer.class)
private Map<ObjectA,ObjectB> myObject;

But when I add a breakpoint in the MapSerializer, it does not even go into it. And I tried to extend JsonSerializer>, it goes into it. Anyone knows how to use the extended MapSerializer? Thanks for the help

¿Fue útil?

Solución

I would not recommend trying to sub-class Jackson's default serializers: this is fragile.

But if you do want to do it, have a look at MapSerializer: my guess is that its createContextual() method ends up constructing a differently configured instance. You may need to override one of other methods which handled creationg of such instances; you just need those to construct instanceof MyMapSerializer instead of default MapSerializer.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top