Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson: JsonIdentityInfo serialises depth of only one child

For some reason JsonIdentityInfo serialises the depth of one child but not the other. My example:

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "id")
class A {
  private long id;
  private B last;

  // Getters, setters...
}

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "id")
class B {
  private long id;
  private A a;
  private C c1;
  private C c2;

  // Getters, setters...
}

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "id")
class C {
  private long id;
  private Set<A> as;
  private B last;

  // Getters, setters...
}

I serialise object B it serialises child A a to some depth, serialises C c1 to few levels deep. But C c2 only gets the reference.

I want A a; C c1; C c2; serialised only to first depth or also include c2 regardless of depth.

like image 626
Sterling Duchess Avatar asked Sep 15 '25 23:09

Sterling Duchess


1 Answers

Just use @JsonUnwrapped annotation on the property c1 and c2 in class B. i.e.

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

class B {
  private long id;
  private A a;
  @JsonUnwrapped
  private C c1;
  @JsonUnwrapped
  private C c2;

  // Getters, setters...
}
like image 174
Vinay Prajapati Avatar answered Sep 17 '25 14:09

Vinay Prajapati