使用Jackson JSON进行反序列化问题(递归对象)

时间:2017-05-05 09:33:14

标签: java serialization jackson deserialization

我使用Jackson JSON来序列化Game对象,但它会引发无限循环异常,因为Position类包含Island类作为Game类。

根据此article(第3部分),我尝试将注释@JsonManagedReference添加到Game的{​​{1}}字段和Island@JsonBackReference' Position字段。

序列化运行正常。但是,当我反序列化时抛出以下异常:

Island

如何正确序列化/反序列化Invalid type definition for type Position;: Can not bind back references as Creator parameters: type Position (reference 'defaultReference', parameter index #0) 对象? 这是我班级的片段。

游戏类

Game

玩家类

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;
   }

   // other code ignored
}

职位等级

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;
   }

   // other code ignored
}

Island 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;
   }

   // other code ignored
}

0 个答案:

没有答案