Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson polymorphic deserialization

I've got the following problem with Jackson and type hierarchy. I'm serializing a class SubA which extends Base into a String, and trying afterwards to derserialize it back. Of course at compile time, the system does not know whether it will be Base or SubA so I'm expecting a Base and will do some other operations afterwards, if it is a SubA.

My Base class looks like:

@JsonTypeInfo(
  use = JsonTypeInfo.Id.NAME,
  include = JsonTypeInfo.As.PROPERTY,
  property = "type")
@JsonSubTypes({
  @Type(value = SubA.class, name = "SubA")
})
public class Base {
  protected String command; // +get +set
  protected String type; // +get +set
}

... and a class deriving from Base:

@JsonTypeName("SubA")
public class SubA extends Base {
  private AnotherClass anotherClass; // +get +set
  private String test; // +get +set
  @JsonIgnore
  @Override
  public String getType() {
    return "SubA";
  }
}

... and I'm trying to execute the following code:

ObjectMapper mapper = new ObjectMapper();
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
Base payload = new SubA(); // + setting anotherClass as well as test variables
String requestStringSend = ow.writeValueAsString(payload);
System.out.println("Sending: " + requestStringSend);
Base received = mapper.readValue(requestStringSend, Base.class);
String requestStringReceived = ow.writeValueAsString(received);
System.out.println("Received: " + requestStringReceived);

The String requestStringSend is:

Sending: {
  "command" : "myCommand",
  "type" : "SubA",
  "anotherClass" : {
    "data" : "someData"
  },
  "test" : "test123"
}

But I'm keep getting the same error over and over again. The mapper does now know what to do with the anotherClass parameter - it does not exist in Base. But I thought the mapper will convert it into an SubA class?

Exception in thread "main" org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "anotherClass" (Class com.test.Base), not marked as ignorable
  at [Source: java.io.StringReader@1256ea2; line: 4, column: 21] (through reference chain: com.test.Base["anotherClass"])
  at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
  at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
  at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:649)
  at org.codehaus.jackson.map.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:635)
  at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:1355)
  at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:717)
  at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
  at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2723)
  at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1854)
  at com.test.Foo.main(Foo.java:32)

I had a look at the following questions/resources:

  • Json deserialization into another class hierarchy using Jackson
  • http://wiki.fasterxml.com/JacksonPolymorphicDeserialization
like image 208
Manuel Avatar asked Nov 16 '25 07:11

Manuel


1 Answers

Your code looks correct for the use case. One possible problem is that you could be accidentally using Jackson 2 annotations with Jackson 1 ObjectMapper (I can see latter is Jackson from package names in exception). Version of annotations and mapper must match; otherwise annotations will be ignored, and this would explain problems you are seeing.

like image 71
StaxMan Avatar answered Nov 17 '25 19:11

StaxMan