【发布时间】:2019-06-24 09:46:01
【问题描述】:
换句话说,可以实现的继承深度是否存在限制。
目前我的深度为2,祖父母->父母->孩子,我遇到了一个问题,杰克逊可以反序列化到父母然后抛出UnrecognizedPropertyException。这是正确的,但是子类确实拥有该属性,我相信我已经为杰克逊添加了正确的类型信息来反序列化子类。
此测试显示问题:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Value;
import lombok.experimental.SuperBuilder;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
public class JacksonInheritanceTest {
@Test
public void deserializeChildrenAsGrandParentList() throws IOException {
ObjectMapper mapper = new ObjectMapper();
String grandparentsJson = "{" +
"\"list\":[{" +
"\"type\": \"parent\"," +
"\"value\": \"child\"," +
"\"someProperty\": \"foobar\"" +
"}]" +
"}";
GrandParentList grandparents = mapper.readValue(grandparentsJson, GrandParentList.class);
Assert.assertNotNull(grandparents);
}
@Test
public void deserializeParentAsGrandParent() throws IOException {
ObjectMapper mapper = new ObjectMapper();
String parentJson = "{" +
"\"type\": \"parent\"," +
"\"value\": \"child\"" +
"}";
GrandParent grandparent = mapper.readValue(parentJson, GrandParent.class);
Assert.assertNotNull(grandparent);
}
@Test
public void deserializeChildAsGrandParent() throws IOException {
ObjectMapper mapper = new ObjectMapper();
String grandparentJson = "{" +
"\"type\": \"parent\"," +
"\"value\": \"child\"," +
"\"someProperty\": \"foobar\"" +
"}";
GrandParent grandparent = mapper.readValue(grandparentJson, GrandParent.class);
Assert.assertNotNull(grandparent);
}
@Test
public void deserializeChildAsParent() throws IOException {
ObjectMapper mapper = new ObjectMapper();
String childJson = "{" +
"\"type\": \"parent\"," +
"\"value\": \"child\"," +
"\"someProperty\": \"foobar\"" +
"}";
Parent parent = mapper.readValue(childJson, Parent.class);
Assert.assertNotNull(parent);
}
@Test
public void deserializeAsChild() throws IOException {
ObjectMapper mapper = new ObjectMapper();
String child1 = "{" +
"\"type\": \"parent\"," +
"\"value\": \"child\"," +
"\"someProperty\": \"foobar\"" +
"}";
Child child = mapper.readValue(child1, Child.class);
Assert.assertNotNull(child);
}
}
class GrandParentList {
@JsonProperty
List<GrandParent> list;
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = Parent.class,
name = "parent")
})
@Getter
@SuperBuilder
@JsonDeserialize(builder = GrandParent.GrandParentBuilderImpl.class)
class GrandParent {
@JsonProperty("type")
private String type;
@JsonPOJOBuilder(withPrefix = "")
static final class GrandParentBuilderImpl extends GrandParentBuilder<GrandParent, GrandParent.GrandParentBuilderImpl> {
}
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "value", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = Child.class, name = "child")
})
@Getter
@SuperBuilder
@JsonDeserialize(builder = Parent.ParentBuilderImpl.class)
class Parent extends GrandParent {
@JsonProperty
private String value;
@JsonPOJOBuilder(withPrefix = "")
static final class ParentBuilderImpl extends ParentBuilder<Parent, ParentBuilderImpl> {
}
}
@EqualsAndHashCode(callSuper = true)
@Value
@SuperBuilder
@JsonDeserialize(builder = Child.ChildBuilderImpl.class)
class Child extends Parent {
@JsonProperty
private String someProperty;
@JsonPOJOBuilder(withPrefix = "")
static final class ChildBuilderImpl extends ChildBuilder<Child, ChildBuilderImpl> {
}
}
【问题讨论】:
-
如果不做超过 2 个就会有问题,如果超过 4 个就会有问题。
-
这个例子是完整的,但不是最小的。到目前为止我知道没有任何限制,在我看来,您没有将“孩子”定义为祖父母或父母的子类,并且孩子没有用 jsontype 进行注释。更多 jsonTypeName 示例:stackoverflow.com/questions/56395909/…