Mongodb 3.4 就开始支持Decimal 类型,解决double的精度问题,但是不太好用,MapReduce的时候Array.sum 也不能计算 Decimal.比较坑,但是聚合可以用
SpringMvc @RequestBody 对新的类型转换JSON 都不太好,例如LocalDate LocalDateTime ,还有Decimal128
这里需要自己写JsonDeserializer, JsonSerializer
/** * User: laizhenwei */ public class Decimal128Serializer extends JsonSerializer<Decimal128> { @Override public void serialize(Decimal128 decimal128, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeString(decimal128.toString()); } }
/** * User: laizhenwei */ public class Decimal128Deserializer extends JsonDeserializer<Decimal128> { @Override public Decimal128 deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { return new Decimal128(new BigDecimal(jsonParser.getText())); } }
在对象的Decimal128字段上加上以下注解,以支持@RequestBody页面传类似"12.01"的浮点字符串,以及返回的也是字符串json value
@JsonDeserialize(using = Decimal128Deserializer.class) @JsonSerialize(using = Decimal128Serializer.class)