From d0887ea1f7a84e6543dc291533c47e6302c7bf77 Mon Sep 17 00:00:00 2001 From: minglipro Date: Tue, 17 Nov 2026 20:35:19 +0800 Subject: [PATCH] =?UTF-8?q?refactor(json):=20=E9=87=8D=E6=9E=84=20JSON=20?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E5=99=A8=E4=BD=93=E7=B3=BB=EF=BC=8C=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20reader/writer/judgment=20=E6=8A=BD=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除按类型划分的 BaseJson*Converter,统一由 BaseJsonConverter 与 JsonConverterUtils 处理;为 Jackson2/Jackson3 分别实现 JsonReader、 JsonWriter 与 ReaderJudgment,并同步更新 SpringBoot 自动配置与测试。 --- .../utils/exception/JsonParserException.kt | 10 +- .../com/mingliqiye/utils/json/api/JSONA.kt | 19 +--- .../utils/json/api/Jackson3JsonApi.kt | 9 +- .../utils/json/api/JacksonJsonApi.kt | 12 +-- .../mingliqiye/utils/json/api/base/JsonApi.kt | 29 +----- .../json/converters/DateTimeJsonConverter.kt | 88 +++++------------ .../NetworkEndpointJsonConverter.kt | 76 ++++---------- .../json/converters/UUIDJsonConverter.kt | 91 ++++++----------- .../json/converters/base/AnnotationGetter.kt | 59 +---------- .../base/BaseJsonBigDecimalConverter.kt | 49 ---------- .../base/BaseJsonBigIntegerConverter.kt | 45 --------- .../base/BaseJsonBooleanConverter.kt | 46 --------- .../converters/base/BaseJsonByteConverter.kt | 43 -------- .../json/converters/base/BaseJsonConverter.kt | 30 ++++-- .../base/BaseJsonDoubleConverter.kt | 42 -------- .../converters/base/BaseJsonFloatConverter.kt | 46 --------- .../converters/base/BaseJsonIntConverter.kt | 46 --------- .../converters/base/BaseJsonLongConverter.kt | 43 -------- .../converters/base/BaseJsonShortConverter.kt | 46 --------- .../base/BaseJsonStringConverter.kt | 46 --------- .../converters/base/JackJson3Deserializer.kt | 24 ++--- .../converters/base/JackJson3Serializer.kt | 43 +++----- .../converters/base/JackJsonDeserializer.kt | 33 +++---- .../converters/base/JackJsonSerializer.kt | 42 +++----- .../json/converters/base/JsonConverter.kt | 91 ----------------- .../converters/base/JsonConverterUtils.kt | 98 +++++++++---------- .../json/judgment/JackSon3ReaderJudgment.kt | 44 +++++++++ .../json/judgment/JackSonReaderJudgment.kt | 44 +++++++++ .../json/judgment/base/BaseJsonJudgment.kt | 41 ++++++++ .../json/reader/JackSon3JsonArrayReader.kt | 83 ++++++++++++++++ .../json/reader/JackSon3JsonObjectReader.kt | 90 +++++++++++++++++ .../utils/json/reader/JackSon3JsonReader.kt | 84 ++++++++++++++++ .../json/reader/JackSonJsonArrayReader.kt | 85 ++++++++++++++++ .../json/reader/JackSonJsonObjectReader.kt | 90 +++++++++++++++++ .../utils/json/reader/JackSonJsonReader.kt | 84 ++++++++++++++++ .../json/reader/base/BaseJsonArrayReader.kt | 78 +++++++++++++++ .../json/reader/base/BaseJsonObjectReader.kt | 82 ++++++++++++++++ .../utils/json/reader/base/BaseJsonReader.kt | 43 ++++++++ .../utils/json/witer/JackSon3ArrayWriter.kt | 69 +++++++++++++ .../utils/json/witer/JackSon3JsonWriter.kt | 70 +++++++++++++ .../utils/json/witer/JackSon3ObjectWriter.kt | 89 +++++++++++++++++ .../utils/json/witer/JackSonArrayWriter.kt | 69 +++++++++++++ .../utils/json/witer/JackSonJsonWriter.kt | 70 +++++++++++++ .../utils/json/witer/JackSonObjectWriter.kt | 89 +++++++++++++++++ .../json/witer/base/BaseJsonArrayWriter.kt | 58 +++++++++++ .../json/witer/base/BaseJsonObjectWriter.kt | 59 +++++++++++ .../utils/json/witer/base/BaseJsonWriter.kt | 35 +++++++ .../autoconfigure/JsonAutoConfiguration.kt | 8 +- .../builder/JacksonJsonBuilder.kt | 8 +- .../builder/JacksonJsonBuilderImpl.kt | 12 +-- .../com/mingliqiye/utils/time/DateTime.kt | 12 ++- .../com/mingliqiye/utils/tuple/Tuples.kt | 14 +-- .../kotlin/com/mingliqiye/utils/TestJson.kt | 31 +++++- 53 files changed, 1721 insertions(+), 1026 deletions(-) delete mode 100644 src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonBigDecimalConverter.kt delete mode 100644 src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonBigIntegerConverter.kt delete mode 100644 src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonBooleanConverter.kt delete mode 100644 src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonByteConverter.kt delete mode 100644 src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonDoubleConverter.kt delete mode 100644 src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonFloatConverter.kt delete mode 100644 src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonIntConverter.kt delete mode 100644 src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonLongConverter.kt delete mode 100644 src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonShortConverter.kt delete mode 100644 src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonStringConverter.kt delete mode 100644 src/main/kotlin/com/mingliqiye/utils/json/converters/base/JsonConverter.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/judgment/JackSon3ReaderJudgment.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/judgment/JackSonReaderJudgment.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/judgment/base/BaseJsonJudgment.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/reader/JackSon3JsonArrayReader.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/reader/JackSon3JsonObjectReader.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/reader/JackSon3JsonReader.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/reader/JackSonJsonArrayReader.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/reader/JackSonJsonObjectReader.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/reader/JackSonJsonReader.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/reader/base/BaseJsonArrayReader.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/reader/base/BaseJsonObjectReader.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/reader/base/BaseJsonReader.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/witer/JackSon3ArrayWriter.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/witer/JackSon3JsonWriter.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/witer/JackSon3ObjectWriter.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/witer/JackSonArrayWriter.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/witer/JackSonJsonWriter.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/witer/JackSonObjectWriter.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/witer/base/BaseJsonArrayWriter.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/witer/base/BaseJsonObjectWriter.kt create mode 100644 src/main/kotlin/com/mingliqiye/utils/json/witer/base/BaseJsonWriter.kt diff --git a/src/main/kotlin/com/mingliqiye/utils/exception/JsonParserException.kt b/src/main/kotlin/com/mingliqiye/utils/exception/JsonParserException.kt index 1134d58..783a591 100644 --- a/src/main/kotlin/com/mingliqiye/utils/exception/JsonParserException.kt +++ b/src/main/kotlin/com/mingliqiye/utils/exception/JsonParserException.kt @@ -16,7 +16,7 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile JsonParserException.kt - * LastUpdate 2026-10-11 16:53:41 + * LastUpdate 2026-11-17 16:22:52 * UpdateUser MingLiPro */ @@ -28,3 +28,11 @@ class JsonParserException @JvmOverloads constructor( enableSuppression: Boolean = false, writableStackTrace: Boolean = true, ) : JsonException(message, throwable, enableSuppression, writableStackTrace) + + +class JsonFormatException @JvmOverloads constructor( + message: String? = null, + throwable: Throwable? = null, + enableSuppression: Boolean = false, + writableStackTrace: Boolean = true, +) : JsonException(message, throwable, enableSuppression, writableStackTrace) diff --git a/src/main/kotlin/com/mingliqiye/utils/json/api/JSONA.kt b/src/main/kotlin/com/mingliqiye/utils/json/api/JSONA.kt index 9aaef96..dbc3730 100644 --- a/src/main/kotlin/com/mingliqiye/utils/json/api/JSONA.kt +++ b/src/main/kotlin/com/mingliqiye/utils/json/api/JSONA.kt @@ -16,7 +16,7 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile JSONA.kt - * LastUpdate 2026-11-07 13:29:54 + * LastUpdate 2026-11-17 19:59:21 * UpdateUser MingLiPro */ @@ -29,8 +29,6 @@ import com.mingliqiye.utils.json.api.JSONA.setJsonApi import com.mingliqiye.utils.json.api.base.JsonApi import com.mingliqiye.utils.json.api.type.JsonTypeReference import com.mingliqiye.utils.json.api.type.listType -import com.mingliqiye.utils.json.converters.base.BaseJsonConverter -import com.mingliqiye.utils.json.converters.base.getJsonConverter import com.mingliqiye.utils.json.objects.base.JsonItem import java.io.File import java.io.IOException @@ -522,21 +520,6 @@ object JSONA { fun convert(source: Any, destinationType: JsonTypeReference): D = jsonApi.convert(source, destinationType) - /** - * 添加 JSON 转换器到 API 中。 - * - * @param c 需要添加的 [BaseJsonConverter] 实例 - */ - @JvmStatic - fun addJsonConverter(c: BaseJsonConverter<*, *>) = jsonApi.addJsonConverter(c) - - /** - * 使用反射创建并添加指定类型的 JSON 转换器到 API 中。 - * - * @param T 继承自 [BaseJsonConverter] 的具体实现类 - */ - inline fun > addJsonConverter() = addJsonConverter(getJsonConverter()) - /** * 将当前字符串解析为指定泛型类型的对象。 * diff --git a/src/main/kotlin/com/mingliqiye/utils/json/api/Jackson3JsonApi.kt b/src/main/kotlin/com/mingliqiye/utils/json/api/Jackson3JsonApi.kt index 9c7ac9a..35fddb9 100644 --- a/src/main/kotlin/com/mingliqiye/utils/json/api/Jackson3JsonApi.kt +++ b/src/main/kotlin/com/mingliqiye/utils/json/api/Jackson3JsonApi.kt @@ -16,7 +16,7 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile Jackson3JsonApi.kt - * LastUpdate 2026-10-12 13:59:59 + * LastUpdate 2026-11-17 19:59:59 * UpdateUser MingLiPro */ @@ -26,8 +26,6 @@ import com.mingliqiye.utils.exception.JsonException import com.mingliqiye.utils.json.api.JackSon3JsonItem.Companion.getJsonItem import com.mingliqiye.utils.json.api.base.JsonApi import com.mingliqiye.utils.json.api.type.JsonTypeReference -import com.mingliqiye.utils.json.converters.base.BaseJsonConverter -import com.mingliqiye.utils.json.converters.base.getJackson3Module import com.mingliqiye.utils.json.objects.base.* import tools.jackson.core.type.TypeReference import tools.jackson.databind.JsonNode @@ -213,11 +211,6 @@ class Jackson3JsonApi : JsonApi { return current } - override fun addJsonConverter(c: BaseJsonConverter<*, *>) { - val mapperBuilder = Jackson3ApiJavaHelper.rebuild(objectMapper) - mapperBuilder.addModule(c.getJackson3Module()) - this.objectMapper = mapperBuilder.build() - } override fun parseJsonItem(json: String): JsonItem { try { diff --git a/src/main/kotlin/com/mingliqiye/utils/json/api/JacksonJsonApi.kt b/src/main/kotlin/com/mingliqiye/utils/json/api/JacksonJsonApi.kt index 29b1742..b61e377 100644 --- a/src/main/kotlin/com/mingliqiye/utils/json/api/JacksonJsonApi.kt +++ b/src/main/kotlin/com/mingliqiye/utils/json/api/JacksonJsonApi.kt @@ -16,7 +16,7 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile JacksonJsonApi.kt - * LastUpdate 2026-10-12 10:48:31 + * LastUpdate 2026-11-17 19:59:59 * UpdateUser MingLiPro */ @@ -33,8 +33,6 @@ import com.mingliqiye.utils.exception.JsonException import com.mingliqiye.utils.json.api.JackSonJsonItem.Companion.getJsonItem import com.mingliqiye.utils.json.api.base.JsonApi import com.mingliqiye.utils.json.api.type.JsonTypeReference -import com.mingliqiye.utils.json.converters.base.BaseJsonConverter -import com.mingliqiye.utils.json.converters.base.getJacksonModule import com.mingliqiye.utils.json.objects.base.* /** @@ -289,14 +287,6 @@ class JacksonJsonApi : JsonApi { } } - override fun addJsonConverter(c: BaseJsonConverter<*, *>) { - try { - objectMapper.registerModule(c.getJacksonModule()) - } catch (e: Exception) { - throw JsonException("Failed to update node value", e) - } - } - override fun parseJsonItem(json: String): JsonItem { try { return getJsonItem(objectMapper.readTree(json)) diff --git a/src/main/kotlin/com/mingliqiye/utils/json/api/base/JsonApi.kt b/src/main/kotlin/com/mingliqiye/utils/json/api/base/JsonApi.kt index a80d3eb..e541239 100644 --- a/src/main/kotlin/com/mingliqiye/utils/json/api/base/JsonApi.kt +++ b/src/main/kotlin/com/mingliqiye/utils/json/api/base/JsonApi.kt @@ -16,7 +16,7 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile JsonApi.kt - * LastUpdate 2026-03-27 14:19:31 + * LastUpdate 2026-11-17 19:59:21 * UpdateUser MingLiPro */ @@ -25,8 +25,6 @@ package com.mingliqiye.utils.json.api.base import com.mingliqiye.utils.exception.JsonParserException import com.mingliqiye.utils.json.api.type.JsonTypeReference import com.mingliqiye.utils.json.api.type.listType -import com.mingliqiye.utils.json.converters.base.BaseJsonConverter -import com.mingliqiye.utils.json.converters.base.getJsonConverter import com.mingliqiye.utils.json.objects.base.JsonItem import okio.IOException import java.io.* @@ -131,17 +129,6 @@ interface JsonApi { */ @JvmStatic inline fun JsonApi.convert(source: Any): D = convert(source, object : JsonTypeReference() {}) - - /** - * 添加JSON转换器到API中 - * - * @param T 需要添加的JSON转换器类 - */ - @JvmStatic - inline fun > JsonApi.addJsonConverter() { - addJsonConverter(getJsonConverter()) - } - } /** @@ -513,20 +500,6 @@ interface JsonApi { */ fun convert(source: Any, destinationType: JsonTypeReference): D - /** - * 注册自定义的 JSON 转换器 - * 用于处理特定类型的自定义序列化和反序列化逻辑 - * - * @param c 自定义的 JSON 转换器实例 - * - * 使用示例: - * ``` - * converter.addJsonConverter(DateJsonConverter()) - * converter.addJsonConverter(LocalDateTimeJsonConverter()) - * ``` - */ - fun addJsonConverter(c: BaseJsonConverter<*, *>) - /** * 将 JSON 字符串解析为 JsonItem 对象 * diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/DateTimeJsonConverter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/DateTimeJsonConverter.kt index f1fce15..bef529c 100644 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/DateTimeJsonConverter.kt +++ b/src/main/kotlin/com/mingliqiye/utils/json/converters/DateTimeJsonConverter.kt @@ -16,82 +16,51 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile DateTimeJsonConverter.kt - * LastUpdate 2026-11-09 14:35:06 + * LastUpdate 2026-11-17 20:17:32 * UpdateUser MingLiPro */ package com.mingliqiye.utils.json.converters +import com.google.gson.JsonParseException import com.mingliqiye.utils.annotation.DateTimeJsonFormat import com.mingliqiye.utils.json.api.type.JsonTypeReference import com.mingliqiye.utils.json.converters.base.AnnotationGetter -import com.mingliqiye.utils.json.converters.base.AnnotationGetter.Companion.get -import com.mingliqiye.utils.json.converters.base.BaseJsonStringConverter -import com.mingliqiye.utils.objects.isNull -import com.mingliqiye.utils.string.isNullish +import com.mingliqiye.utils.json.converters.base.BaseJsonConverter +import com.mingliqiye.utils.json.converters.base.getAnnotation +import com.mingliqiye.utils.json.reader.base.BaseJsonReader +import com.mingliqiye.utils.json.witer.base.BaseJsonWriter import com.mingliqiye.utils.time.DateTime import com.mingliqiye.utils.time.Formatter - -/** - * DateTimeJsonConverter 是一个用于处理 DateTime 类型与 JSON 字符串之间转换的类。 - * 它继承自 BaseJsonStringConverter,提供了序列化(convert)和反序列化(deConvert)的功能。 - */ -class DateTimeJsonConverter private constructor() : BaseJsonStringConverter { - - - companion object { - private val dateTimeJsonConverter by lazy { - DateTimeJsonConverter() - } - - @JvmStatic - fun getJsonConverter(): DateTimeJsonConverter = dateTimeJsonConverter - } - - /** - * 将 DateTime 对象转换为 JSON 字符串。 - * - * @param obj 需要转换的 DateTime 对象,可能为 null。 - * @param annotationGetter 用于获取注解信息的对象,用于决定格式化方式。 - * @return 转换后的字符串,如果输入为 null 则返回 null。 - * @throws Exception 如果在转换过程中发生异常。 - */ +class DateTimeJsonConverter : BaseJsonConverter { @Throws(Exception::class) - override fun convert(obj: DateTime?, annotationGetter: AnnotationGetter): String? { - // 如果输入对象为 null,直接返回 null - if (obj.isNull()) return null - - // 获取 DateTimeJsonFormat 注解信息 - val dateTimeJsonFormat: DateTimeJsonFormat? = annotationGetter.get() - - // 根据注解中的格式化规则进行转换 - return if (Formatter.NONE != dateTimeJsonFormat?.value && dateTimeJsonFormat != null) { + override fun convert( + obj: DateTime?, + jsonWriter: BaseJsonWriter, + annotationGetter: AnnotationGetter, + ) { + if (obj == null) { + jsonWriter.setNull() + return + } + val dateTimeJsonFormat: DateTimeJsonFormat? = annotationGetter.getAnnotation() + val value = if (Formatter.NONE != dateTimeJsonFormat?.value && dateTimeJsonFormat != null) { obj.format(dateTimeJsonFormat.value, dateTimeJsonFormat.repcZero) } else if (dateTimeJsonFormat?.formatter?.isEmpty() == false) { obj.format(dateTimeJsonFormat.formatter, dateTimeJsonFormat.repcZero) } else { obj.format() } + jsonWriter.setString(value) } - /** - * 将 JSON 字符串转换为 DateTime 对象。 - * - * @param obj 需要转换的字符串,可能为 null 或空。 - * @param annotationGetter 用于获取注解信息的对象,用于决定解析方式。 - * @return 解析后的 DateTime 对象,如果输入为 null 或空则返回 null。 - * @throws Exception 如果在解析过程中发生异常。 - */ - @Throws(Exception::class) - override fun deConvert(obj: String?, annotationGetter: AnnotationGetter): DateTime? { - // 如果输入字符串为 null 或空,直接返回 null - if (obj.isNullish()) return null - // 获取 DateTimeJsonFormat 注解信息 - val dateTimeJsonFormat: DateTimeJsonFormat? = annotationGetter.get() - - // 根据注解中的解析规则进行转换 + override fun deConvert(jsonReader: BaseJsonReader, annotationGetter: AnnotationGetter): DateTime? { + val dateTimeJsonFormat: DateTimeJsonFormat? = annotationGetter.getAnnotation() + if (jsonReader.isNull) return null + if (!jsonReader.isString) throw JsonParseException("type item not string") + val obj = jsonReader.getString() return if (Formatter.NONE != dateTimeJsonFormat?.value && dateTimeJsonFormat != null) { DateTime.parse(obj, dateTimeJsonFormat.value, dateTimeJsonFormat.repcZero) } else if (dateTimeJsonFormat?.formatter?.isEmpty() == false) { @@ -101,12 +70,5 @@ class DateTimeJsonConverter private constructor() : BaseJsonStringConverter = JsonTypeReference.of() + override fun getType(): JsonTypeReference = JsonTypeReference.of() } diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/NetworkEndpointJsonConverter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/NetworkEndpointJsonConverter.kt index 091e754..ff0dd0e 100644 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/NetworkEndpointJsonConverter.kt +++ b/src/main/kotlin/com/mingliqiye/utils/json/converters/NetworkEndpointJsonConverter.kt @@ -16,16 +16,18 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile NetworkEndpointJsonConverter.kt - * LastUpdate 2026-11-09 14:36:36 + * LastUpdate 2026-11-17 20:01:08 * UpdateUser MingLiPro */ package com.mingliqiye.utils.json.converters +import com.google.gson.JsonParseException import com.mingliqiye.utils.json.api.type.JsonTypeReference -import com.mingliqiye.utils.json.converters.NetworkEndpointJsonConverter.Companion.getJsonConverter import com.mingliqiye.utils.json.converters.base.AnnotationGetter -import com.mingliqiye.utils.json.converters.base.BaseJsonStringConverter +import com.mingliqiye.utils.json.converters.base.BaseJsonConverter +import com.mingliqiye.utils.json.reader.base.BaseJsonReader +import com.mingliqiye.utils.json.witer.base.BaseJsonWriter import com.mingliqiye.utils.network.NetworkEndpoint /** @@ -41,60 +43,24 @@ import com.mingliqiye.utils.network.NetworkEndpoint * @see BaseJsonStringConverter * @see NetworkEndpoint */ -class NetworkEndpointJsonConverter private constructor() : BaseJsonStringConverter { - - companion object { - private val networkEndpointJsonConverter by lazy { - NetworkEndpointJsonConverter() - } - - /** - * 获取 [NetworkEndpointJsonConverter] 的全局单例。 - * - * @return 共享的转换器实例。 - */ - @JvmStatic - fun getJsonConverter(): NetworkEndpointJsonConverter = networkEndpointJsonConverter +class NetworkEndpointJsonConverter : BaseJsonConverter { + override fun convert( + obj: NetworkEndpoint?, + jsonWriter: BaseJsonWriter, + annotationGetter: AnnotationGetter, + ) { + if (obj == null) jsonWriter.setNull() else jsonWriter.setString(obj.toHostPortString()) } - /** - * 将 [NetworkEndpoint] 序列化为 JSON 字符串。 - * - * 输出格式为 `host:port`,由 [NetworkEndpoint.toHostPortString] 生成。 - * - * @param obj 待序列化的网络端点,可能为 `null`。 - * @param annotationGetter 注解信息提供者,用于在需要时读取格式化相关注解; - * 当前实现不依赖注解,保留该参数以符合转换器契约。 - * @return 序列化后的 `host:port` 字符串;当 [obj] 为 `null` 时返回 `null`。 - * @throws Exception 若序列化过程中发生异常。 - */ - @Throws(Exception::class) - override fun convert(obj: NetworkEndpoint?, annotationGetter: AnnotationGetter): String? { - return obj?.let(NetworkEndpoint::toHostPortString) + override fun deConvert( + jsonReader: BaseJsonReader, + annotationGetter: AnnotationGetter, + ): NetworkEndpoint? { + return if (jsonReader.isNotNull) { + if (!jsonReader.isString) throw JsonParseException("type item not string") + NetworkEndpoint.of(jsonReader.getString()) + } else null } - /** - * 将 JSON 字符串反序列化为 [NetworkEndpoint]。 - * - * 期望输入为 `host:port` 形式的字符串,由 [NetworkEndpoint.Companion.of] 解析。 - * - * @param obj 待反序列化的字符串,可能为 `null`。 - * @param annotationGetter 注解信息提供者,用于在需要时读取解析相关注解; - * 当前实现不依赖注解,保留该参数以符合转换器契约。 - * @return 解析得到的 [NetworkEndpoint];当 [obj] 为 `null` 时返回 `null`。 - * @throws Exception 若字符串格式非法或解析过程中发生异常。 - */ - @Throws(Exception::class) - override fun deConvert(obj: String?, annotationGetter: AnnotationGetter): NetworkEndpoint? { - return obj?.let(NetworkEndpoint.Companion::of) - } - - /** - * 获取本转换器支持的目标类型引用。 - * - * @return 指向 [NetworkEndpoint] 的 [JsonTypeReference]。 - * @throws Exception 若获取类型引用时发生异常。 - */ - @Throws(Exception::class) - override fun getFromType(): JsonTypeReference = JsonTypeReference.Companion.of() + override fun getType(): JsonTypeReference = JsonTypeReference.of() } diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/UUIDJsonConverter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/UUIDJsonConverter.kt index 28de21f..0c60f87 100644 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/UUIDJsonConverter.kt +++ b/src/main/kotlin/com/mingliqiye/utils/json/converters/UUIDJsonConverter.kt @@ -16,84 +16,60 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile UUIDJsonConverter.kt - * LastUpdate 2026-02-08 01:37:00 + * LastUpdate 2026-11-17 20:01:08 * UpdateUser MingLiPro */ package com.mingliqiye.utils.json.converters +import com.google.gson.JsonParseException import com.mingliqiye.utils.annotation.UUIDJsonFormat import com.mingliqiye.utils.base.BaseType import com.mingliqiye.utils.json.api.type.JsonTypeReference import com.mingliqiye.utils.json.converters.base.AnnotationGetter -import com.mingliqiye.utils.json.converters.base.AnnotationGetter.Companion.get -import com.mingliqiye.utils.json.converters.base.BaseJsonStringConverter -import com.mingliqiye.utils.objects.isNull -import com.mingliqiye.utils.string.isNullish +import com.mingliqiye.utils.json.converters.base.BaseJsonConverter +import com.mingliqiye.utils.json.converters.base.getAnnotation +import com.mingliqiye.utils.json.reader.base.BaseJsonReader +import com.mingliqiye.utils.json.witer.base.BaseJsonWriter import com.mingliqiye.utils.uuid.UUID import com.mingliqiye.utils.uuid.UUIDFormatType -/** - * UUIDJsonConverter 是一个用于处理 UUID 类型与 JSON 字符串之间转换的类。 - * 它继承自 BaseJsonStringConverter,并实现了 convert 和 deConvert 方法, - * 分别用于将 UUID 对象序列化为字符串以及反序列化字符串为 UUID 对象。 - */ -class UUIDJsonConverter private constructor() : BaseJsonStringConverter { +class UUIDJsonConverter : BaseJsonConverter { - companion object { - private val uuidJsonConverter by lazy { - UUIDJsonConverter() - } - - @JvmStatic - fun getJsonConverter(): UUIDJsonConverter = uuidJsonConverter - } - - /** - * 将 UUID 对象转换为字符串形式。 - * - * @param obj 需要转换的 UUID 对象,可能为 null。 - * @param annotationGetter 用于获取注解信息的对象。 - * @return 转换后的字符串,如果输入为 null 则返回 null。 - */ override fun convert( obj: UUID?, + jsonWriter: BaseJsonWriter, annotationGetter: AnnotationGetter, - ): String? { - // 如果输入对象为 null,直接返回 null - if (obj.isNull()) return null - - // 获取 UUIDJsonFormat 注解信息,若未找到则使用默认格式 - val dateTimeJsonFormat: UUIDJsonFormat = annotationGetter.get() ?: return obj.getString() - - // 根据注解中的 base 和 value 属性选择合适的字符串表示方式 - return if (BaseType.BASE16 != dateTimeJsonFormat.base) { - obj.getString(dateTimeJsonFormat.base) - } else if (UUIDFormatType.NO_UPPER_SPACE != dateTimeJsonFormat.value) { - obj.getString(dateTimeJsonFormat.value) - } else { - obj.getString() + ) { + if (obj == null) { + jsonWriter.setNull() + return } + val dateTimeJsonFormat: UUIDJsonFormat? = annotationGetter.getAnnotation() + + + val uuid = + if (dateTimeJsonFormat == null) obj.getString() else if (BaseType.BASE16 != dateTimeJsonFormat.base) { + obj.getString(dateTimeJsonFormat.base) + } else if (UUIDFormatType.NO_UPPER_SPACE != dateTimeJsonFormat.value) { + obj.getString(dateTimeJsonFormat.value) + } else { + obj.getString() + } + + jsonWriter.setString(uuid) } - /** - * 将字符串反序列化为 UUID 对象。 - * - * @param obj 需要反序列化的字符串,可能为 null 或空。 - * @param annotationGetter 用于获取注解信息的对象。 - * @return 反序列化后的 UUID 对象,如果输入无效则返回 null。 - */ override fun deConvert( - obj: String?, + jsonReader: BaseJsonReader, annotationGetter: AnnotationGetter, ): UUID? { - // 如果输入字符串为 null 或空,直接返回 null - if (obj.isNullish()) return null + if (jsonReader.isNull) return null + if (!jsonReader.isString) throw JsonParseException("type item not string") - // 获取 UUIDJsonFormat 注解信息,若未找到则使用默认解析方式 - val dateTimeJsonFormat: UUIDJsonFormat = annotationGetter.get() ?: return UUID.of(obj) + val obj = jsonReader.getString() - // 根据注解中的 base 属性选择合适的解析方式 + val dateTimeJsonFormat: UUIDJsonFormat = annotationGetter.getAnnotation() ?: return UUID.of(obj) return if (BaseType.BASE16 != dateTimeJsonFormat.base) { UUID.of(obj, dateTimeJsonFormat.base) } else { @@ -101,10 +77,5 @@ class UUIDJsonConverter private constructor() : BaseJsonStringConverter { } } - /** - * 返回当前转换器支持的目标类型引用。 - * - * @return 表示 UUID 类型的 JsonTypeReference 对象。 - */ - override fun getFromType() = JsonTypeReference.of() + override fun getType(): JsonTypeReference = JsonTypeReference.of() } diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/AnnotationGetter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/AnnotationGetter.kt index 2087426..fa4b5ba 100644 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/AnnotationGetter.kt +++ b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/AnnotationGetter.kt @@ -16,67 +16,12 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile AnnotationGetter.kt - * LastUpdate 2026-03-11 08:46:40 + * LastUpdate 2026-11-17 19:46:16 * UpdateUser MingLiPro */ package com.mingliqiye.utils.json.converters.base -/** - * 定义一个用于获取注解的接口。 - * 提供了一个通用方法来根据注解类型获取对应的注解实例。 - */ interface AnnotationGetter { - /** - * 根据指定的注解类型获取该类型的注解实例。 - * - * @param clazz 注解的Class对象,表示要获取的注解类型。 - * @return 返回对应类型的注解实例,如果未找到则返回null。 - */ - fun get(clazz: Class): T? - - companion object { - /** - * 使用内联函数和重ified类型参数简化注解获取操作。 - * 通过传入泛型参数自动推断注解类型,避免手动传递Class对象。 - * - * @param T 注解类型,必须继承自Annotation。 - * @return 返回对应类型的注解实例,如果未找到则返回null。 - */ - inline fun AnnotationGetter.get(): T? = this.get(T::class.java) - - /** - * 提供一个默认实现,始终返回null。 - * 可用于需要空注解获取器的场景。 - */ - val nullGetter = object : AnnotationGetter { - /** - * 始终返回null,不执行任何实际逻辑。 - * - * @param clazz 注解的Class对象(未使用)。 - * @return 始终返回null。 - */ - override fun get(clazz: Class): T? = null - } - - - /** - * 创建一个AnnotationGetter对象,用于根据指定的注解类型获取对应的注解实例。 - * - * @param annotation 要包装的注解实例,作为内部逻辑的来源。 - * @return 返回一个实现了AnnotationGetter接口的匿名对象。 - */ - fun oneGetter(annotation: Annotation) = object : AnnotationGetter { - /** - * 根据传入的注解类型clazz,尝试返回与annotation匹配的注解实例。 - * - * @param clazz 目标注解类型的Class对象。 - * @return 如果annotation的类型与clazz一致,则返回annotation转换为T类型的实例;否则返回null。 - */ - @Suppress("UNCHECKED_CAST") - override fun get(clazz: Class): T? = - if (annotation.javaClass == clazz) annotation as T else null - } - - } + fun getAnnotation(clazz: Class): T? = null } diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonBigDecimalConverter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonBigDecimalConverter.kt deleted file mode 100644 index 00f58db..0000000 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonBigDecimalConverter.kt +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2026 mingliqiye - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - * ProjectName mingli-utils - * ModuleName mingli-utils.main - * CurrentFile BaseJsonBigDecimalConverter.kt - * LastUpdate 2026-02-05 11:20:59 - * UpdateUser MingLiPro - */ - -package com.mingliqiye.utils.json.converters.base - -import com.mingliqiye.utils.json.api.type.JsonTypeReference -import java.math.BigDecimal - -/** - * 接口 [BaseJsonBigDecimalConverter] 定义了一个用于将 JSON 数据转换为 [BigDecimal] 类型的转换器。 - * - * 此接口继承自 [BaseJsonConverter],并指定目标类型为 [BigDecimal]。 - * 实现此接口的类需要提供具体的转换逻辑。 - * - * @param E 转换器处理的源数据类型。 - */ -interface BaseJsonBigDecimalConverter : BaseJsonConverter { - - /** - * 获取目标类型的引用信息。 - * - * 该方法返回一个 [JsonTypeReference] 对象,表示目标类型为 [BigDecimal]。 - * 通过调用 [JsonTypeReference.Companion.of] 方法创建类型引用。 - * - * @return 目标类型 [BigDecimal] 的类型引用。 - * @throws Exception 如果在获取类型引用时发生异常。 - */ - @Throws(Exception::class) - override fun getToType() = JsonTypeReference.of() -} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonBigIntegerConverter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonBigIntegerConverter.kt deleted file mode 100644 index 265099f..0000000 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonBigIntegerConverter.kt +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2026 mingliqiye - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - * ProjectName mingli-utils - * ModuleName mingli-utils.main - * CurrentFile BaseJsonBigIntegerConverter.kt - * LastUpdate 2026-02-05 11:20:59 - * UpdateUser MingLiPro - */ - -package com.mingliqiye.utils.json.converters.base - -import com.mingliqiye.utils.json.api.type.JsonTypeReference -import java.math.BigInteger - -/** - * 接口 [BaseJsonBigIntegerConverter] 定义了一个用于将 JSON 数据转换为 `BigInteger` 类型的转换器。 - * 该接口继承自 [BaseJsonConverter],并指定了泛型参数 [E] 和 `BigInteger`。 - * - * @param E 表示需要转换的目标类型,由实现类具体指定。 - */ -interface BaseJsonBigIntegerConverter : BaseJsonConverter { - - /** - * 获取目标类型的引用,用于 JSON 反序列化时确定目标类型。 - * 该方法重写了父接口中的 [getToType] 方法,并返回 `BigInteger` 类型的引用。 - * - * @return 返回 [JsonTypeReference] 对象,表示 `BigInteger` 类型的引用。 - * @throws Exception 如果在获取类型引用过程中发生异常,则抛出该异常。 - */ - @Throws(Exception::class) - override fun getToType() = JsonTypeReference.of() -} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonBooleanConverter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonBooleanConverter.kt deleted file mode 100644 index 37efe87..0000000 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonBooleanConverter.kt +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2026 mingliqiye - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - * ProjectName mingli-utils - * ModuleName mingli-utils.main - * CurrentFile BaseJsonBooleanConverter.kt - * LastUpdate 2026-02-05 11:20:59 - * UpdateUser MingLiPro - */ - -package com.mingliqiye.utils.json.converters.base - -import com.mingliqiye.utils.json.api.type.JsonTypeReference - -/** - * 接口 [BaseJsonBooleanConverter] 定义了一个用于处理布尔类型 JSON 转换的基础转换器。 - * - * 该接口继承自 [BaseJsonConverter],并专门用于将某种类型 [E] 转换为布尔类型 [Boolean]。 - * 实现该接口的类需要提供具体的转换逻辑。 - */ -interface BaseJsonBooleanConverter : BaseJsonConverter { - - /** - * 获取目标类型的引用信息。 - * - * 该方法返回一个 [JsonTypeReference] 对象,表示目标类型为 [Boolean]。 - * 此方法重写了父接口中的定义,并通过 [JsonTypeReference.Companion.of] 方法获取布尔类型的引用。 - * - * @return [JsonTypeReference] 表示布尔类型的引用。 - * @throws Exception 如果在获取类型引用时发生异常,则抛出此异常。 - */ - @Throws(Exception::class) - override fun getToType() = JsonTypeReference.of() -} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonByteConverter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonByteConverter.kt deleted file mode 100644 index e268740..0000000 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonByteConverter.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2026 mingliqiye - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - * ProjectName mingli-utils - * ModuleName mingli-utils.main - * CurrentFile BaseJsonByteConverter.kt - * LastUpdate 2026-02-05 11:20:59 - * UpdateUser MingLiPro - */ - -package com.mingliqiye.utils.json.converters.base - -import com.mingliqiye.utils.json.api.type.JsonTypeReference - -/** - * BaseJsonByteConverter 是一个接口,继承自 BaseJsonConverter,用于定义字节类型(Byte)的 JSON 转换器。 - * - * 该接口的主要作用是提供一个标准的字节类型转换实现,并通过 getToType 方法返回目标类型的引用。 - * - * @param E 泛型参数,表示需要转换的目标对象类型。 - */ -interface BaseJsonByteConverter : BaseJsonConverter { - /** - * 获取目标类型的引用,用于标识转换的目标类型为 Byte。 - * - * @return 返回 JsonTypeReference 类型的实例,表示目标类型为 Byte。 - * @throws Exception 如果在获取类型引用过程中发生异常,则抛出该异常。 - */ - @Throws(Exception::class) - override fun getToType() = JsonTypeReference.of() -} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonConverter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonConverter.kt index 5fc564a..96983d4 100644 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonConverter.kt +++ b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonConverter.kt @@ -16,19 +16,33 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile BaseJsonConverter.kt - * LastUpdate 2026-02-26 15:59:35 + * LastUpdate 2026-11-17 20:10:50 * UpdateUser MingLiPro */ package com.mingliqiye.utils.json.converters.base +import com.mingliqiye.utils.json.api.type.JsonTypeReference +import com.mingliqiye.utils.json.reader.base.BaseJsonReader +import com.mingliqiye.utils.json.witer.base.BaseJsonWriter + /** - * BaseJsonConverter 是一个泛型接口,用于定义 JSON 转换器的基本行为。 + * BaseJsonConverter 是一个通用的 JSON 转换器接口,用于定义对象之间的双向转换逻辑。 * - * 该接口继承自 JackSonJsonConverter,并通过泛型参数 F 和 T 定义了转换的源类型和目标类型。 - * 实现该接口的类需要提供具体的 JSON 转换逻辑。 - * - * @param F 源类型,表示需要被转换的数据类型。 - * @param T 目标类型,表示转换后的数据类型。 + * @param F 源类型,表示需要被转换的对象类型。 + * @param T 目标类型,表示转换后的对象类型。 */ -interface BaseJsonConverter : JsonConverter +interface BaseJsonConverter { + + @Throws(Exception::class) + fun convert(obj: T?, jsonWriter: BaseJsonWriter, annotationGetter: AnnotationGetter) + + @Throws(Exception::class) + fun deConvert(jsonReader: BaseJsonReader, annotationGetter: AnnotationGetter): T? + + @Throws(Exception::class) + fun getType(): JsonTypeReference + + @Throws(Exception::class) + fun getClazz() = getType().getRawType() +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonDoubleConverter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonDoubleConverter.kt deleted file mode 100644 index ab47938..0000000 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonDoubleConverter.kt +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2026 mingliqiye - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - * ProjectName mingli-utils - * ModuleName mingli-utils.main - * CurrentFile BaseJsonDoubleConverter.kt - * LastUpdate 2026-02-05 11:20:59 - * UpdateUser MingLiPro - */ -package com.mingliqiye.utils.json.converters.base - -import com.mingliqiye.utils.json.api.type.JsonTypeReference - -/** - * BaseJsonDoubleConverter 是一个接口,继承自 BaseJsonConverter, - * 用于定义将 JSON 数据转换为 Double 类型的转换器。 - * - * @param E 泛型参数,表示需要转换的目标类型。 - */ -interface BaseJsonDoubleConverter : BaseJsonConverter { - - /** - * 获取目标类型的引用,用于标识转换的目标类型为 Double。 - * - * @return 返回 JsonTypeReference 类型的实例,表示目标类型为 Double。 - * @throws Exception 如果在获取类型引用时发生异常,则抛出该异常。 - */ - @Throws(Exception::class) - override fun getToType() = JsonTypeReference.of() -} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonFloatConverter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonFloatConverter.kt deleted file mode 100644 index 6eb0d10..0000000 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonFloatConverter.kt +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2026 mingliqiye - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - * ProjectName mingli-utils - * ModuleName mingli-utils.main - * CurrentFile BaseJsonFloatConverter.kt - * LastUpdate 2026-02-05 11:20:59 - * UpdateUser MingLiPro - */ - -package com.mingliqiye.utils.json.converters.base - -import com.mingliqiye.utils.json.api.type.JsonTypeReference - -/** - * 接口 [BaseJsonFloatConverter] 定义了一个用于处理浮点数类型 JSON 转换的基础转换器。 - * - * 该接口继承自 [BaseJsonConverter],并指定泛型参数 [E] 和 [Float], - * 表示将某种类型 [E] 转换为 [Float] 类型的 JSON 数据。 - */ -interface BaseJsonFloatConverter : BaseJsonConverter { - - /** - * 获取目标类型的引用信息。 - * - * 该方法重写了父接口中的 [getToType] 方法,返回 [Float] 类型的 [JsonTypeReference] 实例。 - * 用于标识当前转换器的目标类型为 [Float]。 - * - * @return [JsonTypeReference] 表示 [Float] 类型的引用信息。 - * @throws Exception 如果在获取类型引用时发生异常,则抛出此异常。 - */ - @Throws(Exception::class) - override fun getToType() = JsonTypeReference.of() -} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonIntConverter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonIntConverter.kt deleted file mode 100644 index a5eeabb..0000000 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonIntConverter.kt +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2026 mingliqiye - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - * ProjectName mingli-utils - * ModuleName mingli-utils.main - * CurrentFile BaseJsonIntConverter.kt - * LastUpdate 2026-02-05 11:20:59 - * UpdateUser MingLiPro - */ - -package com.mingliqiye.utils.json.converters.base - -import com.mingliqiye.utils.json.api.type.JsonTypeReference - -/** - * 接口 [BaseJsonIntConverter] 是一个泛型接口,继承自 [BaseJsonConverter], - * 用于定义将 JSON 数据转换为 [Int] 类型的转换器。 - * - * @param E 表示需要转换的目标类型,通常是一个具体的业务实体或数据模型。 - */ -interface BaseJsonIntConverter : BaseJsonConverter { - - /** - * 获取目标类型的引用信息。 - * - * 该方法重写了父接口中的 [getToType] 方法,返回 [Int] 类型的 [JsonTypeReference] 实例。 - * 用于标识当前转换器的目标类型为 [Int]。 - * - * @return 返回 [JsonTypeReference] 的实例,表示目标类型为 [Int]。 - * @throws Exception 如果在获取类型引用时发生异常,则抛出该异常。 - */ - @Throws(Exception::class) - override fun getToType() = JsonTypeReference.of() -} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonLongConverter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonLongConverter.kt deleted file mode 100644 index aac37cf..0000000 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonLongConverter.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2026 mingliqiye - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - * ProjectName mingli-utils - * ModuleName mingli-utils.main - * CurrentFile BaseJsonLongConverter.kt - * LastUpdate 2026-02-05 11:20:59 - * UpdateUser MingLiPro - */ - -package com.mingliqiye.utils.json.converters.base - -import com.mingliqiye.utils.json.api.type.JsonTypeReference - -/** - * 接口 [BaseJsonLongConverter] 定义了一个用于将 JSON 数据转换为 Long 类型的转换器。 - * - * 此接口继承自 [BaseJsonConverter],并指定泛型参数 [E] 和 [Long], - * 表示该转换器处理的是从类型 [E] 到 [Long] 的转换。 - */ -interface BaseJsonLongConverter : BaseJsonConverter { - - /** - * 获取目标类型的引用,用于标识此转换器的目标类型为 [Long]。 - * - * @return 返回 [JsonTypeReference] 的实例,表示目标类型为 [Long]。 - * @throws Exception 如果在获取类型引用时发生异常,则抛出此异常。 - */ - @Throws(Exception::class) - override fun getToType() = JsonTypeReference.of() -} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonShortConverter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonShortConverter.kt deleted file mode 100644 index 843e694..0000000 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonShortConverter.kt +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2026 mingliqiye - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - * ProjectName mingli-utils - * ModuleName mingli-utils.main - * CurrentFile BaseJsonShortConverter.kt - * LastUpdate 2026-02-05 11:20:59 - * UpdateUser MingLiPro - */ - -package com.mingliqiye.utils.json.converters.base - -import com.mingliqiye.utils.json.api.type.JsonTypeReference - -/** - * 接口 [BaseJsonShortConverter] 定义了一个用于将 JSON 数据转换为 Short 类型的转换器。 - * - * 该接口继承自 [BaseJsonConverter],并指定泛型参数 [E] 和 [Short], - * 表示该转换器处理的源类型为 [E],目标类型为 [Short]。 - */ -interface BaseJsonShortConverter : BaseJsonConverter { - - /** - * 获取目标类型的引用信息。 - * - * 该方法重写了父接口中的 [getToType] 方法,返回 [Short] 类型的 [JsonTypeReference] 实例。 - * 通过 [JsonTypeReference.Companion.of] 方法创建类型引用。 - * - * @return [JsonTypeReference] 表示 [Short] 类型的引用。 - * @throws Exception 如果在获取类型引用过程中发生异常,则抛出该异常。 - */ - @Throws(Exception::class) - override fun getToType() = JsonTypeReference.of() -} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonStringConverter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonStringConverter.kt deleted file mode 100644 index a22ac6a..0000000 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/BaseJsonStringConverter.kt +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2026 mingliqiye - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - * ProjectName mingli-utils - * ModuleName mingli-utils.main - * CurrentFile BaseJsonStringConverter.kt - * LastUpdate 2026-02-05 11:20:59 - * UpdateUser MingLiPro - */ - -package com.mingliqiye.utils.json.converters.base - -import com.mingliqiye.utils.json.api.type.JsonTypeReference - -/** - * 接口 [BaseJsonStringConverter] 定义了一个用于 JSON 字符串转换的基础接口。 - * - * 该接口继承自 [BaseJsonConverter],并指定泛型参数 [E] 和 [String], - * 表示将类型 [E] 转换为字符串类型的 JSON 数据。 - */ -interface BaseJsonStringConverter : BaseJsonConverter { - - /** - * 获取目标类型的引用信息。 - * - * 该方法重写了父接口中的 [getToType] 方法,返回一个表示 [String] 类型的 [JsonTypeReference] 实例。 - * 此方法可能抛出异常,因此使用 [@Throws(Exception::class)] 注解标记。 - * - * @return 返回一个 [JsonTypeReference] 实例,表示目标类型为 [String]。 - * @throws Exception 如果在获取类型引用时发生错误,则抛出此异常。 - */ - @Throws(Exception::class) - override fun getToType() = JsonTypeReference.of() -} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJson3Deserializer.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJson3Deserializer.kt index d5f065d..6b5011f 100644 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJson3Deserializer.kt +++ b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJson3Deserializer.kt @@ -16,32 +16,34 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile JackJson3Deserializer.kt - * LastUpdate 2026-02-26 13:02:52 + * LastUpdate 2026-11-17 19:51:20 * UpdateUser MingLiPro */ package com.mingliqiye.utils.json.converters.base +import com.mingliqiye.utils.json.reader.JackSon3JsonReader import tools.jackson.core.JsonParser -import tools.jackson.core.JsonToken import tools.jackson.databind.BeanProperty import tools.jackson.databind.DeserializationContext import tools.jackson.databind.ValueDeserializer -class JackJson3Deserializer( +class JackJson3Deserializer( val property: BeanProperty? = null, - val jsonConverter: JsonConverter, -) : ValueDeserializer() { + val jsonConverter: BaseJsonConverter, +) : ValueDeserializer() { + + val annotationGetter = + object : AnnotationGetter { + override fun getAnnotation(clazz: Class): T? = property?.getAnnotation(clazz) + } override fun deserialize( p: JsonParser, ctxt: DeserializationContext?, - ): F? { - if (p.currentToken() == JsonToken.VALUE_NULL) return null - return jsonConverter.deConvert(p.readValueAs(jsonConverter.getToClass()), object : AnnotationGetter { - override fun get(clazz: Class): T? = property?.getAnnotation(clazz) - }) + ): T? { + return jsonConverter.deConvert(JackSon3JsonReader(p.readValueAsTree()), annotationGetter) } /** @@ -54,7 +56,7 @@ class JackJson3Deserializer( override fun createContextual( ctxt: DeserializationContext?, property: BeanProperty?, - ): JackJson3Deserializer { + ): JackJson3Deserializer { return JackJson3Deserializer(property, jsonConverter) } diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJson3Serializer.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJson3Serializer.kt index 242bf68..522f95f 100644 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJson3Serializer.kt +++ b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJson3Serializer.kt @@ -16,48 +16,31 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile JackJson3Serializer.kt - * LastUpdate 2026-02-26 13:04:18 + * LastUpdate 2026-11-17 19:51:33 * UpdateUser MingLiPro */ package com.mingliqiye.utils.json.converters.base +import com.mingliqiye.utils.json.witer.JackSon3JsonWriter +import tools.jackson.core.JsonGenerator import tools.jackson.databind.BeanProperty import tools.jackson.databind.SerializationContext import tools.jackson.databind.ValueSerializer -import java.math.BigDecimal -import java.math.BigInteger -class JackJson3Serializer( + +class JackJson3Serializer( val property: BeanProperty? = null, - val jsonConverter: JsonConverter, -) : ValueSerializer() { + val jsonConverter: BaseJsonConverter, +) : ValueSerializer() { - /** - * 序列化方法:将源对象转换为目标对象,并根据目标对象的类型写入 JSON。 - * - * @param value 源对象,可能为 null。 - * @param gen JSON 生成器,用于写入 JSON 数据。 - * @param provider 序列化提供者,可选参数。 - */ - override fun serialize(value: F, gen: tools.jackson.core.JsonGenerator, ctxt: SerializationContext) { - val data: T? = jsonConverter.convert(value, object : AnnotationGetter { - override fun get(clazz: Class): T? = property?.getAnnotation(clazz) - }) - when (data) { - null -> gen.writeNull() - is String -> gen.writeString(data) - is Long -> gen.writeNumber(data) - is Short -> gen.writeNumber(data) - is Byte -> gen.writeNumber(data.toShort()) - is Int -> gen.writeNumber(data) - is BigDecimal -> gen.writeNumber(data) - is BigInteger -> gen.writeNumber(data) - is Float -> gen.writeNumber(data) - is Double -> gen.writeNumber(data) - is Boolean -> gen.writeBoolean(data) - else -> throw IllegalArgumentException("not sport data type ${data.javaClass} $data") + val annotationGetter = + object : AnnotationGetter { + override fun getAnnotation(clazz: Class): T? = property?.getAnnotation(clazz) } + + override fun serialize(value: T, gen: JsonGenerator, ctxt: SerializationContext) { + jsonConverter.convert(value, JackSon3JsonWriter(gen, ctxt), annotationGetter) } override fun createContextual(ctxt: SerializationContext?, property: BeanProperty?): ValueSerializer<*> { diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJsonDeserializer.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJsonDeserializer.kt index f03db7d..5e69ada 100644 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJsonDeserializer.kt +++ b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJsonDeserializer.kt @@ -16,44 +16,41 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile JackJsonDeserializer.kt - * LastUpdate 2026-02-08 02:22:46 + * LastUpdate 2026-11-17 19:53:29 * UpdateUser MingLiPro */ package com.mingliqiye.utils.json.converters.base import com.fasterxml.jackson.core.JsonParser -import com.fasterxml.jackson.core.JsonToken import com.fasterxml.jackson.databind.BeanProperty import com.fasterxml.jackson.databind.DeserializationContext import com.fasterxml.jackson.databind.JsonDeserializer import com.fasterxml.jackson.databind.deser.ContextualDeserializer +import com.mingliqiye.utils.json.reader.JackSonJsonReader -class JackJsonDeserializer( +class JackJsonDeserializer( val property: BeanProperty? = null, - val jsonConverter: JsonConverter, -) : JsonDeserializer(), ContextualDeserializer { + val jsonConverter: BaseJsonConverter, +) : JsonDeserializer(), ContextualDeserializer { + + + val annotationGetter = + object : AnnotationGetter { + override fun getAnnotation(clazz: Class): T? = property?.getAnnotation(clazz) + } + override fun deserialize( p: JsonParser, ctxt: DeserializationContext?, - ): F? { - if (p.currentToken == JsonToken.VALUE_NULL) return null - return jsonConverter.deConvert(p.readValueAs(jsonConverter.getToClass()), object : AnnotationGetter { - override fun get(clazz: Class): T? = property?.getAnnotation(clazz) - }) + ): T? { + return jsonConverter.deConvert(JackSonJsonReader(p.readValueAsTree()), annotationGetter) } - /** - * 上下文感知方法:设置当前属性信息。 - * - * @param ctxt 反序列化上下文,可选参数。 - * @param property 当前属性信息。 - * @return 返回当前反序列化器实例。 - */ override fun createContextual( ctxt: DeserializationContext?, property: BeanProperty?, - ): JackJsonDeserializer { + ): JackJsonDeserializer { return JackJsonDeserializer(property, jsonConverter) } } diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJsonSerializer.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJsonSerializer.kt index 9df85dc..6e6a23a 100644 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJsonSerializer.kt +++ b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JackJsonSerializer.kt @@ -16,7 +16,7 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile JackJsonSerializer.kt - * LastUpdate 2026-02-26 12:55:57 + * LastUpdate 2026-11-17 19:54:12 * UpdateUser MingLiPro */ @@ -27,13 +27,16 @@ import com.fasterxml.jackson.databind.BeanProperty import com.fasterxml.jackson.databind.JsonSerializer import com.fasterxml.jackson.databind.SerializerProvider import com.fasterxml.jackson.databind.ser.ContextualSerializer -import java.math.BigDecimal -import java.math.BigInteger +import com.mingliqiye.utils.json.witer.JackSonJsonWriter -class JackJsonSerializer( +class JackJsonSerializer( val property: BeanProperty?, - val jsonConverter: JsonConverter, -) : JsonSerializer(), ContextualSerializer { + val jsonConverter: BaseJsonConverter, +) : JsonSerializer(), ContextualSerializer { + + val annotationGetter = object : AnnotationGetter { + override fun getAnnotation(clazz: Class): T? = property?.getAnnotation(clazz) + } /** * 序列化方法:将源对象转换为目标对象,并根据目标对象的类型写入 JSON。 @@ -43,30 +46,11 @@ class JackJsonSerializer( * @param provider 序列化提供者,可选参数。 */ override fun serialize( - value: F?, + value: T?, gen: JsonGenerator, provider: SerializerProvider?, ) { - // 执行转换逻辑 - val data: T? = jsonConverter.convert(value, object : AnnotationGetter { - override fun get(clazz: Class): T? = property?.getAnnotation(clazz) - }) - - // 根据目标对象的类型写入对应的 JSON 值 - when (data) { - null -> gen.writeNull() - is String -> gen.writeString(data) - is Long -> gen.writeNumber(data) - is Short -> gen.writeNumber(data) - is Byte -> gen.writeNumber(data.toShort()) - is Int -> gen.writeNumber(data) - is BigDecimal -> gen.writeNumber(data) - is BigInteger -> gen.writeNumber(data) - is Float -> gen.writeNumber(data) - is Double -> gen.writeNumber(data) - is Boolean -> gen.writeBoolean(data) - else -> throw IllegalArgumentException("not sport data type ${data.javaClass} $data") - } + jsonConverter.convert(value, JackSonJsonWriter(gen, provider), annotationGetter) } /** @@ -79,7 +63,7 @@ class JackJsonSerializer( override fun createContextual( prov: SerializerProvider?, property: BeanProperty?, - ): JsonSerializer { - return JackJsonSerializer(property, jsonConverter) + ): JsonSerializer { + return JackJsonSerializer(property, this.jsonConverter) } } diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JsonConverter.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JsonConverter.kt deleted file mode 100644 index ad13ea9..0000000 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JsonConverter.kt +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2026 mingliqiye - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - * ProjectName mingli-utils - * ModuleName mingli-utils.main - * CurrentFile JsonConverter.kt - * LastUpdate 2026-02-07 22:30:18 - * UpdateUser MingLiPro - */ - -package com.mingliqiye.utils.json.converters.base - -import com.mingliqiye.utils.json.api.type.JsonTypeReference - -/** - * BaseJsonConverter 是一个通用的 JSON 转换器接口,用于定义对象之间的双向转换逻辑。 - * - * @param F 源类型,表示需要被转换的对象类型。 - * @param T 目标类型,表示转换后的对象类型。 - */ -interface JsonConverter { - /** - * 将源对象转换为目标对象。 - * - * @param obj 源对象,可能为 null。 - * @param annotationGetter 注解获取器,用于获取字段上的注解信息。 - * @return 转换后的目标对象,可能为 null。 - * @throws Exception 转换过程中可能抛出的异常。 - */ - @Throws(Exception::class) - fun convert(obj: F?, annotationGetter: AnnotationGetter): T? - - /** - * 将目标对象反向转换为源对象。 - * - * @param obj 目标对象,可能为 null。 - * @param annotationGetter 注解获取器,用于获取字段上的注解信息。 - * @return 反向转换后的源对象,可能为 null。 - * @throws Exception 转换过程中可能抛出的异常。 - */ - @Throws(Exception::class) - fun deConvert(obj: T?, annotationGetter: AnnotationGetter): F? - - /** - * 获取源类型的类型引用。 - * - * @return 源类型的 JsonTypeReference 对象。 - * @throws Exception 获取过程中可能抛出的异常。 - */ - @Throws(Exception::class) - fun getFromType(): JsonTypeReference - - /** - * 获取目标类型的类型引用。 - * - * @return 目标类型的 JsonTypeReference 对象。 - * @throws Exception 获取过程中可能抛出的异常。 - */ - @Throws(Exception::class) - fun getToType(): JsonTypeReference - - /** - * 获取源类型的原始类。 - * - * @return 源类型的 Class 对象。 - * @throws Exception 获取过程中可能抛出的异常。 - */ - @Throws(Exception::class) - fun getFromClass() = getFromType().getRawType() - - /** - * 获取目标类型的原始类。 - * - * @return 目标类型的 Class 对象。 - * @throws Exception 获取过程中可能抛出的异常。 - */ - @Throws(Exception::class) - fun getToClass() = getToType().getRawType() -} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JsonConverterUtils.kt b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JsonConverterUtils.kt index 63485fe..6eb917b 100644 --- a/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JsonConverterUtils.kt +++ b/src/main/kotlin/com/mingliqiye/utils/json/converters/base/JsonConverterUtils.kt @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * ProjectName FastBaseMapper.kt + * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile JsonConverterUtils.kt - * LastUpdate 2026-10-26 23:22:18 + * LastUpdate 2026-11-17 20:11:04 * UpdateUser MingLiPro */ @file:JvmName("JsonConverterUtils") @@ -24,61 +24,53 @@ package com.mingliqiye.utils.json.converters.base -import com.mingliqiye.utils.logger.MingLiLoggerFactory -import java.lang.reflect.ParameterizedType -import java.lang.reflect.Type +import java.math.BigDecimal +import java.math.BigInteger +import com.fasterxml.jackson.core.JsonGenerator as JackSon2JsonGenerator +import com.fasterxml.jackson.databind.module.SimpleModule as JackSon2SimpleModule +import tools.jackson.core.JsonGenerator as JackSon3JsonGenerator +import tools.jackson.databind.module.SimpleModule as JackSon3SimpleModule -val log = MingLiLoggerFactory.getLogger("com.mingliqiye.utils.json.converters.base.JsonConverterUtils") -/** - * 获取给定类型的实际类对象。 - * - * @param type 类型对象,可以是Class、ParameterizedType或其他Type的实现。 - * @return 返回与给定类型对应的Class对象;如果无法解析,则返回null。 - */ -fun getClass(type: Type?): Class<*>? { - // 尝试将type直接转换为Class类型,如果失败则检查是否为ParameterizedType - val clazz: Class<*>? = type as? Class<*> ?: if (type is ParameterizedType) { - // 如果是ParameterizedType,则递归获取其原始类型 - getClass(type.rawType) - } else { - null +fun BaseJsonConverter.getJacksonModule(): JackSon2SimpleModule = + JackSon2SimpleModule("${getClazz().simpleName}Module") + .addSerializer(getClazz(), JackJsonSerializer(null, this)) + .addDeserializer(getClazz(), JackJsonDeserializer(null, this)) + +fun BaseJsonConverter.getJackson3Module(): JackSon3SimpleModule = + JackSon3SimpleModule("${getClazz().simpleName}Module") + .addSerializer(getClazz(), JackJson3Serializer(null, this)) + .addDeserializer(getClazz(), JackJson3Deserializer(null, this)) + + +inline fun AnnotationGetter.getAnnotation(): T? = + getAnnotation(T::class.java) + + +fun JackSon3JsonGenerator.writeNumber(value: Number): JackSon3JsonGenerator { + when (value) { + is Long -> this.writeNumber(value) + is Short -> this.writeNumber(value) + is Byte -> this.writeNumber(value.toShort()) + is Int -> this.writeNumber(value) + is BigDecimal -> this.writeNumber(value) + is BigInteger -> this.writeNumber(value) + is Float -> this.writeNumber(value) + is Double -> this.writeNumber(value) } - return clazz + return this } -inline fun > getJsonConverter(): T = getJsonConverter(T::class.java) - -@Suppress("UNCHECKED_CAST") -fun > getJsonConverter(clazz: Class): T { - try { - return clazz.getDeclaredMethod( - "getJsonConverter" - ).invoke(null) as T - } catch (e: Exception) { - log.error("实现于 JsonConverter<*, *> 的类 必须实现静态方法 JsonConverter<*, *> getJsonConverter()", e) - throw e +fun JackSon2JsonGenerator.writeNumber(value: Number): JackSon2JsonGenerator { + when (value) { + is Long -> this.writeNumber(value) + is Short -> this.writeNumber(value) + is Byte -> this.writeNumber(value.toShort()) + is Int -> this.writeNumber(value) + is BigDecimal -> this.writeNumber(value) + is BigInteger -> this.writeNumber(value) + is Float -> this.writeNumber(value) + is Double -> this.writeNumber(value) } + return this } - -fun > com.fasterxml.jackson.databind.ObjectMapper.addJsonConverter(clazz: Class): com.fasterxml.jackson.databind.ObjectMapper = - this.registerModule(getJsonConverter(clazz).getJacksonModule()) - -@JvmName("inline_addJsonConverter") -inline fun > com.fasterxml.jackson.databind.ObjectMapper.addJsonConverter(): com.fasterxml.jackson.databind.ObjectMapper = - this.registerModule(getJsonConverter(T::class.java).getJacksonModule()) - -/** - * 创建并返回一个 Jackson 模块,该模块包含自定义的序列化器和反序列化器。 - * - * @return 配置了自定义序列化器和反序列化器的 SimpleModule 对象。 - */ -fun JsonConverter.getJacksonModule(): com.fasterxml.jackson.databind.module.SimpleModule = - com.fasterxml.jackson.databind.module.SimpleModule("(${getFromClass().name})-(${getToClass().name})") - .addSerializer(getFromClass(), JackJsonSerializer(null, this)) - .addDeserializer(getFromClass(), JackJsonDeserializer(null, this)) - -fun JsonConverter.getJackson3Module(): tools.jackson.databind.module.SimpleModule = - tools.jackson.databind.module.SimpleModule("(${getFromClass().name})-(${getToClass().name})") - .addSerializer(getFromClass(), JackJson3Serializer(null, this)) - .addDeserializer(getFromClass(), JackJson3Deserializer(null, this)) diff --git a/src/main/kotlin/com/mingliqiye/utils/json/judgment/JackSon3ReaderJudgment.kt b/src/main/kotlin/com/mingliqiye/utils/json/judgment/JackSon3ReaderJudgment.kt new file mode 100644 index 0000000..2f16c29 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/judgment/JackSon3ReaderJudgment.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile JackSon3ReaderJudgment.kt + * LastUpdate 2026-11-17 20:29:39 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.judgment + +import com.mingliqiye.utils.json.judgment.base.BaseJsonJudgment +import tools.jackson.databind.JsonNode + +open class JackSon3ReaderJudgment( + private val treeNode: JsonNode, +) : BaseJsonJudgment { + override val isObject: Boolean + get() = treeNode.isObject + override val isArray: Boolean + get() = treeNode.isArray + override val isString: Boolean + get() = treeNode.isString + override val isNumber: Boolean + get() = treeNode.isNumber + override val isBoolean: Boolean + get() = treeNode.isBoolean + override val isNull: Boolean + get() = treeNode.isNull + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/judgment/JackSonReaderJudgment.kt b/src/main/kotlin/com/mingliqiye/utils/json/judgment/JackSonReaderJudgment.kt new file mode 100644 index 0000000..ef89d79 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/judgment/JackSonReaderJudgment.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile JackSonReaderJudgment.kt + * LastUpdate 2026-11-17 20:29:39 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.judgment + +import com.fasterxml.jackson.databind.JsonNode +import com.mingliqiye.utils.json.judgment.base.BaseJsonJudgment + +open class JackSonReaderJudgment( + private val treeNode: JsonNode, +) : BaseJsonJudgment { + override val isObject: Boolean + get() = treeNode.isObject + override val isArray: Boolean + get() = treeNode.isArray + override val isString: Boolean + get() = treeNode.isTextual + override val isNumber: Boolean + get() = treeNode.isNumber + override val isBoolean: Boolean + get() = treeNode.isBoolean + override val isNull: Boolean + get() = treeNode.isNull + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/judgment/base/BaseJsonJudgment.kt b/src/main/kotlin/com/mingliqiye/utils/json/judgment/base/BaseJsonJudgment.kt new file mode 100644 index 0000000..d32fe3f --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/judgment/base/BaseJsonJudgment.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile BaseJsonJudgment.kt + * LastUpdate 2026-11-17 20:26:28 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.judgment.base + +interface BaseJsonJudgment { + val isObject: Boolean + get() = false + val isArray: Boolean + get() = false + val isString: Boolean + get() = false + val isNumber: Boolean + get() = false + val isBoolean: Boolean + get() = false + + val isNull: Boolean + get() = false + val isNotNull: Boolean + get() = !isNull +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSon3JsonArrayReader.kt b/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSon3JsonArrayReader.kt new file mode 100644 index 0000000..8f48465 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSon3JsonArrayReader.kt @@ -0,0 +1,83 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile JackSon3JsonArrayReader.kt + * LastUpdate 2026-11-17 20:29:39 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.reader + +import com.mingliqiye.utils.json.judgment.JackSon3ReaderJudgment +import com.mingliqiye.utils.json.reader.base.BaseJsonArrayReader +import com.mingliqiye.utils.json.reader.base.BaseJsonObjectReader +import tools.jackson.databind.JsonNode +import java.math.BigDecimal +import java.math.BigInteger + +class JackSon3JsonArrayReader( + private val treeNode: JsonNode, +) : BaseJsonArrayReader, JackSon3ReaderJudgment(treeNode) { + override fun getIndexObject(key: Int): BaseJsonObjectReader { + return JackSon3JsonObjectReader(treeNode[key]) + } + + override fun getIndexArray(key: Int): BaseJsonArrayReader { + return JackSon3JsonArrayReader(treeNode[key]) + } + + override fun getIndexString(key: Int): String { + return treeNode[key].stringValue() + } + + override fun getIndexBoolean(key: Int): Boolean { + return treeNode[key].booleanValue() + } + + override fun getIndexByte(key: Int): Byte { + return getIndexShort(key).toByte() + } + + override fun getIndexShort(key: Int): Short { + return treeNode[key].shortValue() + } + + override fun getIndexInt(key: Int): Int { + return treeNode[key].intValue() + } + + override fun getIndexLong(key: Int): Long { + return treeNode[key].longValue() + } + + override fun getIndexFloat(key: Int): Float { + return treeNode[key].floatValue() + } + + override fun getIndexDouble(key: Int): Double { + return treeNode[key].doubleValue() + } + + override fun getIndexBigInteger(key: Int): BigInteger { + return treeNode[key].bigIntegerValue() + } + + override fun getIndexBigDecimal(key: Int): BigDecimal { + return treeNode[key].decimalValue() + } + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSon3JsonObjectReader.kt b/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSon3JsonObjectReader.kt new file mode 100644 index 0000000..88d25ea --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSon3JsonObjectReader.kt @@ -0,0 +1,90 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile JackSon3JsonObjectReader.kt + * LastUpdate 2026-11-17 20:29:39 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.reader + +import com.mingliqiye.utils.json.judgment.JackSon3ReaderJudgment +import com.mingliqiye.utils.json.reader.base.BaseJsonArrayReader +import com.mingliqiye.utils.json.reader.base.BaseJsonObjectReader +import tools.jackson.databind.JsonNode +import java.math.BigDecimal +import java.math.BigInteger + +class JackSon3JsonObjectReader( + private val treeNode: JsonNode, +) : BaseJsonObjectReader, JackSon3ReaderJudgment(treeNode) { + + override fun getFieldObject(key: String): BaseJsonObjectReader { + return JackSon3JsonObjectReader(treeNode[key]) + } + + override fun getFieldArray(key: String): BaseJsonArrayReader { + return JackSon3JsonArrayReader(treeNode[key]) + } + + override fun getFieldString(key: String): String { + return treeNode[key].asString() + } + + override fun getFieldBoolean(key: String): Boolean { + return treeNode[key].booleanValue() + } + + override fun getFieldByte(key: String): Byte { + return getFieldShort(key).toByte() + } + + override fun getFieldShort(key: String): Short { + return treeNode[key].shortValue() + } + + override fun getFieldInt(key: String): Int { + return treeNode[key].intValue() + } + + override fun getFieldLong(key: String): Long { + return treeNode[key].longValue() + } + + override fun getFieldFloat(key: String): Float { + return treeNode[key].floatValue() + + } + + override fun getFieldDouble(key: String): Double { + return treeNode[key].doubleValue() + + } + + override fun getFieldBigInteger(key: String): BigInteger { + return treeNode[key].bigIntegerValue() + + } + + override fun getFieldBigDecimal(key: String): BigDecimal { + return treeNode[key].decimalValue() + } + + override val isObject: Boolean + get() = treeNode.isObject + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSon3JsonReader.kt b/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSon3JsonReader.kt new file mode 100644 index 0000000..2ce2a2e --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSon3JsonReader.kt @@ -0,0 +1,84 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile JackSon3JsonReader.kt + * LastUpdate 2026-11-17 20:29:39 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.reader + +import com.mingliqiye.utils.json.judgment.JackSon3ReaderJudgment +import com.mingliqiye.utils.json.reader.base.BaseJsonArrayReader +import com.mingliqiye.utils.json.reader.base.BaseJsonObjectReader +import com.mingliqiye.utils.json.reader.base.BaseJsonReader +import tools.jackson.databind.JsonNode +import java.math.BigDecimal +import java.math.BigInteger + +class JackSon3JsonReader( + private val treeNode: JsonNode, +) : BaseJsonReader, JackSon3ReaderJudgment(treeNode) { + override fun getObject(): BaseJsonObjectReader { + return JackSon3JsonObjectReader(treeNode) + } + + override fun getArray(): BaseJsonArrayReader { + return JackSon3JsonArrayReader(treeNode) + } + + override fun getString(): String { + return treeNode.asString() + } + + override fun getBoolean(): Boolean { + return treeNode.booleanValue() + } + + override fun getByte(): Byte { + return getShort().toByte() + } + + override fun getShort(): Short { + return treeNode.shortValue() + } + + override fun getInt(): Int { + return treeNode.intValue() + } + + override fun getLong(): Long { + return treeNode.longValue() + } + + override fun getFloat(): Float { + return treeNode.floatValue() + } + + override fun getDouble(): Double { + return treeNode.doubleValue() + } + + override fun getBigInteger(): BigInteger { + return treeNode.bigIntegerValue() + } + + override fun getBigDecimal(): BigDecimal { + return treeNode.decimalValue() + } + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSonJsonArrayReader.kt b/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSonJsonArrayReader.kt new file mode 100644 index 0000000..a902cb8 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSonJsonArrayReader.kt @@ -0,0 +1,85 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile JackSonJsonArrayReader.kt + * LastUpdate 2026-11-17 20:29:39 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.reader + +import com.fasterxml.jackson.databind.JsonNode +import com.mingliqiye.utils.json.judgment.JackSonReaderJudgment +import com.mingliqiye.utils.json.reader.base.BaseJsonArrayReader +import com.mingliqiye.utils.json.reader.base.BaseJsonObjectReader +import java.math.BigDecimal +import java.math.BigInteger + +class JackSonJsonArrayReader( + private val treeNode: JsonNode, +) : BaseJsonArrayReader, JackSonReaderJudgment(treeNode) { + + + override fun getIndexObject(key: Int): BaseJsonObjectReader { + return JackSonJsonObjectReader(treeNode[key]) + } + + override fun getIndexArray(key: Int): BaseJsonArrayReader { + return JackSonJsonArrayReader(treeNode[key]) + } + + override fun getIndexString(key: Int): String { + return treeNode[key].textValue() + } + + override fun getIndexBoolean(key: Int): Boolean { + return treeNode[key].booleanValue() + } + + override fun getIndexByte(key: Int): Byte { + return getIndexShort(key).toByte() + } + + override fun getIndexShort(key: Int): Short { + return treeNode[key].shortValue() + } + + override fun getIndexInt(key: Int): Int { + return treeNode[key].intValue() + } + + override fun getIndexLong(key: Int): Long { + return treeNode[key].longValue() + } + + override fun getIndexFloat(key: Int): Float { + return treeNode[key].floatValue() + } + + override fun getIndexDouble(key: Int): Double { + return treeNode[key].doubleValue() + } + + override fun getIndexBigInteger(key: Int): BigInteger { + return treeNode[key].bigIntegerValue() + } + + override fun getIndexBigDecimal(key: Int): BigDecimal { + return treeNode[key].decimalValue() + } + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSonJsonObjectReader.kt b/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSonJsonObjectReader.kt new file mode 100644 index 0000000..a475d4e --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSonJsonObjectReader.kt @@ -0,0 +1,90 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile JackSonJsonObjectReader.kt + * LastUpdate 2026-11-17 20:29:39 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.reader + +import com.fasterxml.jackson.databind.JsonNode +import com.mingliqiye.utils.json.judgment.JackSonReaderJudgment +import com.mingliqiye.utils.json.reader.base.BaseJsonArrayReader +import com.mingliqiye.utils.json.reader.base.BaseJsonObjectReader +import java.math.BigDecimal +import java.math.BigInteger + +class JackSonJsonObjectReader( + private val treeNode: JsonNode, +) : BaseJsonObjectReader, JackSonReaderJudgment(treeNode) { + + override fun getFieldObject(key: String): BaseJsonObjectReader { + return JackSonJsonObjectReader(treeNode[key]) + } + + override fun getFieldArray(key: String): BaseJsonArrayReader { + return JackSonJsonArrayReader(treeNode[key]) + } + + override fun getFieldString(key: String): String { + return treeNode[key].textValue() + } + + override fun getFieldBoolean(key: String): Boolean { + return treeNode[key].booleanValue() + } + + override fun getFieldByte(key: String): Byte { + return getFieldShort(key).toByte() + } + + override fun getFieldShort(key: String): Short { + return treeNode[key].shortValue() + } + + override fun getFieldInt(key: String): Int { + return treeNode[key].intValue() + } + + override fun getFieldLong(key: String): Long { + return treeNode[key].longValue() + } + + override fun getFieldFloat(key: String): Float { + return treeNode[key].floatValue() + + } + + override fun getFieldDouble(key: String): Double { + return treeNode[key].doubleValue() + + } + + override fun getFieldBigInteger(key: String): BigInteger { + return treeNode[key].bigIntegerValue() + + } + + override fun getFieldBigDecimal(key: String): BigDecimal { + return treeNode[key].decimalValue() + } + + override val isObject: Boolean + get() = treeNode.isObject + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSonJsonReader.kt b/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSonJsonReader.kt new file mode 100644 index 0000000..aec9375 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/reader/JackSonJsonReader.kt @@ -0,0 +1,84 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile JackSonJsonReader.kt + * LastUpdate 2026-11-17 20:29:39 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.reader + +import com.fasterxml.jackson.databind.JsonNode +import com.mingliqiye.utils.json.judgment.JackSonReaderJudgment +import com.mingliqiye.utils.json.reader.base.BaseJsonArrayReader +import com.mingliqiye.utils.json.reader.base.BaseJsonObjectReader +import com.mingliqiye.utils.json.reader.base.BaseJsonReader +import java.math.BigDecimal +import java.math.BigInteger + +class JackSonJsonReader( + private val treeNode: JsonNode, +) : BaseJsonReader, JackSonReaderJudgment(treeNode) { + override fun getObject(): BaseJsonObjectReader { + return JackSonJsonObjectReader(treeNode) + } + + override fun getArray(): BaseJsonArrayReader { + return JackSonJsonArrayReader(treeNode) + } + + override fun getString(): String { + return treeNode.asText() + } + + override fun getBoolean(): Boolean { + return treeNode.booleanValue() + } + + override fun getByte(): Byte { + return getShort().toByte() + } + + override fun getShort(): Short { + return treeNode.shortValue() + } + + override fun getInt(): Int { + return treeNode.intValue() + } + + override fun getLong(): Long { + return treeNode.longValue() + } + + override fun getFloat(): Float { + return treeNode.floatValue() + } + + override fun getDouble(): Double { + return treeNode.doubleValue() + } + + override fun getBigInteger(): BigInteger { + return treeNode.bigIntegerValue() + } + + override fun getBigDecimal(): BigDecimal { + return treeNode.decimalValue() + } + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/reader/base/BaseJsonArrayReader.kt b/src/main/kotlin/com/mingliqiye/utils/json/reader/base/BaseJsonArrayReader.kt new file mode 100644 index 0000000..278f423 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/reader/base/BaseJsonArrayReader.kt @@ -0,0 +1,78 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile BaseJsonArrayReader.kt + * LastUpdate 2026-11-17 19:32:34 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.reader.base + +import com.mingliqiye.utils.exception.JsonParserException +import java.math.BigDecimal +import java.math.BigInteger + +interface BaseJsonArrayReader : BaseJsonReader { + override fun getObject(): BaseJsonObjectReader = + throw JsonParserException("not supported getObject on BaseJsonArrayReader") + + override fun getArray(): BaseJsonArrayReader = + throw JsonParserException("not supported getArray on BaseJsonArrayReader") + + override fun getString(): String = + throw JsonParserException("not supported getString on BaseJsonArrayReader") + + override fun getBoolean(): Boolean = + throw JsonParserException("not supported getBoolean on BaseJsonArrayReader") + + override fun getByte(): Byte = + throw JsonParserException("not supported getByte on BaseJsonArrayReader") + + override fun getShort(): Short = + throw JsonParserException("not supported getShort on BaseJsonArrayReader") + + override fun getInt(): Int = + throw JsonParserException("not supported getInt on BaseJsonArrayReader") + + override fun getLong(): Long = + throw JsonParserException("not supported getLong on BaseJsonArrayReader") + + override fun getFloat(): Float = + throw JsonParserException("not supported getFloat on BaseJsonArrayReader") + + override fun getDouble(): Double = + throw JsonParserException("not supported getDouble on BaseJsonArrayReader") + + override fun getBigInteger(): BigInteger = + throw JsonParserException("not supported getBigInteger on BaseJsonArrayReader") + + override fun getBigDecimal(): BigDecimal = + throw JsonParserException("not supported getBigDecimal on BaseJsonArrayReader") + + fun getIndexObject(key: Int): BaseJsonObjectReader + fun getIndexArray(key: Int): BaseJsonArrayReader + fun getIndexString(key: Int): String + fun getIndexBoolean(key: Int): Boolean + fun getIndexByte(key: Int): Byte + fun getIndexShort(key: Int): Short + fun getIndexInt(key: Int): Int + fun getIndexLong(key: Int): Long + fun getIndexFloat(key: Int): Float + fun getIndexDouble(key: Int): Double + fun getIndexBigInteger(key: Int): BigInteger + fun getIndexBigDecimal(key: Int): BigDecimal +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/reader/base/BaseJsonObjectReader.kt b/src/main/kotlin/com/mingliqiye/utils/json/reader/base/BaseJsonObjectReader.kt new file mode 100644 index 0000000..1fa7e80 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/reader/base/BaseJsonObjectReader.kt @@ -0,0 +1,82 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile BaseJsonObjectReader.kt + * LastUpdate 2026-11-17 19:32:34 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.reader.base + +import com.mingliqiye.utils.exception.JsonParserException +import java.math.BigDecimal +import java.math.BigInteger + +interface BaseJsonObjectReader : BaseJsonReader { + override val isObject: Boolean + get() = true + + + override fun getObject(): BaseJsonObjectReader = + throw JsonParserException("not supported getObject on BaseJsonObjectReader") + + override fun getArray(): BaseJsonArrayReader = + throw JsonParserException("not supported getArray on BaseJsonObjectReader") + + override fun getString(): String = + throw JsonParserException("not supported getString on BaseJsonObjectReader") + + override fun getBoolean(): Boolean = + throw JsonParserException("not supported getBoolean on BaseJsonObjectReader") + + override fun getByte(): Byte = + throw JsonParserException("not supported getByte on BaseJsonObjectReader") + + override fun getShort(): Short = + throw JsonParserException("not supported getShort on BaseJsonObjectReader") + + override fun getInt(): Int = + throw JsonParserException("not supported getInt on BaseJsonObjectReader") + + override fun getLong(): Long = + throw JsonParserException("not supported getLong on BaseJsonObjectReader") + + override fun getFloat(): Float = + throw JsonParserException("not supported getFloat on BaseJsonObjectReader") + + override fun getDouble(): Double = + throw JsonParserException("not supported getDouble on BaseJsonObjectReader") + + override fun getBigInteger(): BigInteger = + throw JsonParserException("not supported getBigInteger on BaseJsonObjectReader") + + override fun getBigDecimal(): BigDecimal = + throw JsonParserException("not supported getBigDecimal on BaseJsonObjectReader") + + fun getFieldObject(key: String): BaseJsonObjectReader + fun getFieldArray(key: String): BaseJsonArrayReader + fun getFieldString(key: String): String + fun getFieldBoolean(key: String): Boolean + fun getFieldByte(key: String): Byte + fun getFieldShort(key: String): Short + fun getFieldInt(key: String): Int + fun getFieldLong(key: String): Long + fun getFieldFloat(key: String): Float + fun getFieldDouble(key: String): Double + fun getFieldBigInteger(key: String): BigInteger + fun getFieldBigDecimal(key: String): BigDecimal +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/reader/base/BaseJsonReader.kt b/src/main/kotlin/com/mingliqiye/utils/json/reader/base/BaseJsonReader.kt new file mode 100644 index 0000000..36cfda5 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/reader/base/BaseJsonReader.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile BaseJsonReader.kt + * LastUpdate 2026-11-17 20:26:28 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.reader.base + +import com.mingliqiye.utils.json.judgment.base.BaseJsonJudgment +import java.math.BigDecimal +import java.math.BigInteger + +interface BaseJsonReader : BaseJsonJudgment { + fun getObject(): BaseJsonObjectReader + fun getArray(): BaseJsonArrayReader + fun getString(): String + fun getBoolean(): Boolean + fun getByte(): Byte + fun getShort(): Short + fun getInt(): Int + fun getLong(): Long + fun getFloat(): Float + fun getDouble(): Double + fun getBigInteger(): BigInteger + fun getBigDecimal(): BigDecimal + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSon3ArrayWriter.kt b/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSon3ArrayWriter.kt new file mode 100644 index 0000000..00ae752 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSon3ArrayWriter.kt @@ -0,0 +1,69 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile JackSon3ArrayWriter.kt + * LastUpdate 2026-11-17 19:34:28 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.witer + +import com.mingliqiye.utils.json.converters.base.writeNumber +import com.mingliqiye.utils.json.witer.base.BaseJsonArrayWriter +import com.mingliqiye.utils.json.witer.base.BaseJsonObjectWriter +import tools.jackson.core.JsonGenerator +import tools.jackson.databind.SerializationContext + +class JackSon3ArrayWriter( + private val jsonGenerator: JsonGenerator, + private val serializationContext: SerializationContext, +) : BaseJsonArrayWriter { + override fun addNull(): JackSon3ArrayWriter { + jsonGenerator.writeNull() + return this + } + + override fun addString(value: String): JackSon3ArrayWriter { + jsonGenerator.writeString(value) + return this + } + + override fun addNumber(value: Number): JackSon3ArrayWriter { + jsonGenerator.writeNumber(value) + return this + } + + override fun addBoolean(value: Boolean): JackSon3ArrayWriter { + jsonGenerator.writeBoolean(value) + return this + } + + override fun addObject(value: (BaseJsonObjectWriter) -> Unit): JackSon3ArrayWriter { + jsonGenerator.writeStartObject() + value(JackSon3ObjectWriter(jsonGenerator, serializationContext)) + jsonGenerator.writeEndObject() + return this + } + + override fun addArray(value: (BaseJsonArrayWriter) -> Unit): JackSon3ArrayWriter { + jsonGenerator.writeStartArray() + value(JackSon3ArrayWriter(jsonGenerator, serializationContext)) + jsonGenerator.writeEndArray() + return this + } + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSon3JsonWriter.kt b/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSon3JsonWriter.kt new file mode 100644 index 0000000..4b14450 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSon3JsonWriter.kt @@ -0,0 +1,70 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile JackSon3JsonWriter.kt + * LastUpdate 2026-11-17 19:32:34 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.witer + +import com.mingliqiye.utils.json.converters.base.writeNumber +import com.mingliqiye.utils.json.witer.base.BaseJsonArrayWriter +import com.mingliqiye.utils.json.witer.base.BaseJsonObjectWriter +import com.mingliqiye.utils.json.witer.base.BaseJsonWriter +import tools.jackson.core.JsonGenerator +import tools.jackson.databind.SerializationContext + +class JackSon3JsonWriter( + private val jsonGenerator: JsonGenerator, + private val serializationContext: SerializationContext, +) : BaseJsonWriter { + + override fun setObject(objCall: (BaseJsonObjectWriter) -> BaseJsonObjectWriter) { + jsonGenerator.writeStartObject() + objCall( + JackSon3ObjectWriter( + jsonGenerator, + serializationContext, + ) + ) + jsonGenerator.writeEndObject() + } + + override fun setArray(arrayCall: (BaseJsonArrayWriter) -> BaseJsonArrayWriter) { + jsonGenerator.writeStartArray() + arrayCall(JackSon3ArrayWriter(jsonGenerator, serializationContext)) + jsonGenerator.writeEndArray() + } + + override fun setString(string: String) { + jsonGenerator.writeString(string) + } + + override fun setNumber(number: Number) { + jsonGenerator.writeNumber(number) + } + + override fun setBoolean(boolean: Boolean) { + jsonGenerator.writeBoolean(boolean) + } + + override fun setNull() { + jsonGenerator.writeNull() + } + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSon3ObjectWriter.kt b/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSon3ObjectWriter.kt new file mode 100644 index 0000000..b3b7039 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSon3ObjectWriter.kt @@ -0,0 +1,89 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile JackSon3ObjectWriter.kt + * LastUpdate 2026-11-17 19:32:34 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.witer + +import com.mingliqiye.utils.json.converters.base.writeNumber +import com.mingliqiye.utils.json.witer.base.BaseJsonArrayWriter +import com.mingliqiye.utils.json.witer.base.BaseJsonObjectWriter +import tools.jackson.core.JsonGenerator +import tools.jackson.databind.SerializationContext + +class JackSon3ObjectWriter( + private val jsonGenerator: JsonGenerator, + private val serializationContext: SerializationContext, +) : BaseJsonObjectWriter { + + fun writeName(name: String): JsonGenerator = jsonGenerator.writeName(name) + + override fun addNull(name: String): BaseJsonObjectWriter { + writeName(name) + return this + } + + override fun addString( + name: String, + value: String, + ): BaseJsonObjectWriter { + writeName(name).writeString(value) + return this + } + + override fun addNumber( + name: String, + value: Number, + ): BaseJsonObjectWriter { + writeName(name).writeNumber(value) + return this + } + + override fun addBoolean( + name: String, + value: Boolean, + ): BaseJsonObjectWriter { + writeName(name).writeBoolean(value) + return this + } + + override fun addObject( + name: String, + valueCaller: (BaseJsonObjectWriter) -> Unit, + ): BaseJsonObjectWriter { + writeName(name) + jsonGenerator.writeStartObject() + valueCaller(JackSon3ObjectWriter(jsonGenerator, serializationContext)) + jsonGenerator.writeEndObject() + return this + } + + override fun addArray( + name: String, + valueCaller: (BaseJsonArrayWriter) -> Unit, + ): BaseJsonObjectWriter { + writeName(name) + jsonGenerator.writeStartArray() + valueCaller(JackSon3ArrayWriter(jsonGenerator, serializationContext)) + jsonGenerator.writeEndArray() + return this + } + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSonArrayWriter.kt b/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSonArrayWriter.kt new file mode 100644 index 0000000..b059bae --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSonArrayWriter.kt @@ -0,0 +1,69 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile JackSonArrayWriter.kt + * LastUpdate 2026-11-17 20:01:56 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.witer + +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.databind.SerializerProvider +import com.mingliqiye.utils.json.converters.base.writeNumber +import com.mingliqiye.utils.json.witer.base.BaseJsonArrayWriter +import com.mingliqiye.utils.json.witer.base.BaseJsonObjectWriter + +class JackSonArrayWriter( + private val jsonGenerator: JsonGenerator, + private val serializationContext: SerializerProvider?, +) : BaseJsonArrayWriter { + override fun addNull(): JackSonArrayWriter { + jsonGenerator.writeNull() + return this + } + + override fun addString(value: String): JackSonArrayWriter { + jsonGenerator.writeString(value) + return this + } + + override fun addNumber(value: Number): JackSonArrayWriter { + jsonGenerator.writeNumber(value) + return this + } + + override fun addBoolean(value: Boolean): JackSonArrayWriter { + jsonGenerator.writeBoolean(value) + return this + } + + override fun addObject(value: (BaseJsonObjectWriter) -> Unit): JackSonArrayWriter { + jsonGenerator.writeStartObject() + value(JackSonObjectWriter(jsonGenerator, serializationContext)) + jsonGenerator.writeEndObject() + return this + } + + override fun addArray(value: (BaseJsonArrayWriter) -> Unit): JackSonArrayWriter { + jsonGenerator.writeStartArray() + value(JackSonArrayWriter(jsonGenerator, serializationContext)) + jsonGenerator.writeEndArray() + return this + } + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSonJsonWriter.kt b/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSonJsonWriter.kt new file mode 100644 index 0000000..ec52a6f --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSonJsonWriter.kt @@ -0,0 +1,70 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile JackSonJsonWriter.kt + * LastUpdate 2026-11-17 20:01:48 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.witer + +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.databind.SerializerProvider +import com.mingliqiye.utils.json.converters.base.writeNumber +import com.mingliqiye.utils.json.witer.base.BaseJsonArrayWriter +import com.mingliqiye.utils.json.witer.base.BaseJsonObjectWriter +import com.mingliqiye.utils.json.witer.base.BaseJsonWriter + +class JackSonJsonWriter( + private val jsonGenerator: JsonGenerator, + private val serializationContext: SerializerProvider?, +) : BaseJsonWriter { + + override fun setObject(objCall: (BaseJsonObjectWriter) -> BaseJsonObjectWriter) { + jsonGenerator.writeStartObject() + objCall( + JackSonObjectWriter( + jsonGenerator, + serializationContext, + ) + ) + jsonGenerator.writeEndObject() + } + + override fun setArray(arrayCall: (BaseJsonArrayWriter) -> BaseJsonArrayWriter) { + jsonGenerator.writeStartArray() + arrayCall(JackSonArrayWriter(jsonGenerator, serializationContext)) + jsonGenerator.writeEndArray() + } + + override fun setString(string: String) { + jsonGenerator.writeString(string) + } + + override fun setNumber(number: Number) { + jsonGenerator.writeNumber(number) + } + + override fun setBoolean(boolean: Boolean) { + jsonGenerator.writeBoolean(boolean) + } + + override fun setNull() { + jsonGenerator.writeNull() + } + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSonObjectWriter.kt b/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSonObjectWriter.kt new file mode 100644 index 0000000..8f046a2 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/witer/JackSonObjectWriter.kt @@ -0,0 +1,89 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile JackSonObjectWriter.kt + * LastUpdate 2026-11-17 20:06:55 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.witer + +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.databind.SerializerProvider +import com.mingliqiye.utils.json.converters.base.writeNumber +import com.mingliqiye.utils.json.witer.base.BaseJsonArrayWriter +import com.mingliqiye.utils.json.witer.base.BaseJsonObjectWriter + +class JackSonObjectWriter( + private val jsonGenerator: JsonGenerator, + private val serializationContext: SerializerProvider?, +) : BaseJsonObjectWriter { + + + override fun addNull(name: String): BaseJsonObjectWriter { + jsonGenerator.writeNullField(name) + return this + } + + override fun addString( + name: String, + value: String, + ): BaseJsonObjectWriter { + jsonGenerator.writeStringField(name, value) + return this + } + + override fun addNumber( + name: String, + value: Number, + ): BaseJsonObjectWriter { + jsonGenerator.writeFieldName(name) + jsonGenerator.writeNumber(value) + return this + } + + override fun addBoolean( + name: String, + value: Boolean, + ): BaseJsonObjectWriter { + jsonGenerator.writeBooleanField(name, value) + return this + } + + override fun addObject( + name: String, + valueCaller: (BaseJsonObjectWriter) -> Unit, + ): BaseJsonObjectWriter { + jsonGenerator.writeFieldName(name) + jsonGenerator.writeStartObject() + valueCaller(JackSonObjectWriter(jsonGenerator, serializationContext)) + jsonGenerator.writeEndObject() + return this + } + + override fun addArray( + name: String, + valueCaller: (BaseJsonArrayWriter) -> Unit, + ): BaseJsonObjectWriter { + jsonGenerator.writeFieldName(name) + jsonGenerator.writeStartArray() + valueCaller(JackSonArrayWriter(jsonGenerator, serializationContext)) + jsonGenerator.writeEndArray() + return this + } + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/witer/base/BaseJsonArrayWriter.kt b/src/main/kotlin/com/mingliqiye/utils/json/witer/base/BaseJsonArrayWriter.kt new file mode 100644 index 0000000..e0ed975 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/witer/base/BaseJsonArrayWriter.kt @@ -0,0 +1,58 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile BaseJsonArrayWriter.kt + * LastUpdate 2026-11-17 19:43:44 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.witer.base + +import com.mingliqiye.utils.exception.JsonFormatException + +interface BaseJsonArrayWriter : BaseJsonWriter { + + + override fun setObject(objCall: (BaseJsonObjectWriter) -> BaseJsonObjectWriter) = + throw JsonFormatException("setObject is not supported on BaseJsonArrayWriter") + + override fun setArray(arrayCall: (BaseJsonArrayWriter) -> BaseJsonArrayWriter) = + throw JsonFormatException("setArray is not supported on BaseJsonArrayWriter") + + override fun setString(string: String) = + throw JsonFormatException("setString is not supported on BaseJsonArrayWriter") + + override fun setNumber(number: Number) = + throw JsonFormatException("setNumber is not supported on BaseJsonArrayWriter") + + override fun setBoolean(boolean: Boolean) = + throw JsonFormatException("setBoolean is not supported on BaseJsonArrayWriter") + + override fun setNull() = throw JsonFormatException("setNull is not supported on BaseJsonArrayWriter") + override val isArray + get() = true + + fun addNull(): BaseJsonArrayWriter + fun addString(value: String): BaseJsonArrayWriter + fun addNumber(value: Number): BaseJsonArrayWriter + fun addBoolean(value: Boolean): BaseJsonArrayWriter + fun addObject(value: (BaseJsonObjectWriter) -> Unit): BaseJsonArrayWriter + fun addArray(value: (BaseJsonArrayWriter) -> Unit): BaseJsonArrayWriter + fun add(value: String): BaseJsonArrayWriter = addString(value) + fun add(value: Number): BaseJsonArrayWriter = addNumber(value) + fun add(value: Boolean): BaseJsonArrayWriter = addBoolean(value) +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/witer/base/BaseJsonObjectWriter.kt b/src/main/kotlin/com/mingliqiye/utils/json/witer/base/BaseJsonObjectWriter.kt new file mode 100644 index 0000000..16bca2a --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/witer/base/BaseJsonObjectWriter.kt @@ -0,0 +1,59 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile BaseJsonObjectWriter.kt + * LastUpdate 2026-11-17 18:39:06 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.witer.base + +import com.mingliqiye.utils.exception.JsonFormatException + +interface BaseJsonObjectWriter : BaseJsonWriter { + override val isObject: Boolean + get() = true + + override fun setObject(objCall: (BaseJsonObjectWriter) -> BaseJsonObjectWriter) = + throw JsonFormatException("setObject is not supported on BaseJsonObjectWriter") + + override fun setArray(arrayCall: (BaseJsonArrayWriter) -> BaseJsonArrayWriter) = + throw JsonFormatException("setArray is not supported on BaseJsonObjectWriter") + + override fun setString(string: String) = + throw JsonFormatException("setString is not supported on BaseJsonObjectWriter") + + override fun setNumber(number: Number) = + throw JsonFormatException("setNumber is not supported on BaseJsonObjectWriter") + + override fun setBoolean(boolean: Boolean) = + throw JsonFormatException("setBoolean is not supported on BaseJsonObjectWriter") + + override fun setNull() = throw JsonFormatException("setNull is not supported on BaseJsonObjectWriter") + fun addNull(name: String): BaseJsonObjectWriter + + fun addString(name: String, value: String): BaseJsonObjectWriter + fun addNumber(name: String, value: Number): BaseJsonObjectWriter + fun addBoolean(name: String, value: Boolean): BaseJsonObjectWriter + fun addObject(name: String, valueCaller: (BaseJsonObjectWriter) -> Unit): BaseJsonObjectWriter + + fun addArray(name: String, valueCaller: (BaseJsonArrayWriter) -> Unit): BaseJsonObjectWriter + fun add(name: String, value: String): BaseJsonObjectWriter = addString(name, value) + fun add(name: String, value: Number): BaseJsonObjectWriter = addNumber(name, value) + fun add(name: String, value: Boolean): BaseJsonObjectWriter = addBoolean(name, value) + +} diff --git a/src/main/kotlin/com/mingliqiye/utils/json/witer/base/BaseJsonWriter.kt b/src/main/kotlin/com/mingliqiye/utils/json/witer/base/BaseJsonWriter.kt new file mode 100644 index 0000000..cd58e7c --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/json/witer/base/BaseJsonWriter.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile BaseJsonWriter.kt + * LastUpdate 2026-11-17 19:32:34 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.json.witer.base + +import com.mingliqiye.utils.json.judgment.BaseJsonJudgment + + +interface BaseJsonWriter : BaseJsonJudgment { + fun setObject(objCall: (BaseJsonObjectWriter) -> BaseJsonObjectWriter) + fun setArray(arrayCall: (BaseJsonArrayWriter) -> BaseJsonArrayWriter) + fun setString(string: String) + fun setNumber(number: Number) + fun setBoolean(boolean: Boolean) + fun setNull() +} diff --git a/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/JsonAutoConfiguration.kt b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/JsonAutoConfiguration.kt index 4c57477..b378b22 100644 --- a/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/JsonAutoConfiguration.kt +++ b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/JsonAutoConfiguration.kt @@ -16,7 +16,7 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile JsonAutoConfiguration.kt - * LastUpdate 2026-11-09 14:44:55 + * LastUpdate 2026-11-17 20:10:26 * UpdateUser MingLiPro */ @@ -36,9 +36,9 @@ open class JsonAutoConfiguration { fun jacksonBuilder(): JacksonBuilder = JacksonBuilder { it.apply { addJsonConverters( - UUIDJsonConverter.getJsonConverter(), - DateTimeJsonConverter.getJsonConverter(), - NetworkEndpointJsonConverter.getJsonConverter(), + UUIDJsonConverter(), + DateTimeJsonConverter(), + NetworkEndpointJsonConverter(), ) } } diff --git a/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/builder/JacksonJsonBuilder.kt b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/builder/JacksonJsonBuilder.kt index ba6a102..04a1f81 100644 --- a/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/builder/JacksonJsonBuilder.kt +++ b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/builder/JacksonJsonBuilder.kt @@ -16,15 +16,15 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile JacksonJsonBuilder.kt - * LastUpdate 2026-11-16 11:36:46 + * LastUpdate 2026-11-17 20:09:28 * UpdateUser MingLiPro */ package com.mingliqiye.utils.springboot.autoconfigure.builder -import com.mingliqiye.utils.json.converters.base.JsonConverter +import com.mingliqiye.utils.json.converters.base.BaseJsonConverter sealed class JacksonJsonBuilder { - abstract fun addJsonConverter(jsonConverter: JsonConverter<*, *>): JacksonJsonBuilder - abstract fun addJsonConverters(vararg jsonConverters: JsonConverter<*, *>): JacksonJsonBuilder + abstract fun addJsonConverter(jsonConverter: BaseJsonConverter<*>): JacksonJsonBuilder + abstract fun addJsonConverters(vararg jsonConverters: BaseJsonConverter<*>): JacksonJsonBuilder } diff --git a/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/builder/JacksonJsonBuilderImpl.kt b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/builder/JacksonJsonBuilderImpl.kt index 71bf6b6..115ea9b 100644 --- a/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/builder/JacksonJsonBuilderImpl.kt +++ b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/builder/JacksonJsonBuilderImpl.kt @@ -16,28 +16,28 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile JacksonJsonBuilderImpl.kt - * LastUpdate 2026-11-16 11:36:46 + * LastUpdate 2026-11-17 20:09:59 * UpdateUser MingLiPro */ package com.mingliqiye.utils.springboot.autoconfigure.builder -import com.mingliqiye.utils.json.converters.base.JsonConverter +import com.mingliqiye.utils.json.converters.base.BaseJsonConverter open class JacksonJsonBuilderImpl : JacksonJsonBuilder() { - override fun addJsonConverter(jsonConverter: JsonConverter<*, *>): JacksonJsonBuilderImpl { + override fun addJsonConverter(jsonConverter: BaseJsonConverter<*>): JacksonJsonBuilderImpl { data.add(jsonConverter) return this } - override fun addJsonConverters(vararg jsonConverters: JsonConverter<*, *>): JacksonJsonBuilder { + override fun addJsonConverters(vararg jsonConverters: BaseJsonConverter<*>): JacksonJsonBuilder { data.addAll(jsonConverters) return this } - private val data = mutableListOf>() + private val data = mutableListOf>() - fun getData(): List> { + fun getData(): List> { return data.toList() } diff --git a/src/main/kotlin/com/mingliqiye/utils/time/DateTime.kt b/src/main/kotlin/com/mingliqiye/utils/time/DateTime.kt index 27ee885..0f5b78b 100644 --- a/src/main/kotlin/com/mingliqiye/utils/time/DateTime.kt +++ b/src/main/kotlin/com/mingliqiye/utils/time/DateTime.kt @@ -16,7 +16,7 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile DateTime.kt - * LastUpdate 2026-11-17 10:16:38 + * LastUpdate 2026-11-17 20:22:01 * UpdateUser MingLiPro */ @@ -65,8 +65,8 @@ class DateTime private constructor( * @return 返回当前时间的 DateTime 实例 */ @JvmStatic - fun now(): DateTime { - return DateTime(LocalDateTime.now()) + fun now(zone: ZoneId = ZoneId.systemDefault()): DateTime { + return DateTime(LocalDateTime.now(zone)) } /** @@ -346,6 +346,10 @@ class DateTime private constructor( return localDateTime } + fun atZone(zoneId: ZoneId = ZoneId.systemDefault()): DateTime { + return DateTime(localDateTime, zoneId) + } + /** * 在当前时间基础上增加指定的时间偏移量。 * @@ -444,7 +448,7 @@ class DateTime private constructor( * @return 返回标准格式的时间字符串 */ override fun toString(): String { - return "DateTime(${format()})" + return "DateTime(${format()},zoneId=${zoneId})" } /** diff --git a/src/main/kotlin/com/mingliqiye/utils/tuple/Tuples.kt b/src/main/kotlin/com/mingliqiye/utils/tuple/Tuples.kt index 074e17b..0ad30cd 100644 --- a/src/main/kotlin/com/mingliqiye/utils/tuple/Tuples.kt +++ b/src/main/kotlin/com/mingliqiye/utils/tuple/Tuples.kt @@ -16,7 +16,7 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile Tuples.kt - * LastUpdate 2026-06-21 10:40:58 + * LastUpdate 2026-11-17 20:29:54 * UpdateUser MingLiPro */ @@ -25,18 +25,10 @@ package com.mingliqiye.utils.tuple import java.io.Serializable -abstract class Tuple(val size: Int = 1) : Serializable, Iterable { +abstract class Tuple(val size: Int = 1) : Serializable, Iterable, Comparable { override fun iterator(): Iterator { - return object : Iterator { - private var index: Int = 0 - override fun next(): Any { - if (!hasNext()) throw NoSuchElementException("element next is None") - return get(index++) - } - - override fun hasNext(): Boolean = index < size - } + return toMutableList().iterator() } open fun toList(): List { diff --git a/src/test/kotlin/com/mingliqiye/utils/TestJson.kt b/src/test/kotlin/com/mingliqiye/utils/TestJson.kt index e82011c..d8fe6c8 100644 --- a/src/test/kotlin/com/mingliqiye/utils/TestJson.kt +++ b/src/test/kotlin/com/mingliqiye/utils/TestJson.kt @@ -16,18 +16,41 @@ * ProjectName mingli-utils * ModuleName mingli-utils.test * CurrentFile TestJson.kt - * LastUpdate 2026-06-20 22:05:31 + * LastUpdate 2026-11-17 20:17:51 * UpdateUser MingLiPro */ package com.mingliqiye.utils +import com.mingliqiye.logger.Loggers +import com.mingliqiye.utils.io.SIO.println +import com.mingliqiye.utils.json.api.JSONA +import com.mingliqiye.utils.json.api.Jackson3JsonApi +import com.mingliqiye.utils.json.converters.DateTimeJsonConverter +import com.mingliqiye.utils.json.converters.base.getJackson3Module +import com.mingliqiye.utils.time.DateTime import org.junit.jupiter.api.Test +import tools.jackson.module.kotlin.jsonMapper +import tools.jackson.module.kotlin.kotlinModule class TestJson { - @Test - fun test() { - } + + private val log = Loggers.getLogger() + + @Test + fun test() { + JSONA.setJsonApi( + Jackson3JsonApi( + jsonMapper { + addModule(kotlinModule {}) + addModules( + DateTimeJsonConverter().getJackson3Module() + ) + } + )) + + JSONA.parse(JSONA.format(DateTime.now())).println() + } }