Java: Jackson polymorphic JSON deserialization of an object with an interface property?(Java:具有接口属性的对象的杰克逊多态 JSON 反序列化?)
问题描述
我正在使用 Jackson 的 ObjectMapper
反序列化包含接口作为其属性之一的对象的 JSON 表示.代码的简化版本可以在这里看到:
I am using Jackson's ObjectMapper
to 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:
https://gist.github.com/sscovil/8735923
基本上,我有一个类 Asset
有两个属性:type
和 properties
.JSON 模型如下所示:
Basically, I have a class Asset
with two properties: type
and properties
. The JSON model looks like this:
{
"type": "document",
"properties": {
"source": "foo",
"proxy": "bar"
}
}
properties
属性被定义为一个名为AssetProperties
的接口,我有几个实现它的类(例如DocumentAssetProperties
、ImageAssetProperties
).这个想法是图像文件具有与文档文件等不同的属性(高度、宽度).
The properties
property 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.
我已经完成了这篇文章中的示例,阅读有关 SO 及其他内容的文档和问题,并在 @JsonTypeInfo
注释参数中尝试不同的配置,但无法破解这个难题.任何帮助将不胜感激.
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 @JsonTypeInfo
annotation parameters, but haven't been able to crack this nut. Any help would be greatly appreciated.
最近,我得到的例外是:
Most recently, the exception I'm getting is this:
java.lang.AssertionError: Could not deserialize JSON.
...
Caused by: org.codehaus.jackson.map.JsonMappingException: Could not resolve type id 'source' into a subtype of [simple type, class AssetProperties]
提前致谢!
解决方案:
非常感谢@Michał Ziober,我能够解决这个问题.我还可以使用 Enum 作为类型 id,这需要一些谷歌搜索.这是一个更新的 Gist 和工作代码:
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:
https://gist.github.com/sscovil/8788339
推荐答案
你应该使用 JsonTypeInfo.As.EXTERNAL_PROPERTY
而不是 JsonTypeInfo.As.PROPERTY
.在这种情况下,您的 Asset
类应如下所示:
You should use JsonTypeInfo.As.EXTERNAL_PROPERTY
instead of JsonTypeInfo.As.PROPERTY
. In this scenario your Asset
class should look like this:
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 + "]";
}
}
另请参阅我在此问题中的回答:JacksonJsonTypeInfo.As.EXTERNAL_PROPERTY 没有按预期工作.
See also my answer in this question: Jackson JsonTypeInfo.As.EXTERNAL_PROPERTY doesn't work as expected.
这篇关于Java:具有接口属性的对象的杰克逊多态 JSON 反序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java:具有接口属性的对象的杰克逊多态 JSON 反序列化?
基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01