title: Java Jackson: deserialize complex polymorphic object model: JsonMappingException: Unexpected token (START_OBJECT), expected VALUE_STRING
body: <p>I have this tree of objects</p>
  
  <p>A</p>
  
  <p>B extends A</p>
  
  <p>C extends B</p>
  
  <p>D extends B</p>
  
  <p>E extends C</p>
  
  <p>F extends A and has one reference to A</p>
  
  <p>A has the following annotation</p>
  
  <p><code>@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS,include=JsonTypeInfo.As.PROPERTY,property="@class")</code></p>
  
  <p>If i try to deserialize a JSON array of objects that extends A, it throws the following error</p>
  
  <blockquote>
    <p>org.codehaus.jackson.map.JsonMappingException: Unexpected token
    (START_OBJECT), expected VALUE_STRING: need JSON String that contains
    type id (for subtype of java.util.Collection)</p>
  </blockquote>
  
  <p>The json string is generated by toString() method of a set and the set is parametric to type A where A is serialized in JSON with the following code:</p>
  
  <pre><code>ObjectMapper objectMapper=new ObjectMapper();
          objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_CONCRETE_AND_ARRAYS);
          String res="";
          try {
              res = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(t);
          } catch (JsonGenerationException e) {
              e.printStackTrace();
          } catch (JsonMappingException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return res;
  </code></pre>
  
  <p>The code to deserialize the json array (that is the set described above) is:</p>
  
  <pre><code>ObjectMapper mapper = new ObjectMapper(); 
  
          mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_CONCRETE_AND_ARRAYS);
          Collection&lt;T&gt; results=null;
          try {
              results =  mapper.readValue(json, TypeFactory.defaultInstance().constructParametricType(Collection.class, clazz ) );
          } catch (JsonParseException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (JsonMappingException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }  
          return results;
  </code></pre>
  
  <p>The json sample that it parses is like:</p>
  
  <pre><code>"[{
    "@class" : "pack1.pack2.MyClass",
    "id" : null,
    "f1" : "",
    "f2" : 0.9933817827,
    "f3" : 6.883261E-4,
    "f4" : 0.001375699,
    "f5" : {
      "@class" : "pack1.pack2.MyClass2",
      "id" : null,
      "f1" : "",
      "f2" : 0.0,
      "f3" : 0.0,
      "f4" : 0.0,
      "f5" : [ "java.util.HashSet", [ 0 ] ],
      "f6" : [ "java.util.HashSet", [ 2 ] ],
      "f7" : [ "java.util.ArrayList", [ "scelta", "brani", "buona" ] ],
      "f8" : [ null, "NOM", null ],
      "f9" : false
    },
    "f10" : [ "java.util.HashMap", {
      "2" : "ADJ"
    } ],
    "f11" : [ "java.util.HashSet", [ 0 ] ],
    "f12" : [ "java.util.HashSet", [ 2 ] ],
    "f13" : [ "java.util.ArrayList", [ "scelta", "brani", "buona" ] ],
    "featureIndicator" : false
  }]"
  </code></pre>
  
  <p>Here the json string includes only some objects of my sample of java Set </p>