|
@@ -0,0 +1,52 @@
|
|
|
+/**
|
|
|
+ * Copyright (C), 2022-07-01
|
|
|
+ * FileName: BigDecimalSerializer
|
|
|
+ * Author: wanglongda
|
|
|
+ * Date: 2022/7/1 14:22
|
|
|
+ * Description: 金钱序列话
|
|
|
+ */
|
|
|
+package me.zhengjie.utils;/**
|
|
|
+ * Created Name: wanglongda
|
|
|
+ * Created Time: 2022/7/1 14:22
|
|
|
+ * Description: iot-zhcy
|
|
|
+ */
|
|
|
+
|
|
|
+import com.alibaba.fastjson.serializer.JSONSerializer;
|
|
|
+import com.alibaba.fastjson.serializer.SerializeWriter;
|
|
|
+import com.alibaba.fastjson.serializer.ToStringSerializer;
|
|
|
+import com.fasterxml.jackson.core.JsonGenerator;
|
|
|
+import com.fasterxml.jackson.databind.JsonSerializer;
|
|
|
+import com.fasterxml.jackson.databind.SerializerProvider;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.math.BigDecimal;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <功能简要> <br>
|
|
|
+ * <金钱序列化>
|
|
|
+ *
|
|
|
+ * @Author wanglongda
|
|
|
+ * @createTime 2022/7/1 14:22
|
|
|
+ * @Version 1.0.0
|
|
|
+ */
|
|
|
+
|
|
|
+@Component
|
|
|
+public class BigDecimalSerializer extends JsonSerializer<BigDecimal> {
|
|
|
+
|
|
|
+ public static final BigDecimalSerializer instance = new BigDecimalSerializer();
|
|
|
+
|
|
|
+ public BigDecimalSerializer() {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
|
|
+ if (!ObjectUtils.isEmpty(bigDecimal)){
|
|
|
+ BigDecimal number = bigDecimal.setScale(2,BigDecimal.ROUND_HALF_EVEN);
|
|
|
+ jsonGenerator.writeNumber(number);
|
|
|
+ } else {
|
|
|
+ jsonGenerator.writeNumber(bigDecimal);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|