第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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

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

撒科打諢 2023-02-16 17:21:49
換句話說,可以實(shí)現(xiàn)的繼承深度是有限制的。目前我的深度為 2,祖父母 -> 父母 -> 孩子,我遇到了一個(gè)問題,杰克遜可以反序列化到父母,然后拋出一個(gè) UnrecognizedPropertyException. 這是正確的,但是子類確實(shí)擁有該屬性,我相信我已經(jīng)為 Jackson 添加了正確的類型信息以反序列化子類。此測(cè)試顯示問題: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 回答

?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊

以下是如何定義具有多個(gè)級(jí)別的繼承:


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


  {

      "list":[{

          "type": "child",

          "someProperty": "foobar"

      }]

  }

繼承樹是:


GrandParent

  Parent

    Child(someProperty:String)

您必須在頂層定義您的“類型”屬性,@JsonTypeInfo(...) 您可以在子級(jí)別上重復(fù)它,但如果您只序列化/反序列化祖父母則不需要。然后在每個(gè)父級(jí)(類 Parent 和 GrandParent)上定義子類型,就像您對(duì) @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;

    }




}

你做的錯(cuò)誤:

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

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

側(cè)節(jié)點(diǎn):來自 junit5,它與來自 junit4 的Assertions功能相同Assert


查看完整回答
反對(duì) 回復(fù) 2023-02-16
?
慕慕森

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊

在 ObjectMapper 對(duì)象上設(shè)置配置:

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);


查看完整回答
反對(duì) 回復(fù) 2023-02-16
  • 2 回答
  • 0 關(guān)注
  • 168 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)