Jackson多态反序列化

Jackson polymorphic deserialization(Jackson多态反序列化)

本文介绍了Jackson多态反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jackson 和类型层次结构存在以下问题.我正在序列化一个将 Base 扩展为 String 的类 SubA,然后尝试反序列化它.当然在编译的时候,系统不知道会不会BaseSubA 所以我期待一个 Base 并且之后会做一些其他操作,如果它是一个 SubA.

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.

我的 Base 类看起来像:

@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
}

... 以及从 Base 派生的类:

... 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);

字符串 requestStringSend 是:

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

但是我一遍又一遍地遇到同样的错误.映射器现在知道如何处理 anotherClass 参数 - 它在 Base 中不存在.但我认为映射器会将其转换为 SubA 类?

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:

  • 使用 Jackson 将 Json 反序列化为另一个类层次结构
  • http://wiki.fasterxml.com/JacksonPolymorphicDeserialization

推荐答案

您的代码在用例中看起来是正确的.一个可能的问题是,您可能会不小心将 Jackson 2 注释与 Jackson 1 ObjectMapper 一起使用(我可以从异常中的包名称中看到后者是 Jackson).注解和映射器的版本必须匹配;否则注释将被忽略,这将解释您看到的问题.

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.

这篇关于Jackson多态反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Jackson多态反序列化

基础教程推荐