簡體   English   中英

“無法識別的領域”傑克遜

[英]“Unrecognized field” Jackson

我正在嘗試從以下JSON代碼反序列化JSON對象:

{ 
    "bg" : { 
        "fileName" : "data/gui/mainMenuBg.jpg"
    },
    "startGameBtn" : { 
        "text" : "Start Game",
        "innerWidth" : 100,
        "innerHeight" : 50
    }
}

我要反序列化的對象看起來像這樣:

public class MainMenu extends BasicTWLGameState {
    private StateBasedGame app;

    @JsonProperty private Image bg;
    @JsonProperty private Button startGameBtn;
    // [...]
}

我為Button類的基類創建了一個混入:

public abstract class WidgetMixIn {
    // Not sure why I have to ignore only this when there are other setters that it should complain about...
    @JsonIgnore public abstract boolean setBorderSize(Border border);

    @JsonProperty("innerWidth") public abstract int getInnerWidth();
    @JsonProperty("innerHeight") public abstract int getInnerHeight();

    public abstract void setInnerSize(
            @JsonProperty("innerWidth") int width,
            @JsonProperty("innerHeight") int height);
}

Button類本身的混合:

public class ButtonMixIn {
    @JsonProperty public String text;
}

我得到的錯誤是:

ERROR:Unrecognized field "innerWidth" (Class de.matthiasmann.twl.Button), not marked as ignorable
    at [Source: data\gui\mainMenu.json; line: 7, column: 27] (through reference chain: state.MainMenu["startGameBtn"]->de.matthiasmann.twl.Button["innerWidth"])

為什么找不到在Widget混合中定義的innerWidth屬性?

干杯。

ButtonMixIn類不具有innerHeight和innerWidth JsonProperty批注。

如果ButtonMixIn類是從定義的抽象按鈕類擴展而來的,那么您將缺少ButtonMixIn擴展WidgetMixIn行。

問題是您的“ setInnerSize()”-Java bean屬性設置器一次只允許設置一個屬性。 多個屬性只能與帶注釋的構造函數一起使用。 因此,您需要將其分為兩個獨立的設置器。

對於它的價值,有一個功能增強請求,讓Jackson使用多參數設置器,但是當前版本尚不支持。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM