我有自己的定制反序列化程序。
@Override
public Map<String, Car<?, ?>> deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode carsNode = p.getCodec().readTree(p);
Map<String, Car<?, ?>> CarsMap = new HashMap<String, Car<?, ?>>();
ObjectMapper mapper = new ObjectMapper();
for (JsonNode node : carsNode) {
CarsMap.put(node.get("name").asText(), mapper.readValue(node.asText(), Car.class));
}
return CarsMap;
}
尽管 Car
是一个基类,而且地图应该有 Map String, Cars 的子类),但这并不起作用。
输入 JSON = [{"name": "本田", "type": "常规", "速度": 60}]
这应该在地图中,例如 Map.put("honda", RegularCar.class)
。