5

I have below 2 classes with circular reference,I didn't put getters and setters for convenience

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "i")
public class A{
    int i;
    B b1;
    B b2;
}

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "i")
public class B {
    int i;
    AI a;
}

if A.b1 and A.b2 reference to same B object, I got serialized json as below:

 {
  "i": 1,
  "b1": {
    "i": 2,
    "a": 1
  },
  "b2": 2
}

But my expected result is:

 {
  "i": 1,
  "b1": {
    "i": 2,
    "a": 1
  },
  "b2": {
    "i": 2,
    "a": 1
  }
}

I checked jackson's source code looks like jackson store id/reference for previously used object and if same reference is used by any other object it will then use id instead of serializing whole object, that is good if objects stay in same circular chain, but if they don't stay in same chain then it is weird like my example shows.

Anybody can help get my expected result with @identityinfo annotation?

marskobe
  • 3
  • 1
Shuai
  • 51
  • 3

0 Answers0