title: How to serialize a JSON file with tree structure with Jackson
body: <p>I need to serialize a JSON file to a Java object. The JSON file has a tree structure, which means that the object at the root has children that are instances of itself and so on. For example, consider the following JSON file:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;name&quot;: &quot;peter&quot;,
    &quot;parent&quot;: null,
    &quot;children&quot;: [
      {
        &quot;name&quot;: &quot;simon&quot;,
        &quot;parent&quot;: &quot;peter&quot;,
        &quot;children&quot;: [
          {
            &quot;name&quot;: &quot;john&quot;,
            &quot;parent&quot;: &quot;simon&quot;,
            &quot;children&quot;: []
          }
        ]
      },
      {
        &quot;name&quot;: &quot;javier&quot;,
        &quot;parent&quot;: &quot;peter&quot;,
        &quot;children&quot;: []
      },
      {
        &quot;name&quot;: &quot;martin&quot;,
        &quot;parent&quot;: &quot;peter&quot;,
        &quot;children&quot;: []
      }
    ]
  }
  </code></pre>
  <p>I have tried the serialization using the Jackson library. This is the object I want to serialize to:</p>
  <pre class="lang-java prettyprint-override"><code>public class Person {
      
     private String name;
     private Person parent;
     private List&lt;Person&gt; children;
  
     @JsonCreator
     public Project(@JsonProperty(&quot;name&quot;) String name,
                    @JsonProperty(&quot;parent&quot;) Person parent,
                    @JsonProperty(&quot;children&quot;) List&lt;Person&gt; children) {
        this.name = name;
        this.coordinates = parent;
        this.children = children;
     }
      
     // getters, setters, and constructor
  }
  </code></pre>
  <p>This is what I have tried so far:</p>
  <pre class="lang-java prettyprint-override"><code>String jsonString = Files.readString(Paths.get(&quot;/path_to/file.json&quot;));
  List&lt;Person&gt; listModel = objectMapper.readValue(jsonString, new TypeReference&lt;&gt;() {});
  </code></pre>
  <p>However, It triggers the following <code>MismatchedInputException: Cannot deserialize instance of java.util.ArrayList&lt;Person&gt; out of START_OBJECT token</code>.</p>