Jackson - ignore class superclass when serializing

I have several model classes that extend LinkedHashMap<String, Object> : they define getters and setters that wrap the get and put Map methods. I'm trying to serialize instances of these classes using Jackson (with RESTEasy), but Jackson doesn't pay attention to my getters that are annotated using @JsonProperty . Instead, it only serializes the key-value pairs of the support card. I tried using @JsonAutoDetect to turn off automatic detection for all methods and fields, but that didn't change anything. Is there a way to prevent Jackson from automatically serializing the map, or should I create new model classes that do not extend LinkedHashMap<String, Object> ?

+4
source share
3 answers

I have several model classes that extend LinkedHashMap<String, Object> : they define getters and setters that wrap methods for getting and placing maps

This is a classic example of when not to use inheritance: you find that some other piece of code (like Jackson) sees your class as an instance of its superclass that is not what you want it to execute, In such cases (and in general) it is usually better to use composition rather than inheritance.

I recommend rewriting the model class so that it contains a map, rather than expanding it. You get much more control than the path, and the resulting model is less fragile. If you need to view your model as a Map , then implement the asMap method (or something similar) that displays this view.

+7
source

I agree with @skaffman's answer. But if you cannot easily change the inheritance structure, there are probably ways around this.

One possibility is that if you have an interface that defines getters / setters, you can add

 @JsonSerialize(as=MyInterface.class) @JsonDeserialize(as=MyInterface.class) 

which would make Jackson use only what is available through a specific interface.

Custom serializers / deserializers are also an option, but this is quite a bit of work.

+9
source

You can implement your own org.codehaus.jackson.map.DeserializerProvider , which extends Jackson org.codehaus.jackson.map.deser.StdDeserializerProvider and _createDeserializer method:

 import org.codehaus.jackson.map.SerializerProvider; import org.codehaus.jackson.map.deser.StdDeserializerProvider; import org.codehaus.jackson.map.DeserializationConfig; ... class MyDeserializerProvider extends StdDeserializerProvider { @Override protected JsonDeserializer<Object> _createDeserializer(DeserializationConfig config, JavaType type, BeanProperty property) throws JsonMappingException { if (type.isMapLikeType()) { // (1) return this._factory.createBeanDeserializer(config, this, type, property); } else { return super._createDeserializer(config, type, property); } } } 

(1) use an if condition appropriate to your needs

The custom deserializer is registered directly with ObjectMapper:

 ObjectMapper om = new ObjectMapper(); om.setDeserializerProvider(new MyDeserializerProvider()); 

I tested this with Jackson 1.9.11.

+2
source

Source: https://habr.com/ru/post/1389148/


All Articles