JSON Jackson pojo シリアライゼーション/デシリアライゼーションのラッパーを書いています。そこで、逆シリアル化されたオブジェクトを汎用的に返す汎用メソッドを作成しようとしました。
コードはこれをよりよく説明すると思います:
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));
}
コード
HashMap<String, Double> weightMap;
JSONParsedFileHandler reader = new JSONParsedFileHandler();
weightMap = reader.readMap("weights.model", HashMap.class, String.class, Double.class);
これは期待どおりに機能しますが、タイプ セーフの警告が表示されます。
Type safety: The expression of type HashMap needs unchecked conversion to conform to HashMap<String,Double>
これは、コード化されたようにパラメーター化されていないことを除いて、返される型が期待どおりであることを意味すると考えました。
誰か考えがありますか?