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; - } -}