From 4067087da6f8615ef3ad07ed03c8332be18e1c90 Mon Sep 17 00:00:00 2001 From: hexiaowu Date: Sun, 14 Mar 2021 18:56:06 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E7=9A=84Long=E5=BA=8F=E5=88=97=E5=8C=96=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E7=B1=BB=E5=9E=8B=E8=A7=84=E5=88=99=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9ELocalDateTime=E7=9A=84=E5=BA=8F=E5=88=97=E5=8C=96?= =?UTF-8?q?=E3=80=81=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96=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 | 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; - } -}