杰克逊多态反序列化 [英] Jackson polymorphic deserialization
问题描述
我遇到了Jackson和类型层次结构的以下问题。我正在序列化一个 SubA
类,它将 Base
扩展为 String
,
然后尝试将其退回去。当然在编译时,系统不知道它是否是
Base
或 SubA
所以我我期待一个 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
.
我的基本
类看起来像:
@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
}
...以及一个来自<$的类c $ c>基数:
... 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:
- Json deserialization into another class hierarchy using Jackson
- http://wiki.fasterxml.com/JacksonPolymorphicDeserialization
推荐答案
您的代码看起来正确用例。一个可能的问题是你可能会意外地使用杰克逊2注释与杰克逊1 ObjectMapper
(我可以看到后者是杰克逊来自包名称的例外)。注释和映射器的版本必须匹配;否则注释将被忽略,这将解释您所看到的问题。
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.
这篇关于杰克逊多态反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!