Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom deserializer to interface using jackson

Saying I have an interface A, I want to use custom deserializer for all classes implement interface A, So I use code below but it doesn't work, While CustomAserializer works. So what should I do to deserialize all classes implement A using my custom deserializer. Thanks.

module.addDeserializer(A.class, new CustomADeserializer());
module.addSerializer(A.class, new CustomASerializer())
like image 894
dustdn Avatar asked Aug 19 '14 15:08

dustdn


People also ask

Does Jackson serialize getters?

Jackson doesn't (by default) care about fields. It will simply serialize everything provided by getters and deserialize everything with a matching setter.

What is Jackson deserialization?

Jackson is a powerful and efficient Java library that handles the serialization and deserialization of Java objects and their JSON representations. It's one of the most widely used libraries for this task, and runs under the hood of many other frameworks.


1 Answers

Just in case someone need a solution to serialize and desiralize inheritance hierarchy

you can use jackson annotation in more elegant way : JsonTypeInfo and JsonSubTypes

 @JsonTypeInfo(
  use = JsonTypeInfo.Id.NAME, 
  include = JsonTypeInfo.As.PROPERTY, 
  property = "type")
@JsonSubTypes({ 
  @Type(value = ServiceUser.class, name = "service"), 
  @Type(value = AdminUser.class, name = "admin") 
})
public interface User{
    // ...
}
like image 196
MajdiBO Avatar answered Sep 18 '22 01:09

MajdiBO