在下面的例子中,我有一个主要类--A及其子类 - B.两者都可以用作通用类X中的属性。
public class A
{
@JsonProperty("primary_key")
public final String primaryKey;
@JsonCreator
A(@JsonProperty("primary_key") String primaryKey)
{
this.primaryKey = primaryKey;
}
}
public class B extends A
{
@JsonProperty("secondary_key")
public final String secondaryKey;
@JsonCreator
B(@JsonProperty("primary_key") String primaryKey, @JsonProperty("secondary_key") String secondaryKey)
{
super(primaryKey);
this.secondaryKey = secondaryKey;
}
}
public class X
{
@JsonProperty("keys")
public final A keys;
@JsonCreator
X(@JsonProperty("keys") A keys)
{
this.keys = keys;
}
}
如何使用Jackson Polymorphic功能将下面给出的json正确反序列化为各自的类:
JSON A:
{ "keys" :{
"primary_key" : "abc"
}
}
JSON B:
{ "keys" : {
"primary_key" : "abc",
"secondary_key" : "xyz"
}
}
预期结果:将密钥对象映射到JSON A的A类和JSON B的B类。
请另外提出建议。
答案 0 :(得分:0)
如果我正确地进行了正确的操作,只需传递值即可,无需任何配置。我相信这就是你要找的东西:
public class Test {
private static final String JSON = "{\"keys\":{\"primary_key\":\"abc\",\"secondary_key\":\"xyz\"}}";
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
X x = mapper.readValue(JSON, X.class);
System.out.println(mapper.writeValueAsString(x));
}
}
class A {
private String primary_key;
public String getPrimary_key() {
return primary_key;
}
public void setPrimary_key(String primary_key) {
this.primary_key = primary_key;
}
}
class B extends A {
private String secondary_key;
public String getSecondary_key() {
return secondary_key;
}
public void setSecondary_key(String secondary_key) {
this.secondary_key = secondary_key;
}
}
class X {
private B keys;
public B getKeys() {
return keys;
}
public void setKeys(B keys) {
this.keys = keys;
}
}
输出将是:
{"keys":{"primary_key":"abc","secondary_key":"xyz"}}
如果这不符合您的预期,请提供其他说明,我会根据需要编辑答案。
答案 1 :(得分:0)
感觉这是一个非常常见的问题,没有简单的注释方法来解决它(或者我可能找不到):
Deserializing polymorphic types with Jackson
您可以做的一件事是将自定义反序列化器添加到对象映射器中。以下是此方法的精彩演示:https://stackoverflow.com/a/19464580/1032167
以下是与您的示例相关的演示:
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.io.IOException;
public class Main4 {
private static final String jsonA = "{ \"keys\" : { \"primary_key\" : \"abc\" } }";
private static final String jsonB =
"{ \"keys\" : { \"primary_key\" : \"abc\", \"secondary_key\" : \"xyz\" } }";
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
SimpleModule idAsRefModule = new SimpleModule("ID-to-ref");
idAsRefModule.addDeserializer(A.class, new AJsonDeserializer());
mapper.registerModule(idAsRefModule);
X tl = mapper.readValue(jsonA, X.class);
System.out.println(tl);
X t2 = mapper.readValue(jsonB, X.class);
System.out.println(t2);
}
public static class AJsonDeserializer extends JsonDeserializer<A>{
@Override
public A deserialize(JsonParser jp, DeserializationContext dc)
throws IOException {
ObjectCodec codec = jp.getCodec();
JsonNode node = codec.readTree(jp);
if (node.has("secondary_key")) {
return codec.treeToValue(node, B.class);
}
return new A(node.findValue("primary_key").asText());
}
}
public static class A
{
@JsonProperty("primary_key")
public final String primaryKey;
@JsonCreator
A(@JsonProperty("primary_key") String primaryKey)
{
this.primaryKey = primaryKey;
}
@Override
public String toString() {
return "A{" +
"primaryKey='" + primaryKey + '\'' +
'}';
}
}
public static class B extends A
{
@JsonProperty("secondary_key")
public final String secondaryKey;
@JsonCreator
B(@JsonProperty("primary_key") String primaryKey,
@JsonProperty("secondary_key") String secondaryKey)
{
super(primaryKey);
this.secondaryKey = secondaryKey;
}
@Override
public String toString() {
return "B{" +
"primaryKey='" + primaryKey + '\'' +
"secondaryKey='" + secondaryKey + '\'' +
'}';
}
}
public static class X
{
@JsonProperty("keys")
public final A keys;
@JsonCreator
X(@JsonProperty("keys") A keys)
{
this.keys = keys;
}
@Override
public String toString() {
return "X{" +
"keys=" + keys +
'}';
}
}
}
但是如果你想使用默认的反序列化程序或者在这里看看如何解决这个问题,你将不得不再创建一个超类:https://stackoverflow.com/a/18405958/1032167