From baa5aafe231cbe25b6a9ece00be936817ad9bd91 Mon Sep 17 00:00:00 2001 From: minglipro Date: Thu, 11 Sep 2025 19:32:51 +0800 Subject: [PATCH] no message --- gradle.properties | 2 +- .../FastjsonJsonStringConverterAdapter.java | 70 +++++++++ .../GsonJsonStringConverterAdapter.java | 50 +++++++ .../JacksonJsonStringConverterAdapter.java | 91 ++++++++++++ .../json/converters/JsonStringConverter.java | 139 +++--------------- .../autoconfigure/GsonAutoConfiguration.java | 47 ++++-- .../JacksonAutoConfiguration.java | 27 +++- .../JsonApiAutoConfiguration.java | 50 ------- ...ot.autoconfigure.AutoConfiguration.imports | 1 - 9 files changed, 289 insertions(+), 188 deletions(-) create mode 100644 src/main/java/com/mingliqiye/utils/json/converters/FastjsonJsonStringConverterAdapter.java create mode 100644 src/main/java/com/mingliqiye/utils/json/converters/GsonJsonStringConverterAdapter.java create mode 100644 src/main/java/com/mingliqiye/utils/json/converters/JacksonJsonStringConverterAdapter.java delete mode 100644 src/main/java/com/mingliqiye/utils/springboot/autoconfigure/JsonApiAutoConfiguration.java diff --git a/gradle.properties b/gradle.properties index 1c5d41e..c57beff 100644 --- a/gradle.properties +++ b/gradle.properties @@ -22,4 +22,4 @@ JDKVERSIONS=1.8 GROUPSID=com.mingliqiye.utils ARTIFACTID=mingli-utils -VERSIONS=3.1.5 +VERSIONS=3.2.3 diff --git a/src/main/java/com/mingliqiye/utils/json/converters/FastjsonJsonStringConverterAdapter.java b/src/main/java/com/mingliqiye/utils/json/converters/FastjsonJsonStringConverterAdapter.java new file mode 100644 index 0000000..b7ee705 --- /dev/null +++ b/src/main/java/com/mingliqiye/utils/json/converters/FastjsonJsonStringConverterAdapter.java @@ -0,0 +1,70 @@ +package com.mingliqiye.utils.json.converters; + +import com.alibaba.fastjson2.JSONReader; +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.reader.ObjectReader; +import com.alibaba.fastjson2.writer.ObjectWriter; + +import java.lang.reflect.Type; + +public class FastjsonJsonStringConverterAdapter< + T extends JsonStringConverter, + TT +> { + + private final JsonStringConverter jsonStringConverter; + + public FastjsonJsonStringConverterAdapter(T jsonStringConverter) { + this.jsonStringConverter = jsonStringConverter; + } + + public static < + T extends JsonStringConverter, + TT + > FastjsonJsonStringConverterAdapter of(T t) { + return new FastjsonJsonStringConverterAdapter<>(t); + } + + + + + /** + * 获取FastJson对象写入器 + * + * @return FastJson的ObjectWriter实例 + */ + public ObjectWriter getFastJsonObjectWriter() { + return ( + JSONWriter writer, + Object object, + Object fieldName, + Type fieldType, + long features + ) -> { + // 如果对象为null则写入null + if (object == null) { + writer.writeNull(); + return; + } + writer.writeString(jsonStringConverter.convert((TT) object)); + }; + } + + /** + * 获取FastJson对象读取器 + * + * @return FastJson的ObjectReader实例 + */ + public ObjectReader getFastJsonObjectReader() { + return ( + JSONReader reader, + Type fieldType, + Object fieldName, + long features + ) -> { + String value = reader.readString(); + return jsonStringConverter.deConvert(value); + }; + } + +} diff --git a/src/main/java/com/mingliqiye/utils/json/converters/GsonJsonStringConverterAdapter.java b/src/main/java/com/mingliqiye/utils/json/converters/GsonJsonStringConverterAdapter.java new file mode 100644 index 0000000..b168a14 --- /dev/null +++ b/src/main/java/com/mingliqiye/utils/json/converters/GsonJsonStringConverterAdapter.java @@ -0,0 +1,50 @@ +package com.mingliqiye.utils.json.converters; + +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +import java.io.IOException; + +public class GsonJsonStringConverterAdapter< + T extends JsonStringConverter, + TT +> { + + private final JsonStringConverter jsonStringConverter; + + public GsonJsonStringConverterAdapter(T jsonStringConverter) { + this.jsonStringConverter = jsonStringConverter; + } + + public static < + T extends JsonStringConverter, + TT + > GsonJsonStringConverterAdapter of(T t) { + return new GsonJsonStringConverterAdapter<>(t); + } + + /** + * 获取Gson类型适配器 + * + * @return Gson的TypeAdapter实例 + */ + public TypeAdapter getGsonTypeAdapter() { + return new TypeAdapter() { + @Override + public void write(JsonWriter out, TT value) throws IOException { + if (value == null) { + out.nullValue(); + return; + } + out.value(jsonStringConverter.convert(value)); + } + + @Override + public TT read(JsonReader in) throws IOException { + String value = in.nextString(); + return jsonStringConverter.deConvert(value); + } + }; + } +} diff --git a/src/main/java/com/mingliqiye/utils/json/converters/JacksonJsonStringConverterAdapter.java b/src/main/java/com/mingliqiye/utils/json/converters/JacksonJsonStringConverterAdapter.java new file mode 100644 index 0000000..d787c0f --- /dev/null +++ b/src/main/java/com/mingliqiye/utils/json/converters/JacksonJsonStringConverterAdapter.java @@ -0,0 +1,91 @@ +package com.mingliqiye.utils.json.converters; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.module.SimpleModule; +import java.io.IOException; + +/** + * JSON转换器的适配器 + * @param JSON转换器 + * @param JSON转换器的泛型 + * @author MingLiPro + */ +public class JacksonJsonStringConverterAdapter< + T extends JsonStringConverter, + TT +> { + + private final JsonStringConverter jsonStringConverter; + + private JacksonJsonStringConverterAdapter(T jsonStringConverter) { + this.jsonStringConverter = jsonStringConverter; + } + + /** + * + * @param t JSON转换器实例 + * @return JSON转换器的适配器 + * @param JSON转换器 + * @param JSON转换器的泛型 + */ + public static < + T extends JsonStringConverter, + TT + > JacksonJsonStringConverterAdapter of(T t) { + return new JacksonJsonStringConverterAdapter<>(t); + } + + /** + * 获取Jackson反序列化器 + * + * @return Jackson的JsonDeserializer实例 + */ + public JsonDeserializer getJacksonJsonDeserializer() { + return new JsonDeserializer() { + @Override + public TT deserialize(JsonParser p, DeserializationContext ctxt) + throws IOException { + if (p.isNaN()) return null; + return jsonStringConverter.deConvert(p.getValueAsString()); + } + }; + } + + /** + * 获取Jackson序列化器 + * + * @return Jackson的JsonSerializer实例 + */ + public JsonSerializer getJacksonJsonSerializer() { + return new JsonSerializer() { + @Override + public void serialize( + TT value, + JsonGenerator gen, + SerializerProvider serializers + ) throws IOException { + if (value == null) { + gen.writeNull(); + return; + } + gen.writeString(jsonStringConverter.convert(value)); + } + }; + } + + /** + * + * 获取 Jackson 的格式化模块 + * + * @return 格式化模块 + */ + public Module getJacksonModule() { + Class tClass = jsonStringConverter.getTClass(); + SimpleModule m = new SimpleModule(tClass.getSimpleName()); + m.addSerializer(tClass, getJacksonJsonSerializer()); + m.addDeserializer(tClass, getJacksonJsonDeserializer()); + return m; + } +} diff --git a/src/main/java/com/mingliqiye/utils/json/converters/JsonStringConverter.java b/src/main/java/com/mingliqiye/utils/json/converters/JsonStringConverter.java index 8944e20..bd17915 100644 --- a/src/main/java/com/mingliqiye/utils/json/converters/JsonStringConverter.java +++ b/src/main/java/com/mingliqiye/utils/json/converters/JsonStringConverter.java @@ -1,19 +1,5 @@ package com.mingliqiye.utils.json.converters; -import com.alibaba.fastjson2.JSONReader; -import com.alibaba.fastjson2.JSONWriter; -import com.alibaba.fastjson2.reader.ObjectReader; -import com.alibaba.fastjson2.writer.ObjectWriter; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.*; -import com.fasterxml.jackson.databind.module.SimpleModule; -import com.google.gson.TypeAdapter; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonWriter; -import java.io.IOException; -import java.lang.reflect.Type; - /** * JSON转换器接口,提供对象与字符串之间的相互转换功能,并支持多种JSON库 * @@ -40,118 +26,35 @@ public abstract class JsonStringConverter { abstract T deConvert(String string); /** - * 获取Jackson反序列化器 - * - * @return Jackson的JsonDeserializer实例 + * 获取 Fastjson 的适配器 + * @return 适配器实例 */ - public JsonDeserializer getJacksonJsonDeserializer() { - return new JsonDeserializer() { - @Override - public T deserialize(JsonParser p, DeserializationContext ctxt) - throws IOException { - // 如果是NaN则返回null - if (p.isNaN()) return null; - return deConvert(p.getValueAsString()); - } - }; + public FastjsonJsonStringConverterAdapter< + JsonStringConverter, + T + > getFastjsonJsonStringConverterAdapter() { + return FastjsonJsonStringConverterAdapter.of(this); } /** - * 获取Jackson序列化器 - * - * @return Jackson的JsonSerializer实例 + * 获取 Gson 的适配器 + * @return 适配器实例 */ - public JsonSerializer getJacksonJsonSerializer() { - return new JsonSerializer() { - @Override - public void serialize( - T value, - JsonGenerator gen, - SerializerProvider serializers - ) throws IOException { - // 如果值为null则写入null - if (value == null) { - gen.writeNull(); - return; - } - gen.writeString(convert(value)); - } - }; - } - - public Module getJacksonModule() { - Class tClass = getTClass(); - SimpleModule m = new SimpleModule(tClass.getSimpleName()); - m.addSerializer(tClass, getJacksonJsonSerializer()); - m.addDeserializer(tClass, getJacksonJsonDeserializer()); - return m; + public GsonJsonStringConverterAdapter< + JsonStringConverter, + T + > getGsonJsonStringConverterAdapter() { + return GsonJsonStringConverterAdapter.of(this); } /** - * 获取Gson类型适配器 - * - * @return Gson的TypeAdapter实例 + * 获取 Jackson 的适配器 + * @return 适配器实例 */ - public TypeAdapter getGsonTypeAdapter() { - return new TypeAdapter() { - @Override - public void write(JsonWriter out, T value) throws IOException { - // 如果值为null则写入null值 - if (value == null) { - out.nullValue(); - return; - } - out.value(convert(value)); - } - - @Override - public T read(JsonReader in) throws IOException { - String value = in.nextString(); - return deConvert(value); - } - }; - } - - /** - * FastJson 支持 - */ - - /** - * 获取FastJson对象写入器 - * - * @return FastJson的ObjectWriter实例 - */ - public ObjectWriter getFastJsonObjectWriter() { - return ( - JSONWriter writer, - Object object, - Object fieldName, - Type fieldType, - long features - ) -> { - // 如果对象为null则写入null - if (object == null) { - writer.writeNull(); - return; - } - writer.writeString(convert((T) object)); - }; - } - - /** - * 获取FastJson对象读取器 - * - * @return FastJson的ObjectReader实例 - */ - public ObjectReader getFastJsonObjectReader() { - return ( - JSONReader reader, - Type fieldType, - Object fieldName, - long features - ) -> { - String value = reader.readString(); - return deConvert(value); - }; + public JacksonJsonStringConverterAdapter< + JsonStringConverter, + T + > getJacksonJsonStringConverterAdapter() { + return JacksonJsonStringConverterAdapter.of(this); } } diff --git a/src/main/java/com/mingliqiye/utils/springboot/autoconfigure/GsonAutoConfiguration.java b/src/main/java/com/mingliqiye/utils/springboot/autoconfigure/GsonAutoConfiguration.java index e306a29..9a3a0f2 100644 --- a/src/main/java/com/mingliqiye/utils/springboot/autoconfigure/GsonAutoConfiguration.java +++ b/src/main/java/com/mingliqiye/utils/springboot/autoconfigure/GsonAutoConfiguration.java @@ -2,6 +2,8 @@ package com.mingliqiye.utils.springboot.autoconfigure; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.mingliqiye.utils.json.GsonJsonApi; +import com.mingliqiye.utils.json.JsonApi; import com.mingliqiye.utils.json.converters.DateTimeJsonConverter; import com.mingliqiye.utils.json.converters.JsonStringConverter; import com.mingliqiye.utils.json.converters.UUIDJsonStringConverter; @@ -13,14 +15,15 @@ import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.gson.GsonBuilderCustomizer; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Primary; @ConditionalOnClass(Gson.class) @AutoConfiguration @AutoConfigureAfter( name = { "org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration", + "com.mingliqiye.utils.springboot.autoconfigure.JacksonAutoConfiguration", } ) public class GsonAutoConfiguration { @@ -34,25 +37,37 @@ public class GsonAutoConfiguration { new DateTimeJsonConverter(); JsonStringConverter uuidJsonStringConverter = new UUIDJsonStringConverter(); - return gsonBuilder - .registerTypeAdapter( - uuidJsonStringConverter.getTClass(), - uuidJsonStringConverter.getGsonTypeAdapter() - ) - .registerTypeAdapter( - dateTimeJsonConverter.getTClass(), - dateTimeJsonConverter.getGsonTypeAdapter() - ); - } - @Bean - @Primary - @ConditionalOnMissingBean - public GsonBuilder gsonBuilder() { try { - return addTypeAdapter(new GsonBuilder()); + return gsonBuilder + .registerTypeAdapter( + uuidJsonStringConverter.getTClass(), + dateTimeJsonConverter + .getGsonJsonStringConverterAdapter() + .getGsonTypeAdapter() + ) + .registerTypeAdapter( + dateTimeJsonConverter.getTClass(), + dateTimeJsonConverter + .getGsonJsonStringConverterAdapter() + .getGsonTypeAdapter() + ); } finally { log.info("MingliUtils GsonBuilder TypeAdapter add"); } } + + @Bean + public GsonBuilderCustomizer mingliGsonCustomizer() { + return GsonAutoConfiguration::addTypeAdapter; + } + + @Bean + @ConditionalOnMissingBean + public JsonApi jsonApi(Gson gson) { + log.info( + "MingliUtils-JsonApiAutoConfiguration: GsonJsonApi bean is created." + ); + return new GsonJsonApi(gson); + } } diff --git a/src/main/java/com/mingliqiye/utils/springboot/autoconfigure/JacksonAutoConfiguration.java b/src/main/java/com/mingliqiye/utils/springboot/autoconfigure/JacksonAutoConfiguration.java index e3489c3..d4e93b8 100644 --- a/src/main/java/com/mingliqiye/utils/springboot/autoconfigure/JacksonAutoConfiguration.java +++ b/src/main/java/com/mingliqiye/utils/springboot/autoconfigure/JacksonAutoConfiguration.java @@ -1,6 +1,8 @@ package com.mingliqiye.utils.springboot.autoconfigure; import com.fasterxml.jackson.databind.ObjectMapper; +import com.mingliqiye.utils.json.JacksonJsonApi; +import com.mingliqiye.utils.json.JsonApi; import com.mingliqiye.utils.json.converters.DateTimeJsonConverter; import com.mingliqiye.utils.json.converters.UUIDJsonStringConverter; import org.slf4j.Logger; @@ -8,6 +10,9 @@ import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Primary; @ConditionalOnClass(ObjectMapper.class) @AutoConfiguration @@ -29,7 +34,25 @@ public class JacksonAutoConfiguration { public static ObjectMapper addModules(ObjectMapper objectMapper) { return objectMapper - .registerModule(new UUIDJsonStringConverter().getJacksonModule()) - .registerModule(new DateTimeJsonConverter().getJacksonModule()); + .registerModule( + new DateTimeJsonConverter() + .getJacksonJsonStringConverterAdapter() + .getJacksonModule() + ) + .registerModule( + new UUIDJsonStringConverter() + .getJacksonJsonStringConverterAdapter() + .getJacksonModule() + ); + } + + @Bean + @Primary + @ConditionalOnMissingBean + public JsonApi jsonApi(ObjectMapper objectMapper) { + log.info( + "MingliUtils-JsonApiAutoConfiguration: JacksonJsonApi bean is created." + ); + return new JacksonJsonApi(objectMapper); } } diff --git a/src/main/java/com/mingliqiye/utils/springboot/autoconfigure/JsonApiAutoConfiguration.java b/src/main/java/com/mingliqiye/utils/springboot/autoconfigure/JsonApiAutoConfiguration.java deleted file mode 100644 index e77d941..0000000 --- a/src/main/java/com/mingliqiye/utils/springboot/autoconfigure/JsonApiAutoConfiguration.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.mingliqiye.utils.springboot.autoconfigure; - -import com.mingliqiye.utils.json.JsonApi; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.autoconfigure.AutoConfiguration; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Primary; - -@AutoConfiguration -@AutoConfigureAfter( - name = { - "com.mingliqiye.utils.springboot.autoconfigure.GsonAutoConfiguration", - "com.mingliqiye.utils.springboot.autoconfigure.JacksonAutoConfiguration", - } -) -public class JsonApiAutoConfiguration { - - private static final Logger log = LoggerFactory.getLogger( - "MingliUtils-JsonApiAutoConfiguration" - ); - - @Bean - @Primary - @ConditionalOnMissingBean - @ConditionalOnClass( - name = { "com.fasterxml.jackson.databind.ObjectMapper" } - ) - public JsonApi jacksonJsonApi( - com.fasterxml.jackson.databind.ObjectMapper objectMapper - ) { - log.info( - "MingliUtils-JsonApiAutoConfiguration: JacksonJsonApi bean is created." - ); - return new com.mingliqiye.utils.json.JacksonJsonApi(objectMapper); - } - - @Bean - @ConditionalOnMissingBean - @ConditionalOnClass(name = { "com.google.gson.Gson" }) - public JsonApi gsonJsonApi(com.google.gson.Gson gson) { - log.info( - "MingliUtils-JsonApiAutoConfiguration: GsonJsonApi bean is created." - ); - return new com.mingliqiye.utils.json.GsonJsonApi(gson); - } -} diff --git a/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 16778a7..8735d74 100644 --- a/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -23,4 +23,3 @@ com.mingliqiye.utils.springboot.autoconfigure.AutoConfiguration com.mingliqiye.utils.springboot.autoconfigure.JacksonAutoConfiguration com.mingliqiye.utils.springboot.autoconfigure.GsonAutoConfiguration -com.mingliqiye.utils.springboot.autoconfigure.JsonApiAutoConfiguration