From fe31d61de9e80543a493e15d57dfd08e6d69af30 Mon Sep 17 00:00:00 2001 From: hexiaowu Date: Fri, 12 Mar 2021 18:07:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=A0JS=E7=B2=BE=E5=87=86=E5=BA=A6=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E6=9C=80=E5=A4=9A=E8=A1=A8=E7=A4=BA2^53-1?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E5=80=BC=E3=80=82=20=E6=96=B0=E5=A2=9ELong?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=BA=8F=E5=88=97=E5=8C=96=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E5=80=99=EF=BC=8C=E8=87=AA=E5=8A=A8=E4=BC=9A=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E4=B8=BA=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=B1=BB=E5=9E=8B=E8=A7=84?= =?UTF-8?q?=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jackson/config/JacksonConfig.java | 11 +++++++- .../framework/jackson/ser/LongSerializer.java | 26 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/jackson/ser/LongSerializer.java diff --git a/src/main/java/cn/iocoder/dashboard/framework/jackson/config/JacksonConfig.java b/src/main/java/cn/iocoder/dashboard/framework/jackson/config/JacksonConfig.java index 27d199a2d..323b447ce 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/jackson/config/JacksonConfig.java +++ b/src/main/java/cn/iocoder/dashboard/framework/jackson/config/JacksonConfig.java @@ -1,7 +1,9 @@ package cn.iocoder.dashboard.framework.jackson.config; +import cn.iocoder.dashboard.framework.jackson.ser.LongSerializer; import cn.iocoder.dashboard.util.json.JsonUtils; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -11,8 +13,15 @@ public class JacksonConfig { @Bean @SuppressWarnings("InstantiationOfUtilityClass") public JsonUtils jsonUtils(ObjectMapper objectMapper) { + SimpleModule simpleModule = new SimpleModule(); + /* + * 新增Long类型序列化规则,数值超过2^53-1,在JS会出现精度丢失问题,因此Long自动序列化为字符串类型 + */ + simpleModule.addSerializer(Long.class,LongSerializer.getInstance()) + .addSerializer(Long.TYPE,LongSerializer.getInstance()); + objectMapper.registerModule(simpleModule); + JsonUtils.init(objectMapper); return new JsonUtils(); } - } diff --git a/src/main/java/cn/iocoder/dashboard/framework/jackson/ser/LongSerializer.java b/src/main/java/cn/iocoder/dashboard/framework/jackson/ser/LongSerializer.java new file mode 100644 index 000000000..6c0561d23 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/jackson/ser/LongSerializer.java @@ -0,0 +1,26 @@ +package cn.iocoder.dashboard.framework.jackson.ser; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; + +import java.io.IOException; + +/** + * Long类型序列化规则 + *

+ * 数值超过2^53-1,在JS会出现精度丢失问题,因此Long自动序列化为字符串类型 + */ +public class LongSerializer extends JsonSerializer { + + private static final LongSerializer LONG_SERIALIZER = new LongSerializer(); + + @Override + public void serialize(Long value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + gen.writeString(value.toString()); + } + + public static LongSerializer getInstance() { + return LONG_SERIALIZER; + } +}