Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson Polymorphic Deserialization based on Enum

I'm working with JacksonPolymorphicDeserialization, this is my code which deserializes into the proper class based in the 'type' property:

@JsonTypeInfo(       use = JsonTypeInfo.Id.NAME,       include = JsonTypeInfo.As.PROPERTY,       property = "type",     defaultImpl = Event.class,      visible = true)   @JsonSubTypes({             @Type(value = SpecialEvent1.class, name = "SPECIAL_EVENT_1"),      @Type(value = SpecialEvent2.class, name = "SPECIAL_EVENT_2"),      })   public abstract class AbstractEvent {      private String type;      public String getType() {     return type;     }      public void setType(String type) {     this.type = type;     }    } 

It's working perfectly and my json turns into the expected class according to the 'type' value.

However, I'm considering to move the 'type' property from String to Enum, this is my new code with this change:

@JsonTypeInfo(       use = JsonTypeInfo.Id.NAME,       include = JsonTypeInfo.As.PROPERTY,       property = "type",     defaultImpl = Event.class,      visible = true)   @JsonSubTypes({             @Type(value = SpecialEvent1.class, name = "SPECIAL_EVENT_1"),      @Type(value = SpecialEvent2.class, name = "SPECIAL_EVENT_2"),      })   public abstract class AbstractEvent {      private EventType type;      public EventType getType() {     return type;     }      public void setType(EventType type) {     this.type = type;     }    } 

and the Enum:

public enum EventType {     SPECIAL_EVENT_1,     SPECIAL_EVENT_2,     EVENT; } 

The problem is that this second approach is not working... any idea why??? can I use Enum here???

Thanks!

like image 981
Curro Avatar asked Apr 11 '13 09:04

Curro


People also ask

How does Jackson deserialize enum?

By default, Jackson will use the Enum name to deserialize from JSON. If we want Jackson to case-insensitively deserialize from JSON by the Enum name, we need to customize the ObjectMapper to enable the ACCEPT_CASE_INSENSITIVE_ENUMS feature.

What is polymorphic Deserialization?

A polymorphic deserialization allows a JSON payload to be deserialized into one of the known gadget classes that are documented in SubTypeValidator. java in jackson-databind in GitHub. The deserialized object is assigned to a generic base class in your object model, such as java. lang. Object or java.

How do you serialize an enum?

1.12 Serialization of Enum Constants To serialize an enum constant, ObjectOutputStream writes the value returned by the enum constant's name method. To deserialize an enum constant, ObjectInputStream reads the constant name from the stream; the deserialized constant is then obtained by calling the java. lang. Enum.

Does JSON support enum?

JSON has no enum type. The two ways of modeling an enum would be: An array, as you have currently. The array values are the elements, and the element identifiers would be represented by the array indexes of the values.


1 Answers

Fixed!

It works with jackson 2.0!!

like image 51
Curro Avatar answered Sep 21 '22 09:09

Curro