Jackson tries to deserialize json into a list of typed objects

I am trying to use Jackson to deserialize a property of an object, which is a list of typed objects. and I get the following error when I try to do this

Unable to create value of type [map type; class java.util.HashMap, [simple type, class java.lang.String] -> [simple type, class java.lang.String]] [map type; class java.util.HashMap, [simple type, class java.lang.String] -> [simple type, class java.lang.String]] from the JSON string; no constructor String / factory method

So far I have the following, but it doesn't seem to work.

 Terms.class @JsonDeserialize(as=JsonMapDeserializer) private List<ObjectA> results = null; //ommitted getter and setters 

My Deserializer class is as follows.

 public class JsonMapDeserializer extends JsonDeserializer<List<ObjectA>> { List<ObjectA> retMap = new ArrayList<ObjectA>(); TypeReference<HashMap<String,String>[]> typeRef = new TypeReference<HashMap<String,String>[]>() {}; @Override public List<ObjectA> deserialize(JsonParser parser, DeserializationContext ctx) throws IOException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); //read the json string into the map HashMap<String, String>[] maps = mapper.readValue(parser, typeRef); if(maps != null) { for(HashMap<String, String> map : maps) { ObjectA result = new ObjectA("id", map.get("id")); retMap.add(result); } } return retMap; } } 

and I use simple modules to add a deserializer as follows

 ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule("safety", Version.unknownVersion()); module.addDeserializer(List.class, new JsonMapDeserializer()); mapper.registerModule(module); 

The JSON string I'm trying to describe is as follows

 "SearchTerms":{"results":[{id":"1010","checked":"true"}] // there are other fields I have just omitted them 

When I run the code for deserialization, I get the following stack trace

 org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [map type; class java.util.HashMap, [simple type, class java.lang.String] -> [simple type, class java.lang.String]] from JSON String; no single-String constructor/factory method (through reference chain: com.model.search["searchTerm1"]) at org.codehaus.jackson.map.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:379) at org.codehaus.jackson.map.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:268) at org.codehaus.jackson.map.deser.std.MapDeserializer.deserialize(MapDeserializer.java:244) at org.codehaus.jackson.map.deser.std.MapDeserializer.deserialize(MapDeserializer.java:33) at org.codehaus.jackson.map.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:104) at org.codehaus.jackson.map.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:18) at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2695) at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1294) at com.az.rd.ke.json.JsonColumnMapDeserializer.deserialize(JsonColumnMapDeserializer.java:41) at com.az.rd.ke.json.JsonColumnMapDeserializer.deserialize(JsonColumnMapDeserializer.java:27) at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299) at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414) at org.codehaus.jackson.map.deser.BeanDeserializer. 

From a look at tracing and debugging the stack, because my module is configured as follows

 module.addDeserializer(List.class, new JsonMapDeserializer()); 

On deserialization, it seems to complain as soon as it hits the first property in my object, which is a list, because the searchTerm1 that it complains about is just a list of strings.

Can anyone advise how to deserialize a list of typed objects, or how I can add a deserializer correctly. If I changed the adddeserializer method to

 module.addDeserializer(List<ObjectA>.class, new JsonMapDeserializer()); 

it has problems with the compiler because the deserializer class is printed as List<ObjectA> .

+6
source share

Source: https://habr.com/ru/post/904148/


All Articles