title: How to deserialize a JSON object to a binary tree by using Jackson
body: <p>From the previous question <a href="https://groups.google.com/forum/#!topic/jackson-user/EbltfyXSVV8" rel="nofollow">How to deserialize a JSON array to a singly linked list </a> , I learned how to deserialize a JSON array to a singly linked list.</p>
  
  <p>Now I want to deserialize a JSON object to a binary tree in Java.</p>
  
  <p>The definition of the binary tree node is as the following:</p>
  
  <pre><code>public class BinaryTreeNode&lt;E&gt; {
      public E value;
      public BinaryTreeNode left;
      public BinaryTreeNode right;
      public BinaryTreeNode(final E value) {
          this.value = value;
      }
  }
  </code></pre>
  
  <p>How to deserialize a JSON string such as:</p>
  
  <pre><code>{
    "value": 2,
    "left": {
      "value": 1,
      "left": null,
      "right": null
    },
    "right": {
      "value": 10,
      "left": {
        "value": 5,
        "left": null,
        "right": null
      },
      "right": null
    }
  }
  </code></pre>
  
  <p>to  a binary tree?</p>
  
  <pre><code>  2 
   / \ 
  1   10 
     / 
    5
  </code></pre>
  
  <p>Here is the unit test code:</p>
  
  <pre><code>@Test public void binaryTreeNodeTest() throws IOException {
      ObjectMapper objectMapper = new ObjectMapper();
  
      final ArrayList&lt;Integer&gt; intArray = objectMapper.readValue("[1,2,3,4,5]",
          new TypeReference&lt;ArrayList&lt;Integer&gt;&gt;() {});
      System.out.println(intArray);
  
      /* How to achieve this?
           2
          / \
         1   10
            /
           5
       {
         "value": 2,
         "left": {
          "value": 1,
          "left": null,
          "right": null
        },
        "right": {
          "value": 10,
          "left": {
            "value": 5,
            "left": null,
            "right": null
          },
          "right": null
        }
      }
      */
      final String jsonStr = "{\n"
          + "  \"value\": 2,\n"
          + "  \"left\": {\n"
          + "    \"value\": 1,\n"
          + "    \"left\": null,\n"
          + "    \"right\": null\n"
          + "  },\n" + "  \"right\": {\n"
          + "    \"value\": 10,\n"
          + "    \"left\": {\n"
          + "      \"value\": 5,\n"
          + "      \"left\": null,\n"
          + "      \"right\": null\n"
          + "    },\n"
          + "    \"right\": null\n"
          + "  }\n"
          + "}";
      System.out.println(jsonStr);
      final BinaryTreeNode&lt;Integer&gt; intTree = objectMapper.readValue(jsonStr,
          new TypeReference&lt;BinaryTreeNode&lt;Integer&gt;&gt;() {});
      System.out.println(intTree);
  }
  </code></pre>
  
  <p>In other word, I want the <code>BinaryTreeNode</code> to be a first-class citizen the same as <code>ArrayList</code>, which can be used in all kinds of combinations, such as <code>HashSet&lt;BinaryTreeNode&lt;Integer&gt;&gt;</code>, <code>BinaryTreeNode&lt;HashMap&lt;String, Integer&gt;&gt;</code>, etc.</p>