杰克逊反序列化的类型与不同的对象 [英] Jackson deserialization of type with different objects
问题描述
我有一个返回布尔值或单例映射的Web服务的结果,例如
I have a result from a web service that returns either a boolean value or a singleton map, e.g.
布尔结果:
{
id: 24428,
rated: false
}
地图结果:
{
id: 78,
rated: {
value: 10
}
}
我个人可以轻松地映射这两个,但是我如何一般地做到这一点?
Individually I can map both of these easily, but how do I do it generically?
基本上我想把它映射到类如下:
Basically I want to map it to a class like:
public class Rating {
private int id;
private int rated;
...
public void setRated(?) {
// if value == false, set rated = -1;
// else decode "value" as rated
}
}
所有的多态实例都使用 @JsonTypeInfo
来根据数据中的属性进行映射,但在这种情况下我没有这个选项。
All of the polymorphic examples use @JsonTypeInfo
to map based on a property in the data, but I don't have that option in this case.
编辑
更新后的代码部分:
EDIT
The updated section of code:
@JsonProperty("rated")
public void setRating(JsonNode ratedNode) {
JsonNode valueNode = ratedNode.get("value");
// if the node doesn't exist then it's the boolean value
if (valueNode == null) {
// Use a default value
this.rating = -1;
} else {
// Convert the value to an integer
this.rating = valueNode.asInt();
}
}
推荐答案
否不,不。您不必编写自定义反序列化程序。首先使用无类型映射:
No no no. You do NOT have to write a custom deserializer. Just use "untyped" mapping first:
public class Response {
public long id;
public Object rated;
}
// OR
public class Response {
public long id;
public JsonNode rated;
}
Response r = mapper.readValue(source, Response.class);
给出布尔值
或<$的值c $ c> java.util.Map forrated(第一种方法);或者 JsonNode
在第二种情况下。
which gives value of Boolean
or java.util.Map
for "rated" (with first approach); or a JsonNode
in second case.
从那里,你可以按原样访问数据,或者更多有趣的是,转换为实际值:
From that, you can either access data as is, or, perhaps more interestingly, convert to actual value:
if (r.rated instanced Boolean) {
// handle that
} else {
ActualRated actual = mapper.convertValue(r.rated, ActualRated.class);
}
// or, if you used JsonNode, use "mapper.treeToValue(ActualRated.class)
还有其他类型的方法 - 使用创建者ActualRated(boolean),让实例从标量中的POJO,或构建。但我认为上面应该有效。
There are other kinds of approaches too -- using creator "ActualRated(boolean)", to let instance constructed either from POJO, or from scalar. But I think above should work.
这篇关于杰克逊反序列化的类型与不同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!