title: Recursive objects to JSON
body: <p>Suppose I have the following code:</p>
  
  <pre><code>public class A {
      B b;
  
      public A() {
          this.b = new B(this);
      }
  }
  
  public class B {
      A a;
  
      B(A a) {
          this.a = a;
      }
  
  }
  </code></pre>
  
  <p>As you can see, those objects (<code>A</code> and <code>B</code>) are referring to eachother, causing an infinite recursion if you try to convert the objects to JSON code: A has B, which as the same A, which has the same B, et cetera, et cetera.</p>
  
  <p>But if I try to convert those objects to JSON, for example using Gson, a <code>StackOverflowError</code> raises, due to the recursion (which is, by the way, completely logical).</p>
  
  <p>Now is there a way in JSON to declare a recursion like this? If not, then is there a way to handle such recursion in JSON?</p>
  
  <p>Or do I have to manually check for recursion, remove it, convert the object to JSON, and reapply the recursion while rebuilding the JSON string to Java objects?</p>