换句话说,可以实现的继承深度是有限制的。目前我的深度为 2,祖父母 -> 父母 -> 孩子,我遇到了一个问题,杰克逊可以反序列化到父母,然后抛出一个 UnrecognizedPropertyException. 这是正确的,但是子类确实拥有该属性,我相信我已经为 Jackson 添加了正确的类型信息以反序列化子类。
此测试显示问题:
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);
}
蓝山帝景
慕慕森
相关分类