Commit 321f71de73fd91dbaf6f334e12260e3feefd6300
1 parent
909d3309
重构 LocalDateTimeDeserializer 和 LocalDateTimeSerializer 的命名,可读性更好
Showing
3 changed files
with
20 additions
and
16 deletions
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/config/YudaoJacksonAutoConfiguration.java
| ... | ... | @@ -2,9 +2,9 @@ package cn.iocoder.yudao.framework.jackson.config; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 5 | -import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeDeserializer; | |
| 6 | -import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeSerializer; | |
| 7 | 5 | import cn.iocoder.yudao.framework.jackson.core.databind.NumberSerializer; |
| 6 | +import cn.iocoder.yudao.framework.jackson.core.databind.TimestampLocalDateTimeDeserializer; | |
| 7 | +import cn.iocoder.yudao.framework.jackson.core.databind.TimestampLocalDateTimeSerializer; | |
| 8 | 8 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | 9 | import com.fasterxml.jackson.databind.module.SimpleModule; |
| 10 | 10 | import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; |
| ... | ... | @@ -37,13 +37,13 @@ public class YudaoJacksonAutoConfiguration { |
| 37 | 37 | .addDeserializer(LocalDate.class, LocalDateDeserializer.INSTANCE) |
| 38 | 38 | .addSerializer(LocalTime.class, LocalTimeSerializer.INSTANCE) |
| 39 | 39 | .addDeserializer(LocalTime.class, LocalTimeDeserializer.INSTANCE) |
| 40 | - // 新增 LocalDateTime 序列化、反序列化规则 | |
| 41 | - .addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE) | |
| 42 | - .addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE); | |
| 40 | + // 新增 LocalDateTime 序列化、反序列化规则,使用 Long 时间戳 | |
| 41 | + .addSerializer(LocalDateTime.class, TimestampLocalDateTimeSerializer.INSTANCE) | |
| 42 | + .addDeserializer(LocalDateTime.class, TimestampLocalDateTimeDeserializer.INSTANCE); | |
| 43 | 43 | // 1.2 注册到 objectMapper |
| 44 | 44 | objectMappers.forEach(objectMapper -> objectMapper.registerModule(simpleModule)); |
| 45 | 45 | |
| 46 | - // 2. 设置 objectMapper 到 JsonUtils { | |
| 46 | + // 2. 设置 objectMapper 到 JsonUtils | |
| 47 | 47 | JsonUtils.init(CollUtil.getFirst(objectMappers)); |
| 48 | 48 | log.info("[init][初始化 JsonUtils 成功]"); |
| 49 | 49 | return new JsonUtils(); | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/LocalDateTimeDeserializer.java renamed to yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/TimestampLocalDateTimeDeserializer.java
| ... | ... | @@ -10,16 +10,18 @@ import java.time.LocalDateTime; |
| 10 | 10 | import java.time.ZoneId; |
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | - * LocalDateTime反序列化规则 | |
| 14 | - * <p> | |
| 15 | - * 会将毫秒级时间戳反序列化为LocalDateTime | |
| 13 | + * 基于时间戳的 LocalDateTime 反序列化器 | |
| 14 | + * | |
| 15 | + * @author 老五 | |
| 16 | 16 | */ |
| 17 | -public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { | |
| 17 | +public class TimestampLocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { | |
| 18 | 18 | |
| 19 | - public static final LocalDateTimeDeserializer INSTANCE = new LocalDateTimeDeserializer(); | |
| 19 | + public static final TimestampLocalDateTimeDeserializer INSTANCE = new TimestampLocalDateTimeDeserializer(); | |
| 20 | 20 | |
| 21 | 21 | @Override |
| 22 | 22 | public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { |
| 23 | + // 将 Long 时间戳,转换为 LocalDateTime 对象 | |
| 23 | 24 | return LocalDateTime.ofInstant(Instant.ofEpochMilli(p.getValueAsLong()), ZoneId.systemDefault()); |
| 24 | 25 | } |
| 26 | + | |
| 25 | 27 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/LocalDateTimeSerializer.java renamed to yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/TimestampLocalDateTimeSerializer.java
| ... | ... | @@ -9,16 +9,18 @@ import java.time.LocalDateTime; |
| 9 | 9 | import java.time.ZoneId; |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | - * LocalDateTime序列化规则 | |
| 13 | - * <p> | |
| 14 | - * 会将LocalDateTime序列化为毫秒级时间戳 | |
| 12 | + * 基于时间戳的 LocalDateTime 序列化器 | |
| 13 | + * | |
| 14 | + * @author 老五 | |
| 15 | 15 | */ |
| 16 | -public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { | |
| 16 | +public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { | |
| 17 | 17 | |
| 18 | - public static final LocalDateTimeSerializer INSTANCE = new LocalDateTimeSerializer(); | |
| 18 | + public static final TimestampLocalDateTimeSerializer INSTANCE = new TimestampLocalDateTimeSerializer(); | |
| 19 | 19 | |
| 20 | 20 | @Override |
| 21 | 21 | public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
| 22 | + // 将 LocalDateTime 对象,转换为 Long 时间戳 | |
| 22 | 23 | gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); |
| 23 | 24 | } |
| 25 | + | |
| 24 | 26 | } | ... | ... |