亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

杰克遜反序列化是否有最大的繼承深度?

杰克遜反序列化是否有最大的繼承深度?

撒科打諢 2023-02-16 17:21:49
換句話說,可以實現的繼承深度是有限制的。目前我的深度為 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);    }
查看完整描述

2 回答

?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

以下是如何定義具有多個級別的繼承:


您想要反序列化最終類型為“child”的 GrandParent 列表


  {

      "list":[{

          "type": "child",

          "someProperty": "foobar"

      }]

  }

繼承樹是:


GrandParent

  Parent

    Child(someProperty:String)

您必須在頂層定義您的“類型”屬性,@JsonTypeInfo(...) 您可以在子級別上重復它,但如果您只序列化/反序列化祖父母則不需要。然后在每個父級(類 Parent 和 GrandParent)上定義子類型,就像您對 @JsonSubTypes 所做的那樣。


代碼


public class JacksonInheritanceTest2 {


    @Test

    public void deserializeChildrenAsGrandParentList() throws IOException {

        ObjectMapper mapper = new ObjectMapper();

        String grandparentsJson = "{" +

                "\"list\":[{" +

                "\"type\": \"child\"," +

                "\"someProperty\": \"foobar\"" +

                "}]" +

                "}";

        GrandParentList grandparents = mapper.readValue(grandparentsJson, GrandParentList.class);

        Assertions.assertNotNull(grandparents);

    }



}


class GrandParentList {

    @JsonProperty

    List<GrandParent> list;

}


@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", visible = true)

@JsonSubTypes({

        @JsonSubTypes.Type(value = Parent.class,name = "parent"),

        //@JsonSubTypes.Type(value = Child.class, name = "child")

})

class GrandParent {

    @JsonProperty("type")

    private String type;


}



//@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", visible = true)

@JsonSubTypes({

        @JsonSubTypes.Type(value = Child.class, name = "child")

})

class Parent extends GrandParent {

    @JsonProperty

    private String value;


}


@JsonSubTypes({

    @JsonSubTypes.Type(value = Child.class, name = "child")

})

class Child extends Parent {

    @JsonProperty

    private String someProperty;


    public String getSomeProperty() {

        return someProperty;

    }


    public void setSomeProperty(String someProperty) {

        this.someProperty = someProperty;

    }




}

你做的錯誤:

  • 定義許多屬性類型名稱,每個屬性類型名稱用于一個父級:您選擇您的屬性類型名稱并僅使用一個。

  • 在 Json 中,您確實在抽象中設置了父級的類型名稱:只有葉類型很重要,樹的其余部分被扣除。

側節點:來自 junit5,它與來自 junit4 的Assertions功能相同Assert


查看完整回答
反對 回復 2023-02-16
?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

在 ObjectMapper 對象上設置配置:

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);


查看完整回答
反對 回復 2023-02-16
  • 2 回答
  • 0 關注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號