Commit 2c8e2209e88d72241386057e62f86815030c96ae
1 parent
a43b884c
feat: 增加全局超长number序列化
Showing
3 changed files
with
35 additions
and
2 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/collection/SetUtils.java
| @@ -11,6 +11,7 @@ import java.util.Set; | @@ -11,6 +11,7 @@ import java.util.Set; | ||
| 11 | */ | 11 | */ |
| 12 | public class SetUtils { | 12 | public class SetUtils { |
| 13 | 13 | ||
| 14 | + @SafeVarargs | ||
| 14 | public static <T> Set<T> asSet(T... objs) { | 15 | public static <T> Set<T> asSet(T... objs) { |
| 15 | return new HashSet<>(Arrays.asList(objs)); | 16 | return new HashSet<>(Arrays.asList(objs)); |
| 16 | } | 17 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/config/YudaoJacksonAutoConfiguration.java
| 1 | package cn.iocoder.yudao.framework.jackson.config; | 1 | package cn.iocoder.yudao.framework.jackson.config; |
| 2 | 2 | ||
| 3 | +import cn.iocoder.yudao.framework.jackson.core.databind.NumberSerializer; | ||
| 3 | import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeDeserializer; | 4 | import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeDeserializer; |
| 4 | import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeSerializer; | 5 | import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeSerializer; |
| 5 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | 6 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| @@ -32,8 +33,8 @@ public class YudaoJacksonAutoConfiguration { | @@ -32,8 +33,8 @@ public class YudaoJacksonAutoConfiguration { | ||
| 32 | * 2. 新增LocalDateTime序列化、反序列化规则 | 33 | * 2. 新增LocalDateTime序列化、反序列化规则 |
| 33 | */ | 34 | */ |
| 34 | simpleModule | 35 | simpleModule |
| 35 | -// .addSerializer(Long.class, ToStringSerializer.instance) | ||
| 36 | -// .addSerializer(Long.TYPE, ToStringSerializer.instance) | 36 | + .addSerializer(Long.class, NumberSerializer.INSTANCE) |
| 37 | + .addSerializer(Long.TYPE, NumberSerializer.INSTANCE) | ||
| 37 | .addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE) | 38 | .addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE) |
| 38 | .addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE); | 39 | .addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE); |
| 39 | 40 |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/NumberSerializer.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.jackson.core.databind; | ||
| 2 | + | ||
| 3 | +import com.fasterxml.jackson.core.JsonGenerator; | ||
| 4 | +import com.fasterxml.jackson.databind.SerializerProvider; | ||
| 5 | +import com.fasterxml.jackson.databind.annotation.JacksonStdImpl; | ||
| 6 | + | ||
| 7 | +import java.io.IOException; | ||
| 8 | +/** | ||
| 9 | + * Long序列化规则 | ||
| 10 | + * <p> | ||
| 11 | + * 会将超长long值转换为string | ||
| 12 | + */ | ||
| 13 | +@JacksonStdImpl | ||
| 14 | +public class NumberSerializer extends com.fasterxml.jackson.databind.ser.std.NumberSerializer { | ||
| 15 | + private static final long MAX_SAFE_INTEGER = 9007199254740991L; | ||
| 16 | + private static final long MIN_SAFE_INTEGER = -9007199254740991L; | ||
| 17 | + | ||
| 18 | + public static final NumberSerializer INSTANCE = new NumberSerializer(Number.class); | ||
| 19 | + public NumberSerializer(Class<? extends Number> rawType) { | ||
| 20 | + super(rawType); | ||
| 21 | + } | ||
| 22 | + @Override | ||
| 23 | + public void serialize(Number value, JsonGenerator gen, SerializerProvider serializers) throws IOException { | ||
| 24 | + // 超出范围 序列化位字符串 | ||
| 25 | + if (value.longValue() > MIN_SAFE_INTEGER && value.longValue() < MAX_SAFE_INTEGER) { | ||
| 26 | + super.serialize(value, gen, serializers); | ||
| 27 | + } else { | ||
| 28 | + gen.writeString(value.toString()); | ||
| 29 | + } | ||
| 30 | + } | ||
| 31 | +} |