Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson deserialization ... Unexpected token (END_OBJECT),

I am trying to deserialize a JSON array into a Java Collection using Jackson. This motivated by the answers to this question I asked last night Can I instantiate a superclass and have a particular subclass be instantiated based on the parameters supplied.

The error I am gettings is (line breaks added for readability):

org.codehaus.jackson.map.JsonMappingException: 
    Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'type'
    that is to contain type id  (for class sempedia.model.query.QueryValue)
 at [Source: java.io.StringReader@325aef; line: 1, column: 175] 
    (through reference chain: sempedia.model.query.QueryProperty["values"])

My situation is quite complicated. My array contains objects which themselves contain a value which is an array. This array in turn contains values which are also objects but are not necessarily the same (thus polymorphism).

Here is a sample JSON string:

[
    {
      "id":"74562",
      "uri":"http://dbpedia.org/ontology/family",
      "name":"family",
      "values":[
         {
            "id":"74563",
            "uri":"http://dbpedia.org/resource/Orycteropodidae",
            "name":"Orycteropodidae"
         }
      ],
      "selected":false
    },
    {
      "id":"78564",
      "uri":"http://dbpedia.org/ontology/someNumber",
      "name":"someNumber",
      "values":[
         {
            "lower":"45",
            "upper":"975",
         }
      ],
      "selected":true
    }
]

I would like to use this (below) code or something similar to get an object which is an instance of Collection<QueryProperty> which I have called queryProperties

ObjectMapper mapper = new ObjectMapper();  
Collection<QueryProperty> queryProperties = 
   queryProperties = mapper.readValue(query, 
      new TypeReference<Collection<QueryProperty>>(){});

My classes for deserialization (they have public getters/setters which I am not printing) are listed below:

public class QueryProperty {    
    int id;
    String uri;
    String name;
    Set<QueryValue> values;
    String selected;
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")  
@JsonSubTypes({  
    @Type(value = ResourceQueryValue.class),  
    @Type(value = NumericQueryValue.class)
    })  
public abstract class QueryValue {
    String type;
}

ResourceQueryValue

public class ResourceQueryValue extends QueryValue{
    int id;
    String uri;
    String name;
}

NumericQueryValue the same JSON doesn't include an object of this type.

public class NumericQueryValue extends QueryValue{
    double lower;
    double upper;
}

Initial part of Stack trace:

org.codehaus.jackson.map.JsonMappingException: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'type' that is to contain type id  (for class sempedia.model.query.QueryValue)
 at [Source: java.io.StringReader@325aef; line: 1, column: 175] (through reference chain: sempedia.model.query.QueryProperty["values"])
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.wrongTokenException(StdDeserializationContext.java:240)
    at org.codehaus.jackson.map.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:86)
    at org.codehaus.jackson.map.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:89)
like image 767
Ankur Avatar asked Jun 22 '11 04:06

Ankur


1 Answers

As often happens, writing out a question helps you see the solution. So I need to do two things.

Firstly I need to add the type information into the JSON - which is not what I really wanted to do, but I guess you need to provide that information somewhere.

And then I need to edit the annotation on QueryValue to be:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")  
@JsonSubTypes({  
    @Type(value = ResourceQueryValue.class, name = "ResourceQueryValue"),  
    @Type(value = NumericQueryValue.class, name= "NumericQueryValue")
    })  
like image 128
Ankur Avatar answered Nov 03 '22 06:11

Ankur