Java Jackson: deserialize complex polymorphic object model: JsonMappingException: Unexpected token (START_OBJECT), expected VALUE_STRING

22,212

I believe the problem is with the default typing. The start of your JSON is not generated as what Jackson expect with default typing. The begining of the JSON should be:

["java.util.HashSet", [{

and the end should have an extra closing bracket }]].

That's because you generate the JSON using the toString() method of your set. You should instead use the ObjectMapper, that's configured with default typing, like so:

res = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(yourSet);
Share:
22,212
Giovanni Bitliner
Author by

Giovanni Bitliner

Computer Engineer Skills Data analysis (sentiment analysis, machine learning), database administration, distributed system Frontend+Backend Languages Java/PHP/Javascript (NodeJs)/Python Contacts LinkedIn Profile GitHub

Updated on June 28, 2020

Comments

  • Giovanni Bitliner
    Giovanni Bitliner almost 4 years

    I have this tree of objects

    A

    B extends A

    C extends B

    D extends B

    E extends C

    F extends A and has one reference to A

    A has the following annotation

    @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS,include=JsonTypeInfo.As.PROPERTY,property="@class")

    If i try to deserialize a JSON array of objects that extends A, it throws the following error

    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)

    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:

    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;
    

    The code to deserialize the json array (that is the set described above) is:

    ObjectMapper mapper = new ObjectMapper(); 
    
            mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_CONCRETE_AND_ARRAYS);
            Collection<T> 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;
    

    The json sample that it parses is like:

    "[{
      "@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
    }]"
    

    Here the json string includes only some objects of my sample of java Set