"Unrecognized field" Jackson
I'm trying to deserialise a JSON object from this JSON code:
{
"bg" : {
"fileName" : "data/gui/mainMenuBg.jpg"
},
"startGameBtn" : {
"text" : "Start Game",
"innerWidth" : 100,
"innerHeight" : 50
}
}
The object I'm deserialising simply looks like this:
public class MainMenu extends BasicTWLGameState {
private StateBasedGame app;
@JsonProperty private Image bg;
@JsonProperty private Button startGameBtn;
// [...]
}
I created a mix-in for the base class of the Button
class:
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);
}
The mix-in for the Button
class itself:
public class ButtonMixIn {
@JsonProperty public String text;
}
The error I'm getting is:
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"])
Why can it not find the innerWidth
开发者_StackOverflow社区 property defined in the Widget
mix-in?
Cheers.
ButtonMixIn class does not have a innerHeight and innerWidth JsonProperty annotation.
If ButtonMixIn class was meant to extend from your defined abstract button class, then you are missing the ButtonMixIn extends WidgetMixIn line.
The problem is your "setInnerSize()" -- Java bean property setters only allow setting one property at a time; multiple properties can only be used with annotated constructors. So you need to split this into two separate setters.
For what it is worth, there is a feature enhacement request to let Jackson use multi-argument setters, but current versions do not support that yet.
精彩评论