Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson - serialization of entities with birectional relationships (avoiding cycles)

I have two entities:

Parent {    Child[] children; }  and   Child {    Parent parent; } 

I'm aware about @JsonBackReference and @JsonManagedReference. They are good, if I'm serializing instances of Parent.

But I also need to transfer instances of Child and I want to have the parent field populated.

In other words:

  1. On serialization of Parent it should have children but their parent field might be empty (can be solved by using json reference annotations).
  2. On serialization of Child it should have parent with their children (but children don't have to have parent populated.

Is there a way to solve it using standard Jackson capabilities?

I.e. skip serialization of entities which were already serialized instead of marking fields eligible or non-eligible for serialization.

like image 796
Eugene Retunsky Avatar asked Apr 08 '12 17:04

Eugene Retunsky


People also ask

Does Jackson use serializable?

Note that Jackson does not use java. io. Serializable for anything: there is no real value for adding that. It gets ignored.

What is @JsonManagedReference?

The @JsonManagedReference annotation is a forward reference that includes during the serialization process whereas @JsonBackReference annotation is a backreference that omits during the serialization process. In the below example, we can implement @JsonManagedReference and @JsonBackReference annotations.

What is @JsonBackReference?

@JsonBackReference is the back part of reference – it will be omitted from serialization. The serialized Item object does not contain a reference to the User object.

What is Jackson object serialization?

Jackson is a solid and mature JSON serialization/deserialization library for Java. The ObjectMapper API provides a straightforward way to parse and generate JSON response objects with a lot of flexibility.


2 Answers

Jackson 2.0 does support full cyclic object references. See "Jackson 2.0 released" (section 'Handle Any Object Graphs, even Cyclic ones!') for an example.

Basically, you will need to use new @JsonIdentityInfo for types that require id/idref style handling. In your case this would be both Parent and Child types (if one extends the other, just add it to super type and that's fine).

like image 61
StaxMan Avatar answered Sep 21 '22 19:09

StaxMan


very handy interface implementation is provided in jackson 2 library as

@Entity @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id") public class Parent { ....  @Entity @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id") public class Child { .... 

in maven

<dependency>     <groupId>com.fasterxml.jackson.core</groupId>     <artifactId>jackson-core</artifactId>     <version>2.0.2</version> </dependency> 

@StaxMan provided a nice link to start from

like image 42
Oleksii Kyslytsyn Avatar answered Sep 17 '22 19:09

Oleksii Kyslytsyn