From f96cc88f8b85cfecd73d41acc2913833882ee5c0 Mon Sep 17 00:00:00 2001 From: eight <641137800@qq.com> Date: 星期日, 08 九月 2024 17:27:41 +0800 Subject: [PATCH] LocalDate json process --- jh-framework/jh-common/src/main/java/cn/lihu/jh/framework/common/util/json/databind/TimestampLocalDateSerializer.java | 27 +++++++++++++ jh-framework/jh-spring-boot-starter-web/src/main/java/cn/lihu/jh/framework/jackson/config/JhJacksonAutoConfiguration.java | 10 ++-- jh-framework/jh-common/src/main/java/cn/lihu/jh/framework/common/util/json/databind/TimestampLocalDateDeserializer.java | 28 ++++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/jh-framework/jh-common/src/main/java/cn/lihu/jh/framework/common/util/json/databind/TimestampLocalDateDeserializer.java b/jh-framework/jh-common/src/main/java/cn/lihu/jh/framework/common/util/json/databind/TimestampLocalDateDeserializer.java new file mode 100644 index 0000000..dd29cc5 --- /dev/null +++ b/jh-framework/jh-common/src/main/java/cn/lihu/jh/framework/common/util/json/databind/TimestampLocalDateDeserializer.java @@ -0,0 +1,28 @@ +package cn.lihu.jh.framework.common.util.json.databind; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; + +import java.io.IOException; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; + +/** + * 鍩轰簬鏃堕棿鎴崇殑 LocalDate 鍙嶅簭鍒楀寲鍣� + * + * @author majianbo + */ +public class TimestampLocalDateDeserializer extends JsonDeserializer<LocalDate> { + + public static final TimestampLocalDateDeserializer INSTANCE = new TimestampLocalDateDeserializer(); + + @Override + public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { + // 灏� Long 鏃堕棿鎴筹紝杞崲涓� LocalDate 瀵硅薄 + return LocalDate.ofInstant(Instant.ofEpochMilli(p.getValueAsLong()), ZoneId.systemDefault()); + } + +} diff --git a/jh-framework/jh-common/src/main/java/cn/lihu/jh/framework/common/util/json/databind/TimestampLocalDateSerializer.java b/jh-framework/jh-common/src/main/java/cn/lihu/jh/framework/common/util/json/databind/TimestampLocalDateSerializer.java new file mode 100644 index 0000000..c0b7a87 --- /dev/null +++ b/jh-framework/jh-common/src/main/java/cn/lihu/jh/framework/common/util/json/databind/TimestampLocalDateSerializer.java @@ -0,0 +1,27 @@ +package cn.lihu.jh.framework.common.util.json.databind; + +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.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; + +/** + * 鍩轰簬鏃堕棿鎴崇殑 LocalDate 搴忓垪鍖栧櫒 + * + * @author majianbo + */ +public class TimestampLocalDateSerializer extends JsonSerializer<LocalDate> { + + public static final TimestampLocalDateSerializer INSTANCE = new TimestampLocalDateSerializer(); + + @Override + public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + // 灏� LocalDate 瀵硅薄锛岃浆鎹负 Long 鏃堕棿鎴� + gen.writeNumber(value.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); + } + +} diff --git a/jh-framework/jh-spring-boot-starter-web/src/main/java/cn/lihu/jh/framework/jackson/config/JhJacksonAutoConfiguration.java b/jh-framework/jh-spring-boot-starter-web/src/main/java/cn/lihu/jh/framework/jackson/config/JhJacksonAutoConfiguration.java index 1827062..887d07e 100644 --- a/jh-framework/jh-spring-boot-starter-web/src/main/java/cn/lihu/jh/framework/jackson/config/JhJacksonAutoConfiguration.java +++ b/jh-framework/jh-spring-boot-starter-web/src/main/java/cn/lihu/jh/framework/jackson/config/JhJacksonAutoConfiguration.java @@ -2,9 +2,7 @@ import cn.hutool.core.collection.CollUtil; import cn.lihu.jh.framework.common.util.json.JsonUtils; -import cn.lihu.jh.framework.common.util.json.databind.NumberSerializer; -import cn.lihu.jh.framework.common.util.json.databind.TimestampLocalDateTimeDeserializer; -import cn.lihu.jh.framework.common.util.json.databind.TimestampLocalDateTimeSerializer; +import cn.lihu.jh.framework.common.util.json.databind.*; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; @@ -18,6 +16,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.format.DateTimeFormatter; import java.util.List; @AutoConfiguration @@ -27,14 +26,15 @@ @Bean @SuppressWarnings("InstantiationOfUtilityClass") public JsonUtils jsonUtils(List<ObjectMapper> objectMappers) { + // 1.1 鍒涘缓 SimpleModule 瀵硅薄 SimpleModule simpleModule = new SimpleModule(); simpleModule // 鏂板 Long 绫诲瀷搴忓垪鍖栬鍒欙紝鏁板�艰秴杩� 2^53-1锛屽湪 JS 浼氬嚭鐜扮簿搴︿涪澶遍棶棰橈紝鍥犳 Long 鑷姩搴忓垪鍖栦负瀛楃涓茬被鍨� .addSerializer(Long.class, NumberSerializer.INSTANCE) .addSerializer(Long.TYPE, NumberSerializer.INSTANCE) - .addSerializer(LocalDate.class, LocalDateSerializer.INSTANCE) - .addDeserializer(LocalDate.class, LocalDateDeserializer.INSTANCE) + .addSerializer(LocalDate.class, TimestampLocalDateSerializer.INSTANCE) + .addDeserializer(LocalDate.class, TimestampLocalDateDeserializer.INSTANCE) .addSerializer(LocalTime.class, LocalTimeSerializer.INSTANCE) .addDeserializer(LocalTime.class, LocalTimeDeserializer.INSTANCE) // 鏂板 LocalDateTime 搴忓垪鍖栥�佸弽搴忓垪鍖栬鍒欙紝浣跨敤 Long 鏃堕棿鎴� -- Gitblit v1.9.3