I use Jackson JSON to serialize an object Game, but it throws a loop loop exception, since the class Positioncontains the class Islandas a class Game.
In accordance with this article (part 3), I tried to add an annotation field @JsonManagedReferenceto Game Islandand @JsonBackReferenceto Position Island.
Serialization is working fine. However, the following exception occurs when I deserialize it:
Invalid type definition for type Position;: Can not bind back references as Creator parameters: type Position (reference 'defaultReference', parameter index
What can I do to serialize / deserialize an object Gamecorrectly? Here is a snippet of my classes.
Game class
public class Game {
@JsonManagedReference
private Island island;
private Player player;
@JsonCreator
public Game(@JsonProperty("island")Island island, @JsonProperty("player")Player player) {
this.island = island;
this.player = player;
}
}
Player class
public class Player {
private Position position;
private String name;
@JsonCreator
public Player(@JsonProperty("position")Position position, @JsonProperty("name")String name) {
this.position = position;
this.name = name;
}
}
Position class
public class Position {
private int row;
private int column;
@JsonBackReference
private Island island
@JsonCreator
public Position(@JsonProperty("island")Island island, @JsonProperty("row")int row, @JsonProperty("column")int column) {
this.island = island;
this.row = row;
this.column = column;
}
}
Island class
public class Island {
private final int numRows;
private final int numColumns;
@JsonCreator
public Island(@JsonProperty("numRows")int numRows, @JsonProperty("numColumns")int numColumns) {
this.numRows = numRows;
this.numColumns = numColumns;
}
}