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
java.util.*
in your json... Ha! that's the default typing, I think. Have you tried without that? – Hootchykootchy