I'm writing a wrapper for JSON Jackson pojo serialization/deserialization. So I tried to write a generic method that will return me the deserialized object generically.
I think the code would explain this better:
public <K,V, M extends Map<K, V>> M readMap(String path, Class<M> mapType, Class<K> keyClass, Class<V> valueClass) throws JsonParseException, JsonMappingException, IOException
{
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(new File(path), mapper.getTypeFactory().constructMapType(mapType, keyClass, valueClass));
}
The code
HashMap<String, Double> weightMap;
JSONParsedFileHandler reader = new JSONParsedFileHandler();
weightMap = reader.readMap("weights.model", HashMap.class, String.class, Double.class);
This works as expected, however, I get a type safety warning:
Type safety: The expression of type HashMap needs unchecked conversion to conform to HashMap<String,Double>
I figured this mean that the type returned is as expected except it is not parameterized as I coded.
Does anyone have any thoughts?