Commit fe31d61de9e80543a493e15d57dfd08e6d69af30
1 parent
4e5430b5
因JS精准度问题,最多表示2^53-1的数值。
新增Long类型序列化的时候,自动会转换为字符串类型规则
Showing
2 changed files
with
36 additions
and
1 deletions
src/main/java/cn/iocoder/dashboard/framework/jackson/config/JacksonConfig.java
| 1 | package cn.iocoder.dashboard.framework.jackson.config; | 1 | package cn.iocoder.dashboard.framework.jackson.config; |
| 2 | 2 | ||
| 3 | +import cn.iocoder.dashboard.framework.jackson.ser.LongSerializer; | ||
| 3 | import cn.iocoder.dashboard.util.json.JsonUtils; | 4 | import cn.iocoder.dashboard.util.json.JsonUtils; |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | 5 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import com.fasterxml.jackson.databind.module.SimpleModule; | ||
| 5 | import org.springframework.context.annotation.Bean; | 7 | import org.springframework.context.annotation.Bean; |
| 6 | import org.springframework.context.annotation.Configuration; | 8 | import org.springframework.context.annotation.Configuration; |
| 7 | 9 | ||
| @@ -11,8 +13,15 @@ public class JacksonConfig { | @@ -11,8 +13,15 @@ public class JacksonConfig { | ||
| 11 | @Bean | 13 | @Bean |
| 12 | @SuppressWarnings("InstantiationOfUtilityClass") | 14 | @SuppressWarnings("InstantiationOfUtilityClass") |
| 13 | public JsonUtils jsonUtils(ObjectMapper objectMapper) { | 15 | public JsonUtils jsonUtils(ObjectMapper objectMapper) { |
| 16 | + SimpleModule simpleModule = new SimpleModule(); | ||
| 17 | + /* | ||
| 18 | + * 新增Long类型序列化规则,数值超过2^53-1,在JS会出现精度丢失问题,因此Long自动序列化为字符串类型 | ||
| 19 | + */ | ||
| 20 | + simpleModule.addSerializer(Long.class,LongSerializer.getInstance()) | ||
| 21 | + .addSerializer(Long.TYPE,LongSerializer.getInstance()); | ||
| 22 | + objectMapper.registerModule(simpleModule); | ||
| 23 | + | ||
| 14 | JsonUtils.init(objectMapper); | 24 | JsonUtils.init(objectMapper); |
| 15 | return new JsonUtils(); | 25 | return new JsonUtils(); |
| 16 | } | 26 | } |
| 17 | - | ||
| 18 | } | 27 | } |
src/main/java/cn/iocoder/dashboard/framework/jackson/ser/LongSerializer.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.framework.jackson.ser; | ||
| 2 | + | ||
| 3 | +import com.fasterxml.jackson.core.JsonGenerator; | ||
| 4 | +import com.fasterxml.jackson.databind.JsonSerializer; | ||
| 5 | +import com.fasterxml.jackson.databind.SerializerProvider; | ||
| 6 | + | ||
| 7 | +import java.io.IOException; | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * Long类型序列化规则 | ||
| 11 | + * <p> | ||
| 12 | + * 数值超过2^53-1,在JS会出现精度丢失问题,因此Long自动序列化为字符串类型 | ||
| 13 | + */ | ||
| 14 | +public class LongSerializer extends JsonSerializer<Long> { | ||
| 15 | + | ||
| 16 | + private static final LongSerializer LONG_SERIALIZER = new LongSerializer(); | ||
| 17 | + | ||
| 18 | + @Override | ||
| 19 | + public void serialize(Long value, JsonGenerator gen, SerializerProvider serializers) throws IOException { | ||
| 20 | + gen.writeString(value.toString()); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + public static LongSerializer getInstance() { | ||
| 24 | + return LONG_SERIALIZER; | ||
| 25 | + } | ||
| 26 | +} |