From 918a0abfda61bda8199256e4d4ecd5e20564569e Mon Sep 17 00:00:00 2001 From: jasmine <362055143@qq.com> Date: Thu, 22 May 2025 03:24:53 +0000 Subject: [PATCH] =?UTF-8?q?feat(json/jackson):=20Jackson=20=E5=A4=A7?= =?UTF-8?q?=E6=95=B0=E5=80=BC=E5=BA=8F=E5=88=97=E5=8C=96=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=A4=9A=E6=A8=A1=E5=BC=8F=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JacksonAutoConfiguration.java | 76 ++++++++++++++----- .../autoconfigure/JacksonProperties.java | 44 +++++++++++ .../enums/BigNumberSerializeModeEnum.java | 33 ++++++++ .../main/resources/default-json-jackson.yml | 2 + 4 files changed, 134 insertions(+), 21 deletions(-) create mode 100644 continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/autoconfigure/JacksonProperties.java create mode 100644 continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/enums/BigNumberSerializeModeEnum.java diff --git a/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/autoconfigure/JacksonAutoConfiguration.java b/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/autoconfigure/JacksonAutoConfiguration.java index a40caf62..839bebd5 100644 --- a/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/autoconfigure/JacksonAutoConfiguration.java +++ b/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/autoconfigure/JacksonAutoConfiguration.java @@ -16,8 +16,24 @@ package top.continew.starter.json.jackson.autoconfigure; -import cn.hutool.core.date.DatePattern; +import java.math.BigInteger; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.util.Objects; +import java.util.TimeZone; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.PropertySource; + import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; @@ -25,26 +41,16 @@ import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.autoconfigure.AutoConfiguration; -import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.PropertySource; + +import cn.hutool.core.date.DatePattern; import top.continew.starter.core.enums.BaseEnum; import top.continew.starter.core.util.GeneralPropertySourceFactory; +import top.continew.starter.json.jackson.enums.BigNumberSerializeModeEnum; import top.continew.starter.json.jackson.serializer.BaseEnumDeserializer; import top.continew.starter.json.jackson.serializer.BaseEnumSerializer; import top.continew.starter.json.jackson.serializer.BigNumberSerializer; import top.continew.starter.json.jackson.serializer.SimpleDeserializersWrapper; -import java.math.BigInteger; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.format.DateTimeFormatter; -import java.util.TimeZone; - /** * Jackson 自动配置 * @@ -52,17 +58,27 @@ import java.util.TimeZone; * @since 1.0.0 */ @AutoConfiguration +@EnableConfigurationProperties(JacksonProperties.class) @PropertySource(value = "classpath:default-json-jackson.yml", factory = GeneralPropertySourceFactory.class) public class JacksonAutoConfiguration { private static final Logger log = LoggerFactory.getLogger(JacksonAutoConfiguration.class); + private final JacksonProperties jacksonProperties; + + public JacksonAutoConfiguration(JacksonProperties jacksonProperties) { + this.jacksonProperties = jacksonProperties; + } + @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> { + JavaTimeModule javaTimeModule = this.timeModule(); - SimpleModule simpleModule = this.simpleModule(); + SimpleModule enumModule = this.baseEnumModule(); + SimpleModule bigNumberModule = this.bigNumberModule(); + builder.timeZone(TimeZone.getDefault()); - builder.modules(javaTimeModule, simpleModule); + builder.modules(javaTimeModule, enumModule, bigNumberModule); log.debug("[ContiNew Starter] - Auto Configuration 'Jackson' completed initialization."); }; } @@ -74,11 +90,7 @@ public class JacksonAutoConfiguration { * @since 1.0.0 */ private JavaTimeModule timeModule() { - // 针对大数值的序列化处理 JavaTimeModule javaTimeModule = new JavaTimeModule(); - javaTimeModule.addSerializer(Long.class, BigNumberSerializer.SERIALIZER_INSTANCE); - javaTimeModule.addSerializer(Long.TYPE, BigNumberSerializer.SERIALIZER_INSTANCE); - javaTimeModule.addSerializer(BigInteger.class, BigNumberSerializer.SERIALIZER_INSTANCE); // 针对时间类型:LocalDateTime 的序列化和反序列化处理 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter)); @@ -100,7 +112,7 @@ public class JacksonAutoConfiguration { * @return SimpleModule / * @since 2.4.0 */ - private SimpleModule simpleModule() { + private SimpleModule baseEnumModule() { SimpleModule simpleModule = new SimpleModule(); simpleModule.addSerializer(BaseEnum.class, BaseEnumSerializer.SERIALIZER_INSTANCE); SimpleDeserializersWrapper deserializers = new SimpleDeserializersWrapper(); @@ -108,4 +120,26 @@ public class JacksonAutoConfiguration { simpleModule.setDeserializers(deserializers); return simpleModule; } + + /** + * 大数值序列化及反序列化配置 + * + * @return SimpleModule / + * @since 2.12.0 + */ + private SimpleModule bigNumberModule() { + SimpleModule bigNumberModule = new SimpleModule(); + if (Objects.equals(jacksonProperties.getBigNumberSerializeMode(), BigNumberSerializeModeEnum.FLEXIBLE)) { + bigNumberModule.addSerializer(Long.class, BigNumberSerializer.SERIALIZER_INSTANCE); + bigNumberModule.addSerializer(Long.TYPE, BigNumberSerializer.SERIALIZER_INSTANCE); + bigNumberModule.addSerializer(BigInteger.class, BigNumberSerializer.SERIALIZER_INSTANCE); + } else if (Objects.equals(jacksonProperties + .getBigNumberSerializeMode(), BigNumberSerializeModeEnum.TO_STRING)) { + bigNumberModule.addSerializer(Long.class, ToStringSerializer.instance); + bigNumberModule.addSerializer(Long.TYPE, ToStringSerializer.instance); + bigNumberModule.addSerializer(BigInteger.class, ToStringSerializer.instance); + } + + return bigNumberModule; + } } diff --git a/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/autoconfigure/JacksonProperties.java b/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/autoconfigure/JacksonProperties.java new file mode 100644 index 00000000..52c7ec8f --- /dev/null +++ b/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/autoconfigure/JacksonProperties.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.starter.json.jackson.autoconfigure; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +import top.continew.starter.json.jackson.enums.BigNumberSerializeModeEnum; + +/** + * jackson 配置属性 + * + * @author Jasmine + * @author Charles7c + * @since 2.12.0 + */ +@ConfigurationProperties("spring.jackson") +public class JacksonProperties { + /** + * 大数值类型序列化模式 + */ + private BigNumberSerializeModeEnum bigNumberSerializeMode; + + public BigNumberSerializeModeEnum getBigNumberSerializeMode() { + return bigNumberSerializeMode; + } + + public void setBigNumberSerializeMode(BigNumberSerializeModeEnum bigNumberSerializeMode) { + this.bigNumberSerializeMode = bigNumberSerializeMode; + } +} diff --git a/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/enums/BigNumberSerializeModeEnum.java b/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/enums/BigNumberSerializeModeEnum.java new file mode 100644 index 00000000..5ad5277e --- /dev/null +++ b/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/enums/BigNumberSerializeModeEnum.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.starter.json.jackson.enums; + +/** + * 大数值序列化模式 + * + * @author Jasmine + * @author Charles7c + * @since 2.12.0 + */ +public enum BigNumberSerializeModeEnum { + /** 超过js的最大值转字符串类型,否则保持原类型 */ + FLEXIBLE, + /** 不操作 */ + NO_OPERATE, + /** 统一转String类型 */ + TO_STRING,; +} diff --git a/continew-starter-json/continew-starter-json-jackson/src/main/resources/default-json-jackson.yml b/continew-starter-json/continew-starter-json-jackson/src/main/resources/default-json-jackson.yml index 48f43891..782ae65a 100644 --- a/continew-starter-json/continew-starter-json-jackson/src/main/resources/default-json-jackson.yml +++ b/continew-starter-json/continew-starter-json-jackson/src/main/resources/default-json-jackson.yml @@ -21,3 +21,5 @@ spring: deserialization: # 允许反序列化不存在的属性 fail-on-unknown-properties: false + # 大数值序列化类型的转换 + big-number-serialize-mode: FLEXIBLE