简体   繁体   中英

How to json with circular references?

I have the following class construct creating a circular dependency. In general, Jackson library should be able to handle these circular dependencies.

I'm looking for a way to not having to use annotations on every class that has circularities, but somehow configure it generally in the ObjectMapper .

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
abstract class Shape;

class Line extends Shape {
    //a line can only connect 2 circles
    Circle from, to;
}

class Circle extends Shape {
    // a circle can have many lines connected
    List<Line> lines;
}

I then serialize a list, which contains both circles and lines :

List<Shape> shapes;

    ObjectMapper om = new ObjectMapper().setDefaultTyping();

It is possible to configure the id generation globally on the mapper?

不能。由于并非所有类型都可以具有ID(仅POJO类型(即CollectionMap和数组已出)),并且由于详细信息可能有所不同,因此与类型ID不同,没有“默认ID”设置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM