refactor(json): 重构 JSON 转换器体系,新增 reader/writer/judgment 抽象

移除按类型划分的 BaseJson*Converter,统一由 BaseJsonConverter 与
JsonConverterUtils 处理;为 Jackson2/Jackson3 分别实现 JsonReader、
JsonWriter 与 ReaderJudgment,并同步更新 SpringBoot 自动配置与测试。
This commit is contained in:
2026-11-17 20:35:19 +08:00
parent 16da482de7
commit d0887ea1f7
53 changed files with 1721 additions and 1026 deletions
@@ -16,7 +16,7 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile JsonParserException.kt * CurrentFile JsonParserException.kt
* LastUpdate 2026-10-11 16:53:41 * LastUpdate 2026-11-17 16:22:52
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
@@ -28,3 +28,11 @@ class JsonParserException @JvmOverloads constructor(
enableSuppression: Boolean = false, enableSuppression: Boolean = false,
writableStackTrace: Boolean = true, writableStackTrace: Boolean = true,
) : JsonException(message, throwable, enableSuppression, writableStackTrace) ) : 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)
@@ -16,7 +16,7 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile JSONA.kt * CurrentFile JSONA.kt
* LastUpdate 2026-11-07 13:29:54 * LastUpdate 2026-11-17 19:59:21
* UpdateUser MingLiPro * 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.base.JsonApi
import com.mingliqiye.utils.json.api.type.JsonTypeReference import com.mingliqiye.utils.json.api.type.JsonTypeReference
import com.mingliqiye.utils.json.api.type.listType 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 com.mingliqiye.utils.json.objects.base.JsonItem
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
@@ -522,21 +520,6 @@ object JSONA {
fun <D> convert(source: Any, destinationType: JsonTypeReference<D>): D = fun <D> convert(source: Any, destinationType: JsonTypeReference<D>): D =
jsonApi.convert(source, destinationType) jsonApi.convert(source, destinationType)
/**
* 添加 JSON 转换器到 API 中。
*
* @param c 需要添加的 [BaseJsonConverter] 实例
*/
@JvmStatic
fun addJsonConverter(c: BaseJsonConverter<*, *>) = jsonApi.addJsonConverter(c)
/**
* 使用反射创建并添加指定类型的 JSON 转换器到 API 中。
*
* @param T 继承自 [BaseJsonConverter] 的具体实现类
*/
inline fun <reified T : BaseJsonConverter<*, *>> addJsonConverter() = addJsonConverter(getJsonConverter<T>())
/** /**
* 将当前字符串解析为指定泛型类型的对象。 * 将当前字符串解析为指定泛型类型的对象。
* *
@@ -16,7 +16,7 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile Jackson3JsonApi.kt * CurrentFile Jackson3JsonApi.kt
* LastUpdate 2026-10-12 13:59:59 * LastUpdate 2026-11-17 19:59:59
* UpdateUser MingLiPro * 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.JackSon3JsonItem.Companion.getJsonItem
import com.mingliqiye.utils.json.api.base.JsonApi import com.mingliqiye.utils.json.api.base.JsonApi
import com.mingliqiye.utils.json.api.type.JsonTypeReference 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 com.mingliqiye.utils.json.objects.base.*
import tools.jackson.core.type.TypeReference import tools.jackson.core.type.TypeReference
import tools.jackson.databind.JsonNode import tools.jackson.databind.JsonNode
@@ -213,11 +211,6 @@ class Jackson3JsonApi : JsonApi {
return current 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 { override fun parseJsonItem(json: String): JsonItem {
try { try {
@@ -16,7 +16,7 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile JacksonJsonApi.kt * CurrentFile JacksonJsonApi.kt
* LastUpdate 2026-10-12 10:48:31 * LastUpdate 2026-11-17 19:59:59
* UpdateUser MingLiPro * 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.JackSonJsonItem.Companion.getJsonItem
import com.mingliqiye.utils.json.api.base.JsonApi import com.mingliqiye.utils.json.api.base.JsonApi
import com.mingliqiye.utils.json.api.type.JsonTypeReference 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.* 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 { override fun parseJsonItem(json: String): JsonItem {
try { try {
return getJsonItem(objectMapper.readTree(json)) return getJsonItem(objectMapper.readTree(json))
@@ -16,7 +16,7 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile JsonApi.kt * CurrentFile JsonApi.kt
* LastUpdate 2026-03-27 14:19:31 * LastUpdate 2026-11-17 19:59:21
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
@@ -25,8 +25,6 @@ package com.mingliqiye.utils.json.api.base
import com.mingliqiye.utils.exception.JsonParserException import com.mingliqiye.utils.exception.JsonParserException
import com.mingliqiye.utils.json.api.type.JsonTypeReference import com.mingliqiye.utils.json.api.type.JsonTypeReference
import com.mingliqiye.utils.json.api.type.listType 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 com.mingliqiye.utils.json.objects.base.JsonItem
import okio.IOException import okio.IOException
import java.io.* import java.io.*
@@ -131,17 +129,6 @@ interface JsonApi {
*/ */
@JvmStatic @JvmStatic
inline fun <reified D> JsonApi.convert(source: Any): D = convert(source, object : JsonTypeReference<D>() {}) inline fun <reified D> JsonApi.convert(source: Any): D = convert(source, object : JsonTypeReference<D>() {})
/**
* 添加JSON转换器到API中
*
* @param T 需要添加的JSON转换器类
*/
@JvmStatic
inline fun <reified T : BaseJsonConverter<*, *>> JsonApi.addJsonConverter() {
addJsonConverter(getJsonConverter<T>())
}
} }
/** /**
@@ -513,20 +500,6 @@ interface JsonApi {
*/ */
fun <D> convert(source: Any, destinationType: JsonTypeReference<D>): D fun <D> convert(source: Any, destinationType: JsonTypeReference<D>): D
/**
* 注册自定义的 JSON 转换器
* 用于处理特定类型的自定义序列化和反序列化逻辑
*
* @param c 自定义的 JSON 转换器实例
*
* 使用示例:
* ```
* converter.addJsonConverter(DateJsonConverter())
* converter.addJsonConverter(LocalDateTimeJsonConverter())
* ```
*/
fun addJsonConverter(c: BaseJsonConverter<*, *>)
/** /**
* 将 JSON 字符串解析为 JsonItem 对象 * 将 JSON 字符串解析为 JsonItem 对象
* *
@@ -16,82 +16,51 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile DateTimeJsonConverter.kt * CurrentFile DateTimeJsonConverter.kt
* LastUpdate 2026-11-09 14:35:06 * LastUpdate 2026-11-17 20:17:32
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
package com.mingliqiye.utils.json.converters package com.mingliqiye.utils.json.converters
import com.google.gson.JsonParseException
import com.mingliqiye.utils.annotation.DateTimeJsonFormat import com.mingliqiye.utils.annotation.DateTimeJsonFormat
import com.mingliqiye.utils.json.api.type.JsonTypeReference import com.mingliqiye.utils.json.api.type.JsonTypeReference
import com.mingliqiye.utils.json.converters.base.AnnotationGetter 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.BaseJsonConverter
import com.mingliqiye.utils.json.converters.base.BaseJsonStringConverter import com.mingliqiye.utils.json.converters.base.getAnnotation
import com.mingliqiye.utils.objects.isNull import com.mingliqiye.utils.json.reader.base.BaseJsonReader
import com.mingliqiye.utils.string.isNullish import com.mingliqiye.utils.json.witer.base.BaseJsonWriter
import com.mingliqiye.utils.time.DateTime import com.mingliqiye.utils.time.DateTime
import com.mingliqiye.utils.time.Formatter import com.mingliqiye.utils.time.Formatter
class DateTimeJsonConverter : BaseJsonConverter<DateTime> {
/**
* DateTimeJsonConverter 是一个用于处理 DateTime 类型与 JSON 字符串之间转换的类。
* 它继承自 BaseJsonStringConverter,提供了序列化(convert)和反序列化(deConvert)的功能。
*/
class DateTimeJsonConverter private constructor() : BaseJsonStringConverter<DateTime> {
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 如果在转换过程中发生异常。
*/
@Throws(Exception::class) @Throws(Exception::class)
override fun convert(obj: DateTime?, annotationGetter: AnnotationGetter): String? { override fun convert(
// 如果输入对象为 null,直接返回 null obj: DateTime?,
if (obj.isNull()) return null jsonWriter: BaseJsonWriter,
annotationGetter: AnnotationGetter,
// 获取 DateTimeJsonFormat 注解信息 ) {
val dateTimeJsonFormat: DateTimeJsonFormat? = annotationGetter.get<DateTimeJsonFormat>() if (obj == null) {
jsonWriter.setNull()
// 根据注解中的格式化规则进行转换 return
return if (Formatter.NONE != dateTimeJsonFormat?.value && dateTimeJsonFormat != null) { }
val dateTimeJsonFormat: DateTimeJsonFormat? = annotationGetter.getAnnotation<DateTimeJsonFormat>()
val value = if (Formatter.NONE != dateTimeJsonFormat?.value && dateTimeJsonFormat != null) {
obj.format(dateTimeJsonFormat.value, dateTimeJsonFormat.repcZero) obj.format(dateTimeJsonFormat.value, dateTimeJsonFormat.repcZero)
} else if (dateTimeJsonFormat?.formatter?.isEmpty() == false) { } else if (dateTimeJsonFormat?.formatter?.isEmpty() == false) {
obj.format(dateTimeJsonFormat.formatter, dateTimeJsonFormat.repcZero) obj.format(dateTimeJsonFormat.formatter, dateTimeJsonFormat.repcZero)
} else { } else {
obj.format() 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 注解信息 override fun deConvert(jsonReader: BaseJsonReader, annotationGetter: AnnotationGetter): DateTime? {
val dateTimeJsonFormat: DateTimeJsonFormat? = annotationGetter.get<DateTimeJsonFormat>() val dateTimeJsonFormat: DateTimeJsonFormat? = annotationGetter.getAnnotation<DateTimeJsonFormat>()
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) { return if (Formatter.NONE != dateTimeJsonFormat?.value && dateTimeJsonFormat != null) {
DateTime.parse(obj, dateTimeJsonFormat.value, dateTimeJsonFormat.repcZero) DateTime.parse(obj, dateTimeJsonFormat.value, dateTimeJsonFormat.repcZero)
} else if (dateTimeJsonFormat?.formatter?.isEmpty() == false) { } else if (dateTimeJsonFormat?.formatter?.isEmpty() == false) {
@@ -101,12 +70,5 @@ class DateTimeJsonConverter private constructor() : BaseJsonStringConverter<Date
} }
} }
/** override fun getType(): JsonTypeReference<DateTime> = JsonTypeReference.of<DateTime>()
* 获取当前转换器支持的目标类型引用。
*
* @return 返回 DateTime 类型的 JsonTypeReference。
* @throws Exception 如果在获取类型引用时发生异常。
*/
@Throws(Exception::class)
override fun getFromType(): JsonTypeReference<DateTime> = JsonTypeReference.of<DateTime>()
} }
@@ -16,16 +16,18 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile NetworkEndpointJsonConverter.kt * CurrentFile NetworkEndpointJsonConverter.kt
* LastUpdate 2026-11-09 14:36:36 * LastUpdate 2026-11-17 20:01:08
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
package com.mingliqiye.utils.json.converters package com.mingliqiye.utils.json.converters
import com.google.gson.JsonParseException
import com.mingliqiye.utils.json.api.type.JsonTypeReference 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.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 import com.mingliqiye.utils.network.NetworkEndpoint
/** /**
@@ -41,60 +43,24 @@ import com.mingliqiye.utils.network.NetworkEndpoint
* @see BaseJsonStringConverter * @see BaseJsonStringConverter
* @see NetworkEndpoint * @see NetworkEndpoint
*/ */
class NetworkEndpointJsonConverter private constructor() : BaseJsonStringConverter<NetworkEndpoint> { class NetworkEndpointJsonConverter : BaseJsonConverter<NetworkEndpoint> {
override fun convert(
companion object { obj: NetworkEndpoint?,
private val networkEndpointJsonConverter by lazy { jsonWriter: BaseJsonWriter,
NetworkEndpointJsonConverter() annotationGetter: AnnotationGetter,
} ) {
if (obj == null) jsonWriter.setNull() else jsonWriter.setString(obj.toHostPortString())
/**
* 获取 [NetworkEndpointJsonConverter] 的全局单例。
*
* @return 共享的转换器实例。
*/
@JvmStatic
fun getJsonConverter(): NetworkEndpointJsonConverter = networkEndpointJsonConverter
} }
/** override fun deConvert(
* 将 [NetworkEndpoint] 序列化为 JSON 字符串。 jsonReader: BaseJsonReader,
* annotationGetter: AnnotationGetter,
* 输出格式为 `host:port`,由 [NetworkEndpoint.toHostPortString] 生成。 ): NetworkEndpoint? {
* return if (jsonReader.isNotNull) {
* @param obj 待序列化的网络端点,可能为 `null`。 if (!jsonReader.isString) throw JsonParseException("type item not string")
* @param annotationGetter 注解信息提供者,用于在需要时读取格式化相关注解; NetworkEndpoint.of(jsonReader.getString())
* 当前实现不依赖注解,保留该参数以符合转换器契约。 } else null
* @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 getType(): JsonTypeReference<NetworkEndpoint> = JsonTypeReference.of()
* 将 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<NetworkEndpoint> = JsonTypeReference.Companion.of()
} }
@@ -16,84 +16,60 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile UUIDJsonConverter.kt * CurrentFile UUIDJsonConverter.kt
* LastUpdate 2026-02-08 01:37:00 * LastUpdate 2026-11-17 20:01:08
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
package com.mingliqiye.utils.json.converters package com.mingliqiye.utils.json.converters
import com.google.gson.JsonParseException
import com.mingliqiye.utils.annotation.UUIDJsonFormat import com.mingliqiye.utils.annotation.UUIDJsonFormat
import com.mingliqiye.utils.base.BaseType import com.mingliqiye.utils.base.BaseType
import com.mingliqiye.utils.json.api.type.JsonTypeReference import com.mingliqiye.utils.json.api.type.JsonTypeReference
import com.mingliqiye.utils.json.converters.base.AnnotationGetter 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.BaseJsonConverter
import com.mingliqiye.utils.json.converters.base.BaseJsonStringConverter import com.mingliqiye.utils.json.converters.base.getAnnotation
import com.mingliqiye.utils.objects.isNull import com.mingliqiye.utils.json.reader.base.BaseJsonReader
import com.mingliqiye.utils.string.isNullish import com.mingliqiye.utils.json.witer.base.BaseJsonWriter
import com.mingliqiye.utils.uuid.UUID import com.mingliqiye.utils.uuid.UUID
import com.mingliqiye.utils.uuid.UUIDFormatType import com.mingliqiye.utils.uuid.UUIDFormatType
/** class UUIDJsonConverter : BaseJsonConverter<UUID> {
* UUIDJsonConverter 是一个用于处理 UUID 类型与 JSON 字符串之间转换的类。
* 它继承自 BaseJsonStringConverter,并实现了 convert 和 deConvert 方法,
* 分别用于将 UUID 对象序列化为字符串以及反序列化字符串为 UUID 对象。
*/
class UUIDJsonConverter private constructor() : BaseJsonStringConverter<UUID> {
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( override fun convert(
obj: UUID?, obj: UUID?,
jsonWriter: BaseJsonWriter,
annotationGetter: AnnotationGetter, annotationGetter: AnnotationGetter,
): String? { ) {
// 如果输入对象为 null,直接返回 null if (obj == null) {
if (obj.isNull()) return null jsonWriter.setNull()
return
// 获取 UUIDJsonFormat 注解信息,若未找到则使用默认格式
val dateTimeJsonFormat: UUIDJsonFormat = annotationGetter.get<UUIDJsonFormat>() ?: 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()
} }
val dateTimeJsonFormat: UUIDJsonFormat? = annotationGetter.getAnnotation<UUIDJsonFormat>()
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( override fun deConvert(
obj: String?, jsonReader: BaseJsonReader,
annotationGetter: AnnotationGetter, annotationGetter: AnnotationGetter,
): UUID? { ): UUID? {
// 如果输入字符串为 null 或空,直接返回 null if (jsonReader.isNull) return null
if (obj.isNullish()) return null if (!jsonReader.isString) throw JsonParseException("type item not string")
// 获取 UUIDJsonFormat 注解信息,若未找到则使用默认解析方式 val obj = jsonReader.getString()
val dateTimeJsonFormat: UUIDJsonFormat = annotationGetter.get<UUIDJsonFormat>() ?: return UUID.of(obj)
// 根据注解中的 base 属性选择合适的解析方式 val dateTimeJsonFormat: UUIDJsonFormat = annotationGetter.getAnnotation<UUIDJsonFormat>() ?: return UUID.of(obj)
return if (BaseType.BASE16 != dateTimeJsonFormat.base) { return if (BaseType.BASE16 != dateTimeJsonFormat.base) {
UUID.of(obj, dateTimeJsonFormat.base) UUID.of(obj, dateTimeJsonFormat.base)
} else { } else {
@@ -101,10 +77,5 @@ class UUIDJsonConverter private constructor() : BaseJsonStringConverter<UUID> {
} }
} }
/** override fun getType(): JsonTypeReference<UUID> = JsonTypeReference.of()
* 返回当前转换器支持的目标类型引用。
*
* @return 表示 UUID 类型的 JsonTypeReference 对象。
*/
override fun getFromType() = JsonTypeReference.of<UUID>()
} }
@@ -16,67 +16,12 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile AnnotationGetter.kt * CurrentFile AnnotationGetter.kt
* LastUpdate 2026-03-11 08:46:40 * LastUpdate 2026-11-17 19:46:16
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
package com.mingliqiye.utils.json.converters.base package com.mingliqiye.utils.json.converters.base
/**
* 定义一个用于获取注解的接口。
* 提供了一个通用方法来根据注解类型获取对应的注解实例。
*/
interface AnnotationGetter { interface AnnotationGetter {
/** fun <T : Annotation> getAnnotation(clazz: Class<T>): T? = null
* 根据指定的注解类型获取该类型的注解实例。
*
* @param clazz 注解的Class对象,表示要获取的注解类型。
* @return 返回对应类型的注解实例,如果未找到则返回null。
*/
fun <T : Annotation> get(clazz: Class<T>): T?
companion object {
/**
* 使用内联函数和重ified类型参数简化注解获取操作。
* 通过传入泛型参数自动推断注解类型,避免手动传递Class对象。
*
* @param T 注解类型,必须继承自Annotation。
* @return 返回对应类型的注解实例,如果未找到则返回null。
*/
inline fun <reified T : Annotation> AnnotationGetter.get(): T? = this.get(T::class.java)
/**
* 提供一个默认实现,始终返回null。
* 可用于需要空注解获取器的场景。
*/
val nullGetter = object : AnnotationGetter {
/**
* 始终返回null,不执行任何实际逻辑。
*
* @param clazz 注解的Class对象(未使用)。
* @return 始终返回null。
*/
override fun <T : Annotation> get(clazz: Class<T>): 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 <T : Annotation> get(clazz: Class<T>): T? =
if (annotation.javaClass == clazz) annotation as T else 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<E> : BaseJsonConverter<E, BigDecimal> {
/**
* 获取目标类型的引用信息。
*
* 该方法返回一个 [JsonTypeReference] 对象,表示目标类型为 [BigDecimal]。
* 通过调用 [JsonTypeReference.Companion.of] 方法创建类型引用。
*
* @return 目标类型 [BigDecimal] 的类型引用。
* @throws Exception 如果在获取类型引用时发生异常。
*/
@Throws(Exception::class)
override fun getToType() = JsonTypeReference.of<BigDecimal>()
}
@@ -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<E> : BaseJsonConverter<E, BigInteger> {
/**
* 获取目标类型的引用,用于 JSON 反序列化时确定目标类型。
* 该方法重写了父接口中的 [getToType] 方法,并返回 `BigInteger` 类型的引用。
*
* @return 返回 [JsonTypeReference] 对象,表示 `BigInteger` 类型的引用。
* @throws Exception 如果在获取类型引用过程中发生异常,则抛出该异常。
*/
@Throws(Exception::class)
override fun getToType() = JsonTypeReference.of<BigInteger>()
}
@@ -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<E> : BaseJsonConverter<E, Boolean> {
/**
* 获取目标类型的引用信息。
*
* 该方法返回一个 [JsonTypeReference] 对象,表示目标类型为 [Boolean]。
* 此方法重写了父接口中的定义,并通过 [JsonTypeReference.Companion.of] 方法获取布尔类型的引用。
*
* @return [JsonTypeReference<Boolean>] 表示布尔类型的引用。
* @throws Exception 如果在获取类型引用时发生异常,则抛出此异常。
*/
@Throws(Exception::class)
override fun getToType() = JsonTypeReference.of<Boolean>()
}
@@ -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<E> : BaseJsonConverter<E, Byte> {
/**
* 获取目标类型的引用,用于标识转换的目标类型为 Byte。
*
* @return 返回 JsonTypeReference 类型的实例,表示目标类型为 Byte。
* @throws Exception 如果在获取类型引用过程中发生异常,则抛出该异常。
*/
@Throws(Exception::class)
override fun getToType() = JsonTypeReference.of<Byte>()
}
@@ -16,19 +16,33 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile BaseJsonConverter.kt * CurrentFile BaseJsonConverter.kt
* LastUpdate 2026-02-26 15:59:35 * LastUpdate 2026-11-17 20:10:50
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
package com.mingliqiye.utils.json.converters.base 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 定义了转换的源类型和目标类型。 * @param F 源类型,表示需要被转换的对象类型。
* 实现该接口的类需要提供具体的 JSON 转换逻辑。 * @param T 目标类型,表示转换后的对象类型。
*
* @param F 源类型,表示需要被转换的数据类型。
* @param T 目标类型,表示转换后的数据类型。
*/ */
interface BaseJsonConverter<F, T> : JsonConverter<F, T> interface BaseJsonConverter<T> {
@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<T>
@Throws(Exception::class)
fun getClazz() = getType().getRawType()
}
@@ -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<E> : BaseJsonConverter<E, Double> {
/**
* 获取目标类型的引用,用于标识转换的目标类型为 Double。
*
* @return 返回 JsonTypeReference<Double> 类型的实例,表示目标类型为 Double。
* @throws Exception 如果在获取类型引用时发生异常,则抛出该异常。
*/
@Throws(Exception::class)
override fun getToType() = JsonTypeReference.of<Double>()
}
@@ -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<E> : BaseJsonConverter<E, Float> {
/**
* 获取目标类型的引用信息。
*
* 该方法重写了父接口中的 [getToType] 方法,返回 [Float] 类型的 [JsonTypeReference] 实例。
* 用于标识当前转换器的目标类型为 [Float]。
*
* @return [JsonTypeReference] 表示 [Float] 类型的引用信息。
* @throws Exception 如果在获取类型引用时发生异常,则抛出此异常。
*/
@Throws(Exception::class)
override fun getToType() = JsonTypeReference.of<Float>()
}
@@ -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<E> : BaseJsonConverter<E, Int> {
/**
* 获取目标类型的引用信息。
*
* 该方法重写了父接口中的 [getToType] 方法,返回 [Int] 类型的 [JsonTypeReference] 实例。
* 用于标识当前转换器的目标类型为 [Int]。
*
* @return 返回 [JsonTypeReference] 的实例,表示目标类型为 [Int]。
* @throws Exception 如果在获取类型引用时发生异常,则抛出该异常。
*/
@Throws(Exception::class)
override fun getToType() = JsonTypeReference.of<Int>()
}
@@ -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<E> : BaseJsonConverter<E, Long> {
/**
* 获取目标类型的引用,用于标识此转换器的目标类型为 [Long]。
*
* @return 返回 [JsonTypeReference] 的实例,表示目标类型为 [Long]。
* @throws Exception 如果在获取类型引用时发生异常,则抛出此异常。
*/
@Throws(Exception::class)
override fun getToType() = JsonTypeReference.of<Long>()
}
@@ -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<E> : BaseJsonConverter<E, Short> {
/**
* 获取目标类型的引用信息。
*
* 该方法重写了父接口中的 [getToType] 方法,返回 [Short] 类型的 [JsonTypeReference] 实例。
* 通过 [JsonTypeReference.Companion.of] 方法创建类型引用。
*
* @return [JsonTypeReference] 表示 [Short] 类型的引用。
* @throws Exception 如果在获取类型引用过程中发生异常,则抛出该异常。
*/
@Throws(Exception::class)
override fun getToType() = JsonTypeReference.of<Short>()
}
@@ -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<E> : BaseJsonConverter<E, String> {
/**
* 获取目标类型的引用信息。
*
* 该方法重写了父接口中的 [getToType] 方法,返回一个表示 [String] 类型的 [JsonTypeReference] 实例。
* 此方法可能抛出异常,因此使用 [@Throws(Exception::class)] 注解标记。
*
* @return 返回一个 [JsonTypeReference] 实例,表示目标类型为 [String]。
* @throws Exception 如果在获取类型引用时发生错误,则抛出此异常。
*/
@Throws(Exception::class)
override fun getToType() = JsonTypeReference.of<String>()
}
@@ -16,32 +16,34 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile JackJson3Deserializer.kt * CurrentFile JackJson3Deserializer.kt
* LastUpdate 2026-02-26 13:02:52 * LastUpdate 2026-11-17 19:51:20
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
package com.mingliqiye.utils.json.converters.base package com.mingliqiye.utils.json.converters.base
import com.mingliqiye.utils.json.reader.JackSon3JsonReader
import tools.jackson.core.JsonParser import tools.jackson.core.JsonParser
import tools.jackson.core.JsonToken
import tools.jackson.databind.BeanProperty import tools.jackson.databind.BeanProperty
import tools.jackson.databind.DeserializationContext import tools.jackson.databind.DeserializationContext
import tools.jackson.databind.ValueDeserializer import tools.jackson.databind.ValueDeserializer
class JackJson3Deserializer<F, T>( class JackJson3Deserializer<T>(
val property: BeanProperty? = null, val property: BeanProperty? = null,
val jsonConverter: JsonConverter<F, T>, val jsonConverter: BaseJsonConverter<T>,
) : ValueDeserializer<F>() { ) : ValueDeserializer<T>() {
val annotationGetter =
object : AnnotationGetter {
override fun <T : Annotation> getAnnotation(clazz: Class<T>): T? = property?.getAnnotation(clazz)
}
override fun deserialize( override fun deserialize(
p: JsonParser, p: JsonParser,
ctxt: DeserializationContext?, ctxt: DeserializationContext?,
): F? { ): T? {
if (p.currentToken() == JsonToken.VALUE_NULL) return null return jsonConverter.deConvert(JackSon3JsonReader(p.readValueAsTree()), annotationGetter)
return jsonConverter.deConvert(p.readValueAs(jsonConverter.getToClass()), object : AnnotationGetter {
override fun <T : Annotation> get(clazz: Class<T>): T? = property?.getAnnotation(clazz)
})
} }
/** /**
@@ -54,7 +56,7 @@ class JackJson3Deserializer<F, T>(
override fun createContextual( override fun createContextual(
ctxt: DeserializationContext?, ctxt: DeserializationContext?,
property: BeanProperty?, property: BeanProperty?,
): JackJson3Deserializer<F, T> { ): JackJson3Deserializer<T> {
return JackJson3Deserializer(property, jsonConverter) return JackJson3Deserializer(property, jsonConverter)
} }
@@ -16,48 +16,31 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile JackJson3Serializer.kt * CurrentFile JackJson3Serializer.kt
* LastUpdate 2026-02-26 13:04:18 * LastUpdate 2026-11-17 19:51:33
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
package com.mingliqiye.utils.json.converters.base 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.BeanProperty
import tools.jackson.databind.SerializationContext import tools.jackson.databind.SerializationContext
import tools.jackson.databind.ValueSerializer import tools.jackson.databind.ValueSerializer
import java.math.BigDecimal
import java.math.BigInteger
class JackJson3Serializer<F, T>(
class JackJson3Serializer<T>(
val property: BeanProperty? = null, val property: BeanProperty? = null,
val jsonConverter: JsonConverter<F, T>, val jsonConverter: BaseJsonConverter<T>,
) : ValueSerializer<F>() { ) : ValueSerializer<T>() {
/** val annotationGetter =
* 序列化方法:将源对象转换为目标对象,并根据目标对象的类型写入 JSON。 object : AnnotationGetter {
* override fun <T : Annotation> getAnnotation(clazz: Class<T>): T? = property?.getAnnotation(clazz)
* @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 <T : Annotation> get(clazz: Class<T>): 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")
} }
override fun serialize(value: T, gen: JsonGenerator, ctxt: SerializationContext) {
jsonConverter.convert(value, JackSon3JsonWriter(gen, ctxt), annotationGetter)
} }
override fun createContextual(ctxt: SerializationContext?, property: BeanProperty?): ValueSerializer<*> { override fun createContextual(ctxt: SerializationContext?, property: BeanProperty?): ValueSerializer<*> {
@@ -16,44 +16,41 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile JackJsonDeserializer.kt * CurrentFile JackJsonDeserializer.kt
* LastUpdate 2026-02-08 02:22:46 * LastUpdate 2026-11-17 19:53:29
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
package com.mingliqiye.utils.json.converters.base package com.mingliqiye.utils.json.converters.base
import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.JsonToken
import com.fasterxml.jackson.databind.BeanProperty import com.fasterxml.jackson.databind.BeanProperty
import com.fasterxml.jackson.databind.DeserializationContext import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.deser.ContextualDeserializer import com.fasterxml.jackson.databind.deser.ContextualDeserializer
import com.mingliqiye.utils.json.reader.JackSonJsonReader
class JackJsonDeserializer<F, T>( class JackJsonDeserializer<T>(
val property: BeanProperty? = null, val property: BeanProperty? = null,
val jsonConverter: JsonConverter<F, T>, val jsonConverter: BaseJsonConverter<T>,
) : JsonDeserializer<F>(), ContextualDeserializer { ) : JsonDeserializer<T>(), ContextualDeserializer {
val annotationGetter =
object : AnnotationGetter {
override fun <T : Annotation> getAnnotation(clazz: Class<T>): T? = property?.getAnnotation(clazz)
}
override fun deserialize( override fun deserialize(
p: JsonParser, p: JsonParser,
ctxt: DeserializationContext?, ctxt: DeserializationContext?,
): F? { ): T? {
if (p.currentToken == JsonToken.VALUE_NULL) return null return jsonConverter.deConvert(JackSonJsonReader(p.readValueAsTree()), annotationGetter)
return jsonConverter.deConvert(p.readValueAs(jsonConverter.getToClass()), object : AnnotationGetter {
override fun <T : Annotation> get(clazz: Class<T>): T? = property?.getAnnotation(clazz)
})
} }
/**
* 上下文感知方法:设置当前属性信息。
*
* @param ctxt 反序列化上下文,可选参数。
* @param property 当前属性信息。
* @return 返回当前反序列化器实例。
*/
override fun createContextual( override fun createContextual(
ctxt: DeserializationContext?, ctxt: DeserializationContext?,
property: BeanProperty?, property: BeanProperty?,
): JackJsonDeserializer<F, T> { ): JackJsonDeserializer<T> {
return JackJsonDeserializer(property, jsonConverter) return JackJsonDeserializer(property, jsonConverter)
} }
} }
@@ -16,7 +16,7 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile JackJsonSerializer.kt * CurrentFile JackJsonSerializer.kt
* LastUpdate 2026-02-26 12:55:57 * LastUpdate 2026-11-17 19:54:12
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
@@ -27,13 +27,16 @@ import com.fasterxml.jackson.databind.BeanProperty
import com.fasterxml.jackson.databind.JsonSerializer import com.fasterxml.jackson.databind.JsonSerializer
import com.fasterxml.jackson.databind.SerializerProvider import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.ser.ContextualSerializer import com.fasterxml.jackson.databind.ser.ContextualSerializer
import java.math.BigDecimal import com.mingliqiye.utils.json.witer.JackSonJsonWriter
import java.math.BigInteger
class JackJsonSerializer<F, T>( class JackJsonSerializer<T>(
val property: BeanProperty?, val property: BeanProperty?,
val jsonConverter: JsonConverter<F, T>, val jsonConverter: BaseJsonConverter<T>,
) : JsonSerializer<F>(), ContextualSerializer { ) : JsonSerializer<T>(), ContextualSerializer {
val annotationGetter = object : AnnotationGetter {
override fun <T : Annotation> getAnnotation(clazz: Class<T>): T? = property?.getAnnotation(clazz)
}
/** /**
* 序列化方法:将源对象转换为目标对象,并根据目标对象的类型写入 JSON。 * 序列化方法:将源对象转换为目标对象,并根据目标对象的类型写入 JSON。
@@ -43,30 +46,11 @@ class JackJsonSerializer<F, T>(
* @param provider 序列化提供者,可选参数。 * @param provider 序列化提供者,可选参数。
*/ */
override fun serialize( override fun serialize(
value: F?, value: T?,
gen: JsonGenerator, gen: JsonGenerator,
provider: SerializerProvider?, provider: SerializerProvider?,
) { ) {
// 执行转换逻辑 jsonConverter.convert(value, JackSonJsonWriter(gen, provider), annotationGetter)
val data: T? = jsonConverter.convert(value, object : AnnotationGetter {
override fun <T : Annotation> get(clazz: Class<T>): 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")
}
} }
/** /**
@@ -79,7 +63,7 @@ class JackJsonSerializer<F, T>(
override fun createContextual( override fun createContextual(
prov: SerializerProvider?, prov: SerializerProvider?,
property: BeanProperty?, property: BeanProperty?,
): JsonSerializer<F> { ): JsonSerializer<T> {
return JackJsonSerializer(property, jsonConverter) return JackJsonSerializer(property, this.jsonConverter)
} }
} }
@@ -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<F, T> {
/**
* 将源对象转换为目标对象。
*
* @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<F>
/**
* 获取目标类型的类型引用。
*
* @return 目标类型的 JsonTypeReference 对象。
* @throws Exception 获取过程中可能抛出的异常。
*/
@Throws(Exception::class)
fun getToType(): JsonTypeReference<T>
/**
* 获取源类型的原始类。
*
* @return 源类型的 Class 对象。
* @throws Exception 获取过程中可能抛出的异常。
*/
@Throws(Exception::class)
fun getFromClass() = getFromType().getRawType()
/**
* 获取目标类型的原始类。
*
* @return 目标类型的 Class 对象。
* @throws Exception 获取过程中可能抛出的异常。
*/
@Throws(Exception::class)
fun getToClass() = getToType().getRawType()
}
@@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* ProjectName FastBaseMapper.kt * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile JsonConverterUtils.kt * CurrentFile JsonConverterUtils.kt
* LastUpdate 2026-10-26 23:22:18 * LastUpdate 2026-11-17 20:11:04
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
@file:JvmName("JsonConverterUtils") @file:JvmName("JsonConverterUtils")
@@ -24,61 +24,53 @@
package com.mingliqiye.utils.json.converters.base package com.mingliqiye.utils.json.converters.base
import com.mingliqiye.utils.logger.MingLiLoggerFactory import java.math.BigDecimal
import java.lang.reflect.ParameterizedType import java.math.BigInteger
import java.lang.reflect.Type 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")
/** fun <T> BaseJsonConverter<T>.getJacksonModule(): JackSon2SimpleModule =
* 获取给定类型的实际类对象。 JackSon2SimpleModule("${getClazz().simpleName}Module")
* .addSerializer(getClazz(), JackJsonSerializer(null, this))
* @param type 类型对象,可以是Class、ParameterizedType或其他Type的实现。 .addDeserializer(getClazz(), JackJsonDeserializer(null, this))
* @return 返回与给定类型对应的Class对象;如果无法解析,则返回null。
*/ fun <T> BaseJsonConverter<T>.getJackson3Module(): JackSon3SimpleModule =
fun getClass(type: Type?): Class<*>? { JackSon3SimpleModule("${getClazz().simpleName}Module")
// 尝试将type直接转换为Class类型,如果失败则检查是否为ParameterizedType .addSerializer(getClazz(), JackJson3Serializer(null, this))
val clazz: Class<*>? = type as? Class<*> ?: if (type is ParameterizedType) { .addDeserializer(getClazz(), JackJson3Deserializer(null, this))
// 如果是ParameterizedType,则递归获取其原始类型
getClass(type.rawType)
} else { inline fun <reified T : Annotation> AnnotationGetter.getAnnotation(): T? =
null 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 <reified T : JsonConverter<*, *>> getJsonConverter(): T = getJsonConverter(T::class.java) fun JackSon2JsonGenerator.writeNumber(value: Number): JackSon2JsonGenerator {
when (value) {
@Suppress("UNCHECKED_CAST") is Long -> this.writeNumber(value)
fun <T : JsonConverter<*, *>> getJsonConverter(clazz: Class<T>): T { is Short -> this.writeNumber(value)
try { is Byte -> this.writeNumber(value.toShort())
return clazz.getDeclaredMethod( is Int -> this.writeNumber(value)
"getJsonConverter" is BigDecimal -> this.writeNumber(value)
).invoke(null) as T is BigInteger -> this.writeNumber(value)
} catch (e: Exception) { is Float -> this.writeNumber(value)
log.error("实现于 JsonConverter<*, *> 的类 必须实现静态方法 JsonConverter<*, *> getJsonConverter()", e) is Double -> this.writeNumber(value)
throw e
} }
return this
} }
fun <T : BaseJsonConverter<*, *>> com.fasterxml.jackson.databind.ObjectMapper.addJsonConverter(clazz: Class<T>): com.fasterxml.jackson.databind.ObjectMapper =
this.registerModule(getJsonConverter(clazz).getJacksonModule())
@JvmName("inline_addJsonConverter")
inline fun <reified T : BaseJsonConverter<*, *>> com.fasterxml.jackson.databind.ObjectMapper.addJsonConverter(): com.fasterxml.jackson.databind.ObjectMapper =
this.registerModule(getJsonConverter(T::class.java).getJacksonModule())
/**
* 创建并返回一个 Jackson 模块,该模块包含自定义的序列化器和反序列化器。
*
* @return 配置了自定义序列化器和反序列化器的 SimpleModule 对象。
*/
fun <F, T> JsonConverter<F, T>.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 <F, T> JsonConverter<F, T>.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))
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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()
}
}
@@ -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
}
@@ -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()
}
}
@@ -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()
}
}
@@ -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
}
@@ -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()
}
}
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
}
}
@@ -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()
}
}
@@ -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
}
}
@@ -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
}
}
@@ -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()
}
}
@@ -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
}
}
@@ -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)
}
@@ -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)
}
@@ -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()
}
@@ -16,7 +16,7 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile JsonAutoConfiguration.kt * CurrentFile JsonAutoConfiguration.kt
* LastUpdate 2026-11-09 14:44:55 * LastUpdate 2026-11-17 20:10:26
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
@@ -36,9 +36,9 @@ open class JsonAutoConfiguration {
fun jacksonBuilder(): JacksonBuilder = JacksonBuilder { fun jacksonBuilder(): JacksonBuilder = JacksonBuilder {
it.apply { it.apply {
addJsonConverters( addJsonConverters(
UUIDJsonConverter.getJsonConverter(), UUIDJsonConverter(),
DateTimeJsonConverter.getJsonConverter(), DateTimeJsonConverter(),
NetworkEndpointJsonConverter.getJsonConverter(), NetworkEndpointJsonConverter(),
) )
} }
} }
@@ -16,15 +16,15 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile JacksonJsonBuilder.kt * CurrentFile JacksonJsonBuilder.kt
* LastUpdate 2026-11-16 11:36:46 * LastUpdate 2026-11-17 20:09:28
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
package com.mingliqiye.utils.springboot.autoconfigure.builder 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 { sealed class JacksonJsonBuilder {
abstract fun addJsonConverter(jsonConverter: JsonConverter<*, *>): JacksonJsonBuilder abstract fun addJsonConverter(jsonConverter: BaseJsonConverter<*>): JacksonJsonBuilder
abstract fun addJsonConverters(vararg jsonConverters: JsonConverter<*, *>): JacksonJsonBuilder abstract fun addJsonConverters(vararg jsonConverters: BaseJsonConverter<*>): JacksonJsonBuilder
} }
@@ -16,28 +16,28 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile JacksonJsonBuilderImpl.kt * CurrentFile JacksonJsonBuilderImpl.kt
* LastUpdate 2026-11-16 11:36:46 * LastUpdate 2026-11-17 20:09:59
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
package com.mingliqiye.utils.springboot.autoconfigure.builder 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() { open class JacksonJsonBuilderImpl : JacksonJsonBuilder() {
override fun addJsonConverter(jsonConverter: JsonConverter<*, *>): JacksonJsonBuilderImpl { override fun addJsonConverter(jsonConverter: BaseJsonConverter<*>): JacksonJsonBuilderImpl {
data.add(jsonConverter) data.add(jsonConverter)
return this return this
} }
override fun addJsonConverters(vararg jsonConverters: JsonConverter<*, *>): JacksonJsonBuilder { override fun addJsonConverters(vararg jsonConverters: BaseJsonConverter<*>): JacksonJsonBuilder {
data.addAll(jsonConverters) data.addAll(jsonConverters)
return this return this
} }
private val data = mutableListOf<JsonConverter<*, *>>() private val data = mutableListOf<BaseJsonConverter<*>>()
fun getData(): List<JsonConverter<*, *>> { fun getData(): List<BaseJsonConverter<*>> {
return data.toList() return data.toList()
} }
@@ -16,7 +16,7 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile DateTime.kt * CurrentFile DateTime.kt
* LastUpdate 2026-11-17 10:16:38 * LastUpdate 2026-11-17 20:22:01
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
@@ -65,8 +65,8 @@ class DateTime private constructor(
* @return 返回当前时间的 DateTime 实例 * @return 返回当前时间的 DateTime 实例
*/ */
@JvmStatic @JvmStatic
fun now(): DateTime { fun now(zone: ZoneId = ZoneId.systemDefault()): DateTime {
return DateTime(LocalDateTime.now()) return DateTime(LocalDateTime.now(zone))
} }
/** /**
@@ -346,6 +346,10 @@ class DateTime private constructor(
return localDateTime return localDateTime
} }
fun atZone(zoneId: ZoneId = ZoneId.systemDefault()): DateTime {
return DateTime(localDateTime, zoneId)
}
/** /**
* 在当前时间基础上增加指定的时间偏移量。 * 在当前时间基础上增加指定的时间偏移量。
* *
@@ -444,7 +448,7 @@ class DateTime private constructor(
* @return 返回标准格式的时间字符串 * @return 返回标准格式的时间字符串
*/ */
override fun toString(): String { override fun toString(): String {
return "DateTime(${format()})" return "DateTime(${format()},zoneId=${zoneId})"
} }
/** /**
@@ -16,7 +16,7 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.main * ModuleName mingli-utils.main
* CurrentFile Tuples.kt * CurrentFile Tuples.kt
* LastUpdate 2026-06-21 10:40:58 * LastUpdate 2026-11-17 20:29:54
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
@@ -25,18 +25,10 @@ package com.mingliqiye.utils.tuple
import java.io.Serializable import java.io.Serializable
abstract class Tuple(val size: Int = 1) : Serializable, Iterable<Any> { abstract class Tuple(val size: Int = 1) : Serializable, Iterable<Any>, Comparable<Tuple> {
override fun iterator(): Iterator<Any> { override fun iterator(): Iterator<Any> {
return object : Iterator<Any> { return toMutableList().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
}
} }
open fun toList(): List<Any> { open fun toList(): List<Any> {
@@ -16,18 +16,41 @@
* ProjectName mingli-utils * ProjectName mingli-utils
* ModuleName mingli-utils.test * ModuleName mingli-utils.test
* CurrentFile TestJson.kt * CurrentFile TestJson.kt
* LastUpdate 2026-06-20 22:05:31 * LastUpdate 2026-11-17 20:17:51
* UpdateUser MingLiPro * UpdateUser MingLiPro
*/ */
package com.mingliqiye.utils 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 org.junit.jupiter.api.Test
import tools.jackson.module.kotlin.jsonMapper
import tools.jackson.module.kotlin.kotlinModule
class TestJson { class TestJson {
@Test
fun test() { private val log = Loggers.getLogger<TestJson>()
}
@Test
fun test() {
JSONA.setJsonApi(
Jackson3JsonApi(
jsonMapper {
addModule(kotlinModule {})
addModules(
DateTimeJsonConverter().getJackson3Module()
)
}
))
JSONA.parse<DateTime>(JSONA.format(DateTime.now())).println()
}
} }