用StdDeserializer都是错的, 使用配置
你说的都是错的. 可以使用这个配置 可以使用下这个
objectMapper.enable (DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
public static void main(String[] args) throws JsonProcessingException {
String json = "{\"name\":\"John\",\"age\":30,\"salary\":1000.00}";
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(BigDecimal.class, new BigDecimalDeserializer());
objectMapper.registerModule(module);
Map<String, Object> map = objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
BigDecimal salary = (BigDecimal) map.get("salary");
}
错误示范:
SimpleModule module = new SimpleModule();
module.addDeserializer(BigDecimal.class, new BigDecimalDeserializer());
objectMapper.registerModule(module);
objectMapper.
public static class BigDecimalDeserializer extends StdDeserializer<BigDecimal> {
public BigDecimalDeserializer() {
super(BigDecimal.class);
}
@Override
public BigDecimal deserialize(JsonParser parser, DeserializationContext context) throws IOException {
return new BigDecimal(parser.getDoubleValue());
}
}