我有这段Java代码:
public class MapJSONHandler<KeyType, ValueType> {
private ObjectMapper mapper;
private String filePath;
public MapJSONHandler(String filePath) {
mapper = new ObjectMapper();
this.filePath = filePath;
}
public ValueType getValue(KeyType key) throws Exception{
ValueType value = getMap().get(key);
return value;
}
private Map<KeyType, ValueType> getMap() throws Exception {
Map<KeyType, ValueType> map;
File file = new File(filePath);
if (file.length() == 0) {
map = new HashMap<>();
} else {
map = mapper.readValue(file, new TypeReference<HashMap<KeyType, ValueType>>() {});
}
return map;
}
}
public class Program1
{
public static void main(String [] args)
{
MapJSONHandler<String, byte[]> usersHandler = new MapJSONHandler<>("users.json");
byte[] hash = usersHandler.getValue("foo");
}
}
JSON文件包含适当的元素。 Program1引发ClassCastException(无法将java.lang.String强制转换为[B])。为什么?
答案 0 :(得分:0)
这里有错误的期望。
这就是因为您定义了 generic-enabled 类
byte[]
的形式检索属性的值
class MapJSONHandler<KeyType, ValueType>
不幸的是,这是不正确的。有了这行代码
mapper.readValue(file, new TypeReference<HashMap<KeyType, ValueType>>() {})
ObjectMapper#readValue
将返回HashMap<Object, Object>
,因为通用类型会丢失。
您可以在这里看到<String, byte[]>
的知识了。
与使用显式设置类型相比,您会注意到差异
new TypeReference<HashMap<String, byte[]>>() {}
基础值将是任何允许的类型,例如String
,Integer
,Double
或Boolean
。因此
usersHandler.getValue("foo")
将正确返回String
,而不是byte[]
。
我认为,如果您需要将所有值都转换为byte[]
,则需要一个自定义反序列化器,或者只是为了显式设置泛型类型。