52

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.

Eugene Retunsky
  • 13,009
  • 4
  • 52
  • 55

2 Answers2

48

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).

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • Pretty fresh release. Requires some package moving. OK, I'll check it out. – Eugene Retunsky Apr 09 '12 at 17:49
  • 1
    Yeah, it is, big upgrade. But feature itself is big too. :-) – StaxMan Apr 09 '12 at 19:06
  • How do you deserialize using JSON2 in JavaScript? – f.khantsis Apr 21 '12 at 19:07
  • Sorry -- Jackson 2.0 is just version of library; result is just regular JSON. But as to how to resolve references, for that you would need a matching javascript library: JSON format does not have anything to explicitly help in resolving deps. – StaxMan Apr 22 '12 at 04:36
  • 1
    I have a similar problem but with no luck after trying this solution :http://stackoverflow.com/q/36809325/3300911 – raikumardipak Apr 25 '16 at 06:13
  • Any idea plz on eat cud b the problem – raikumardipak Apr 25 '16 at 06:14
  • doesn't helped with hibernate mapping – Oleg Abrazhaev Jul 15 '16 at 11:27
  • @StaxMan it will create IDs if the pojo already exists. How to avoid it ? I have created one issue regarding that. can you please help ?https://stackoverflow.com/questions/47078057/unable-to-deserialize-json-that-contain-2-objects-with-the-same-id-using-jackson – Krish Nov 03 '17 at 07:42
  • 1
    This is a solid answer, just want to add to be careful if using Lombok! Add "@EqualsAndHashCode(exclude = {"fieldInRelationship"})" to both classes or those annotations will be ignored as was happening in my case! – fernandopcg Apr 13 '22 at 15:48
32

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

Oleksii Kyslytsyn
  • 2,458
  • 2
  • 27
  • 43