Java:具有接口属性的对象的Jackson 多态JSON 反序列化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21485923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Hymanson polymorphic JSON deserialization of an object with an interface property?
提问by Shaun Scovil
I am using Hymanson's ObjectMapperto deserialize a JSON representation of an object that contains an interface as one of its properties. A simplified version of the code can be seen here:
我正在使用 HymansonObjectMapper反序列化一个对象的 JSON 表示,该对象包含一个接口作为其属性之一。代码的简化版本可以在这里看到:
https://gist.github.com/sscovil/8735923
https://gist.github.com/sscovil/8735923
Basically, I have a class Assetwith two properties: typeand properties. The JSON model looks like this:
基本上,我有一个Asset具有两个属性的类:type和properties. JSON 模型如下所示:
{
"type": "document",
"properties": {
"source": "foo",
"proxy": "bar"
}
}
The propertiesproperty is defined as an interface called AssetProperties, and I have several classes that implement it (e.g. DocumentAssetProperties, ImageAssetProperties). The idea is that image files have different properties (height, width) than document files, etc.
该properties属性被定义为一个名为 的接口AssetProperties,我有几个实现它的类(例如DocumentAssetProperties,ImageAssetProperties)。这个想法是图像文件与文档文件等具有不同的属性(高度、宽度)。
I've worked off of the examples in this article, read through docs and questions here on SO and beyond, and experimented with different configurations in the @JsonTypeInfoannotation parameters, but haven't been able to crack this nut. Any help would be greatly appreciated.
我在工作过的例子这篇文章,通读文档和问题,这里SO和超越,并在不同的配置试验@JsonTypeInfo标注的参数,但一直没能破解这个螺母。任何帮助将不胜感激。
Most recently, the exception I'm getting is this:
最近,我得到的例外是这样的:
java.lang.AssertionError: Could not deserialize JSON.
...
Caused by: org.codehaus.Hymanson.map.JsonMappingException: Could not resolve type id 'source' into a subtype of [simple type, class AssetProperties]
Thanks in advance!
提前致谢!
SOLUTION:
解决方案:
With many thanks to @Micha? Ziober, I was able to resolve this issue. I was also able to use an Enum as a type id, which took a bit of Googling. Here is an updated Gist with working code:
非常感谢@Micha?Ziober,我能够解决这个问题。我还可以使用 Enum 作为类型 id,这需要一些谷歌搜索。这是带有工作代码的更新要点:
采纳答案by Micha? Ziober
You should use JsonTypeInfo.As.EXTERNAL_PROPERTYinstead of JsonTypeInfo.As.PROPERTY. In this scenario your Assetclass should look like this:
您应该使用JsonTypeInfo.As.EXTERNAL_PROPERTY而不是JsonTypeInfo.As.PROPERTY. 在这种情况下,您的Asset类应如下所示:
class Asset {
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = ImageAssetProperties.class, name = "image"),
@JsonSubTypes.Type(value = DocumentAssetProperties.class, name = "document") })
private AssetProperties properties;
public AssetProperties getProperties() {
return properties;
}
public void setProperties(AssetProperties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "Asset [properties("+properties.getClass().getSimpleName()+")=" + properties + "]";
}
}
See also my answer in this question: Hymanson JsonTypeInfo.As.EXTERNAL_PROPERTY doesn't work as expected.
另请参阅我在此问题中的回答:Hymanson JsonTypeInfo.As.EXTERNAL_PROPERTY 无法正常工作。

