title: Handling Jackson circular dependencies using a context stack instead of using JsonIdentityInfo, or JsonBackReference & JsonManagedReference
body: <p>I am using Jackson to serialize JSON, but my JSON clients are not Java, and don't use Jackson.</p>
  
  <p>I want to avoid circular reference serialization infinite recursion by, whenever serializing a whole bean would cause an infinite recursion, serializing the id of the bean instead of the whole bean.</p>
  
  <p>This can be accomplished by maintaining a stack of the current serialization context.</p>
  
  <p>Whenever Jackson starts to serialize a bean, it should see if the stack contains the bean.</p>
  
  <p>If the bean is already in the stack, serialize the id instead of the whole bean, thereby avoiding the infinite recursion.</p>
  
  <p>If the bean is not in the stack, push it onto the stack, and serialize the whole bean normally.</p>
  
  <p>Once Jackson has finished serializing the bean, pop the bean off the stack.</p>
  
  <p>Therefore, I want the following Java to serialize into the following JSON:</p>
  
  <p>Java:</p>
  
  <pre><code>class A {
      String id;
      B      b;
      B      c;
  }
  
  class B {
      String id;
      A      a;
  }
  
  class Main {
      public static void main(String[] args) {
          A a = new A();
          B b = new B();
          a.id = "a1";
          b.id = "b1";
          a.b = b;
          a.c = b;
          b.a = a;
          ObjectMapper m = new ObjectMapper();
          System.out.println(m.writeValueAsString(a));
          System.out.println(m.writeValueAsString(b));
      }
  }
  </code></pre>
  
  <p>JSON:</p>
  
  <pre><code>{
      "id": "a1",
      "b": {
          "id": "b1",
          "a":  "a1"
      },
      "c": {
          "id": "b1",
          "a":  "a1"
      }
  }
  
  {
      "id": "b1",
      "a": {
          "id": "a1",
          "b":  "b1"
      }
  }
  </code></pre>
  
  <p>I only know of two built-in Jackson ways to handle circular references, neither of which, if I understand them correctly, can output the above JSON:</p>
  
  <ol>
  <li><p><code>@JsonIdentityInfo</code>, which, when best setup, annotating both <code>A</code> &amp; <code>B</code> with:</p>
  
  <pre><code>@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
  </code></pre>
  
  <p>would output (with a comment indicating the problem):</p>
  
  <pre><code>{
      "id": "a1",
      "b": {
          "id": "b1",
          "a":  "a1"
      },
      "c": "b1" // problem: value of "c" is b.id instead of b with b.a = a.id
  }
  
  {
      "id": "b1",
      "a": {
          "id": "a1",
          "b":  "b1"
      }
  }
  </code></pre></li>
  <li><p><code>@JsonManagedReference</code> &amp; <code>@JsonBackReference</code>, for which I have to specify one side as forward, and the other as back.  Say I choose <code>A</code> -> <code>B</code> properties as forward, then I get:</p>
  
  <pre><code>{
      "id": "a1",
      "b": {
          "id": "b1",
          "a":  "a1"
      },
      "c": {
          "id": "b1",
          "a":  "a1"
      }
  }
  
  {
      "id": "b1"
      // problem: "a" isn't serialized
  }
  </code></pre></li>
  </ol>
  
  <p>Is there any built-in Jackson way to serialize as I desire?</p>
  
  <p>If not, does Jackson maintain a serialization stack?</p>
  
  <p>If so, how can I access it, and somehow modify the serialization output?</p>
  
  <p>If not, how can I hook into Jackson to maintain a stack in my own classes that implement some Jackson interfaces / extend some Jackson classes, and then use that stack to influence serialization?</p>