To parse this JSON object:
{"topology": [
{"id": "4cdbc040-657a-4847-b266-7e31d9e2c3d9","name": "PhysicalTOPO","layer": "datalink","nodes": null,"links": null,"sub-layer": "ethernet"},
{"id": "4acbd130-846b-3536-a142-8a42d8a3c4b8","name": "LogicalTOPO","layer": "ip","nodes": null,"links": null,"sub-layer": "ethernet"},
{"id": "9acbd139-946b-9536-a149-9a42d9a3c4b9","name": "NetworkTOPO","layer": "ip","nodes": null,"links": null,"sub-layer": "ethernet"}
]}
I've created VO class and parse function as below:
public class TopologyVo extends Vo {
@JsonProperty("topology")
public Topology[] topology;
//public List<Topology> topology; -- both have same exception
public static class Topology extends Vo{
@JsonProperty("id")
public String id;
@JsonProperty("name")
public String name;
@JsonProperty("layer")
public String layer;
@JsonProperty("nodes")
public Object nodes; // it does not matter to map null to Object now
@JsonProperty("links")
public Object links;
@JsonProperty("sub-layer")
public String subLayer;
}
}
// caller
...
TopologyVo vo = (TopologyVo)parse(content, TopologyVo.class);
...
public Object parse(String input, Class<?> mapClass) throws Exception {
Object object = null;
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
object = mapper.readValue(input, mapClass);
return object;
}
Resulted an exception:
com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class java.lang.Class]
at com.fasterxml.jackson.databind.deser.DeserializerCache._handleUnknownKeyDeserializer(DeserializerCache.java:580)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findKeyDeserializer(DeserializerCache.java:170)
at com.fasterxml.jackson.databind.DeserializationContext.findKeyDeserializer(DeserializationContext.java:404)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.createContextual(MapDeserializer.java:232)
at com.fasterxml.jackson.databind.DeserializationContext.handleSecondaryContextualization(DeserializationContext.java:572)
at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:369)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:697)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:438)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:294)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:243)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:144)
at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:381)
at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:3095)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2989)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2098)
at test.ApiTest.parse(ApiTest.java:195)
Since there is no Map in my VO class, I have no idea how to implement "key deserializer" in the answer to similar question (Can not find a (Map) Key deserializer for type [simple type, class com.comcast.ivr.core.domain.AutoHandlingSlotKey]).
Does this problem come from array (or list/collection) of subclass within VO?
This works perfectly for me: