From fe31d61de9e80543a493e15d57dfd08e6d69af30 Mon Sep 17 00:00:00 2001 From: hexiaowu Date: Fri, 12 Mar 2021 18:07:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=9B=A0JS=E7=B2=BE=E5=87=86=E5=BA=A6?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E6=9C=80=E5=A4=9A=E8=A1=A8=E7=A4=BA?= =?UTF-8?q?2^53-1=E7=9A=84=E6=95=B0=E5=80=BC=E3=80=82=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?Long=E7=B1=BB=E5=9E=8B=E5=BA=8F=E5=88=97=E5=8C=96=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=EF=BC=8C=E8=87=AA=E5=8A=A8=E4=BC=9A=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E4=B8=BA=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E8=A7=84=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; + } +} From 4067087da6f8615ef3ad07ed03c8332be18e1c90 Mon Sep 17 00:00:00 2001 From: hexiaowu Date: Sun, 14 Mar 2021 18:56:06 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=9A=84Long=E5=BA=8F=E5=88=97=E5=8C=96=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E7=B1=BB=E5=9E=8B=E8=A7=84=E5=88=99=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9ELocalDateTime=E7=9A=84=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=E3=80=81=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96=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 | 20 +++++++++----- .../deser/LocalDateTimeDeserializer.java | 26 +++++++++++++++++++ .../jackson/ser/LocalDateTimeSerializer.java | 24 +++++++++++++++++ .../framework/jackson/ser/LongSerializer.java | 26 ------------------- 4 files changed, 64 insertions(+), 32 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/jackson/deser/LocalDateTimeDeserializer.java create mode 100644 src/main/java/cn/iocoder/dashboard/framework/jackson/ser/LocalDateTimeSerializer.java delete 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 323b447ce..483d956f8 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,12 +1,16 @@ package cn.iocoder.dashboard.framework.jackson.config; -import cn.iocoder.dashboard.framework.jackson.ser.LongSerializer; +import cn.iocoder.dashboard.framework.jackson.deser.LocalDateTimeDeserializer; +import cn.iocoder.dashboard.framework.jackson.ser.LocalDateTimeSerializer; import cn.iocoder.dashboard.util.json.JsonUtils; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.time.LocalDateTime; + @Configuration public class JacksonConfig { @@ -15,11 +19,15 @@ public class JacksonConfig { 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); + * 1. 新增Long类型序列化规则,数值超过2^53-1,在JS会出现精度丢失问题,因此Long自动序列化为字符串类型 + * 2. 新增LocalDateTime序列化、反序列化规则 + */ + simpleModule.addSerializer(Long.class, ToStringSerializer.instance) + .addSerializer(Long.TYPE, ToStringSerializer.instance) + .addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE) + .addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE); + + objectMapper.registerModules(simpleModule); JsonUtils.init(objectMapper); return new JsonUtils(); diff --git a/src/main/java/cn/iocoder/dashboard/framework/jackson/deser/LocalDateTimeDeserializer.java b/src/main/java/cn/iocoder/dashboard/framework/jackson/deser/LocalDateTimeDeserializer.java new file mode 100644 index 000000000..122f461a4 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/jackson/deser/LocalDateTimeDeserializer.java @@ -0,0 +1,26 @@ +package cn.iocoder.dashboard.framework.jackson.deser; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; + +import java.io.IOException; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; + +/** + * LocalDateTime反序列化规则 + *

+ * 会将毫秒级时间戳反序列化为LocalDateTime + */ +public class LocalDateTimeDeserializer extends JsonDeserializer { + + public static final LocalDateTimeDeserializer INSTANCE = new LocalDateTimeDeserializer(); + + @Override + public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { + return LocalDateTime.ofInstant(Instant.ofEpochMilli(p.getValueAsLong()), ZoneId.systemDefault()); + } +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/jackson/ser/LocalDateTimeSerializer.java b/src/main/java/cn/iocoder/dashboard/framework/jackson/ser/LocalDateTimeSerializer.java new file mode 100644 index 000000000..d7230eadd --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/jackson/ser/LocalDateTimeSerializer.java @@ -0,0 +1,24 @@ +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; +import java.time.LocalDateTime; +import java.time.ZoneId; + +/** + * LocalDateTime序列化规则 + *

+ * 会将LocalDateTime序列化为毫秒级时间戳 + */ +public class LocalDateTimeSerializer extends JsonSerializer { + + public static final LocalDateTimeSerializer INSTANCE = new LocalDateTimeSerializer(); + + @Override + public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); + } +} 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 deleted file mode 100644 index 6c0561d23..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/jackson/ser/LongSerializer.java +++ /dev/null @@ -1,26 +0,0 @@ -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; - } -}