v3.28.0 【优化】优化 Long、BigInteger、BigDecimal 的 JSON 序列化【优化】个人中心样式【优化】Spin加载

This commit is contained in:
zhuoda
2025-08-24 23:05:00 +08:00
parent 3ceea05ba1
commit 63ef235b95
12 changed files with 87 additions and 164 deletions

View File

@@ -20,17 +20,25 @@ public class LongJsonSerializer extends JsonSerializer<Long> {
public static final LongJsonSerializer INSTANCE = new LongJsonSerializer();
/**
* JS 安全整数范围
* 根据 JS Number.MIN_SAFE_INTEGER 与 Number.MAX_SAFE_INTEGER 得来
*/
private static final long JS_MIN_SAFE_INTEGER = -9007199254740991L;
private static final long JS_MAX_SAFE_INTEGER = 9007199254740991L;
@Override
public void serialize(Long value, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
if (null == value) {
gen.writeNull();
return;
}
// js中最大安全整数16位 Number.MAX_SAFE_INTEGER
String longStr = String.valueOf(value);
if (longStr.length() > 16) {
gen.writeString(longStr);
// 如果超出了 JavaScript 安全整数范围,则序列化为字符串
if (value < JS_MIN_SAFE_INTEGER || value > JS_MAX_SAFE_INTEGER) {
gen.writeString(Long.toString(value));
} else {
// 否则,序列化为数字
gen.writeNumber(value);
}
}

View File

@@ -2,6 +2,7 @@ package net.lab1024.sa.base.config;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
@@ -13,6 +14,8 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
@@ -37,6 +40,9 @@ public class JsonConfig {
builder.serializers(new LocalDateSerializer(DatePattern.NORM_DATE_FORMAT.getDateTimeFormatter()));
builder.serializers(new LocalDateTimeSerializer(DatePattern.NORM_DATETIME_FORMAT.getDateTimeFormatter()));
builder.serializerByType(Long.class, LongJsonSerializer.INSTANCE);
builder.serializerByType(Long.TYPE, LongJsonSerializer.INSTANCE);
builder.serializerByType(BigInteger.class, ToStringSerializer.instance);
builder.serializerByType(BigDecimal.class, ToStringSerializer.instance);
};
}