title: Gson, serialize a tree structure
body: <p>I am trying to serialize a tree with GSon. This is the class of my <code>TreeNode</code> I want to serialize:</p>
  
  <pre><code>public class TreeNode {
  private TreeNode parent;
  private ArrayList&lt;TreeNode&gt; children;
  private Object value;
      //methods
  }
  </code></pre>
  
  <p>And my GSon calls look like this:</p>
  
  <pre><code>    TreeNode headNode = getHeadNode();
      Gson gson = new Gson();
      Type typeOfSrc = new TypeToken&lt;TreeNode&gt;(){}.getType();
      String gsonTreeString = gson.toJson(headNode,typeOfSrc);
  </code></pre>
  
  <p>As soon the <code>headNode</code> has at least one child, a stack overflow occurs and I don't understand why. Can someone tell me what I am doing wrong?</p>