feat(utils): 添加数组工具类和国际化支持
- 添加ByteArray.toHexString和String.toHexByteArray扩展函数 - 实现多种数组类型的copyTo和copyFrom扩展方法 - 在AutoConfiguration中集成I18N国际化功能 - 添加AutoService注解及处理器用于自动服务注册 - 新增BadGatewayException HTTP状态异常类 - 更新启动横幅格式并添加国际化文本支持
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile JacksonJsonApi.kt
|
||||
* LastUpdate 2026-02-05 10:31:14
|
||||
* LastUpdate 2026-02-05 14:41:27
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
@@ -28,8 +28,8 @@ import com.fasterxml.jackson.databind.JsonNode
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.databind.ObjectReader
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode
|
||||
import com.mingliqiye.utils.exception.JsonException
|
||||
import com.mingliqiye.utils.json.api.base.JsonApi
|
||||
import com.mingliqiye.utils.json.api.exception.JsonException
|
||||
import com.mingliqiye.utils.json.api.type.JsonTypeReference
|
||||
import com.mingliqiye.utils.json.converters.base.BaseJsonConverter
|
||||
|
||||
@@ -63,7 +63,7 @@ class JacksonJsonApi : JsonApi {
|
||||
* @param clazz 目标对象类型
|
||||
* @param <T> 泛型参数,表示目标对象类型
|
||||
* @return 解析后的对象
|
||||
* @throws com.mingliqiye.utils.json.api.exception.JsonException 当解析失败时抛出异常
|
||||
* @throws JsonException 当解析失败时抛出异常
|
||||
*/
|
||||
override fun <T> parse(json: String, clazz: Class<T>): T {
|
||||
return try {
|
||||
|
||||
@@ -1,56 +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 JsonException.kt
|
||||
* LastUpdate 2026-02-05 11:12:36
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.json.api.exception
|
||||
|
||||
/**
|
||||
* 自定义异常类,用于处理 JSON 相关操作中出现的错误。
|
||||
*
|
||||
* 该类继承自 [RuntimeException],提供了三种构造函数以支持不同的异常场景:
|
||||
* 1. 仅包含错误信息的构造函数。
|
||||
* 2. 包含错误信息和原因(Throwable)的构造函数。
|
||||
* 3. 仅包含原因(Throwable)的构造函数。
|
||||
*/
|
||||
class JsonException : RuntimeException {
|
||||
|
||||
/**
|
||||
* 构造函数:创建一个带有指定错误信息的 [JsonException] 实例。
|
||||
*
|
||||
* @param message 错误信息描述。
|
||||
*/
|
||||
constructor(message: String) : super(message)
|
||||
|
||||
/**
|
||||
* 构造函数:创建一个带有指定错误信息和原因的 [JsonException] 实例。
|
||||
*
|
||||
* @param message 错误信息描述。
|
||||
* @param cause 导致此异常的根本原因。
|
||||
*/
|
||||
constructor(message: String, cause: Throwable) : super(message, cause)
|
||||
|
||||
/**
|
||||
* 构造函数:创建一个由指定原因引发的 [JsonException] 实例。
|
||||
*
|
||||
* @param cause 导致此异常的根本原因。
|
||||
*/
|
||||
constructor(cause: Throwable) : super(cause)
|
||||
}
|
||||
@@ -16,12 +16,13 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile DateTimeJsonConverter.kt
|
||||
* LastUpdate 2026-02-05 11:18:57
|
||||
* LastUpdate 2026-02-07 22:13:41
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.json.converters
|
||||
|
||||
import com.mingliqiye.utils.annotation.DateTimeJsonFormat
|
||||
import com.mingliqiye.utils.json.api.type.JsonTypeReference
|
||||
import com.mingliqiye.utils.json.converters.base.AnnotationGetter
|
||||
import com.mingliqiye.utils.json.converters.base.AnnotationGetter.Companion.get
|
||||
@@ -29,7 +30,6 @@ import com.mingliqiye.utils.json.converters.base.BaseJsonStringConverter
|
||||
import com.mingliqiye.utils.objects.isNull
|
||||
import com.mingliqiye.utils.string.isNullish
|
||||
import com.mingliqiye.utils.time.DateTime
|
||||
import com.mingliqiye.utils.time.DateTimeJsonFormat
|
||||
import com.mingliqiye.utils.time.Formatter
|
||||
|
||||
|
||||
@@ -37,7 +37,17 @@ import com.mingliqiye.utils.time.Formatter
|
||||
* DateTimeJsonConverter 是一个用于处理 DateTime 类型与 JSON 字符串之间转换的类。
|
||||
* 它继承自 BaseJsonStringConverter,提供了序列化(convert)和反序列化(deConvert)的功能。
|
||||
*/
|
||||
class DateTimeJsonConverter : BaseJsonStringConverter<DateTime> {
|
||||
class DateTimeJsonConverter private constructor() : BaseJsonStringConverter<DateTime> {
|
||||
|
||||
|
||||
companion object {
|
||||
private val dateTimeJsonConverter by lazy {
|
||||
DateTimeJsonConverter()
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getJsonConverter(): DateTimeJsonConverter = dateTimeJsonConverter
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 DateTime 对象转换为 JSON 字符串。
|
||||
|
||||
@@ -16,12 +16,13 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile UUIDJsonConverter.kt
|
||||
* LastUpdate 2026-02-05 11:19:45
|
||||
* LastUpdate 2026-02-08 01:37:00
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.json.converters
|
||||
|
||||
import com.mingliqiye.utils.annotation.UUIDJsonFormat
|
||||
import com.mingliqiye.utils.base.BaseType
|
||||
import com.mingliqiye.utils.json.api.type.JsonTypeReference
|
||||
import com.mingliqiye.utils.json.converters.base.AnnotationGetter
|
||||
@@ -31,14 +32,22 @@ import com.mingliqiye.utils.objects.isNull
|
||||
import com.mingliqiye.utils.string.isNullish
|
||||
import com.mingliqiye.utils.uuid.UUID
|
||||
import com.mingliqiye.utils.uuid.UUIDFormatType
|
||||
import com.mingliqiye.utils.uuid.UUIDJsonFormat
|
||||
|
||||
/**
|
||||
* UUIDJsonConverter 是一个用于处理 UUID 类型与 JSON 字符串之间转换的类。
|
||||
* 它继承自 BaseJsonStringConverter,并实现了 convert 和 deConvert 方法,
|
||||
* 分别用于将 UUID 对象序列化为字符串以及反序列化字符串为 UUID 对象。
|
||||
*/
|
||||
class UUIDJsonConverter : BaseJsonStringConverter<UUID> {
|
||||
class UUIDJsonConverter private constructor() : BaseJsonStringConverter<UUID> {
|
||||
|
||||
companion object {
|
||||
private val uuidJsonConverter by lazy {
|
||||
UUIDJsonConverter()
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getJsonConverter(): UUIDJsonConverter = uuidJsonConverter
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 UUID 对象转换为字符串形式。
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile AnnotationGetter.kt
|
||||
* LastUpdate 2026-02-05 11:12:36
|
||||
* LastUpdate 2026-02-06 16:57:45
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
@@ -58,5 +58,24 @@ interface AnnotationGetter {
|
||||
*/
|
||||
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。
|
||||
*/
|
||||
override fun <T : Annotation> get(clazz: Class<T>): T? =
|
||||
if (annotation.javaClass == clazz) annotation as T else null
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,195 +16,19 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile BaseJsonConverter.kt
|
||||
* LastUpdate 2026-02-05 11:16:12
|
||||
* LastUpdate 2026-02-08 01:22:35
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.json.converters.base
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator
|
||||
import com.fasterxml.jackson.core.JsonParser
|
||||
import com.fasterxml.jackson.core.JsonToken
|
||||
import com.fasterxml.jackson.databind.*
|
||||
import com.fasterxml.jackson.databind.deser.ContextualDeserializer
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule
|
||||
import com.fasterxml.jackson.databind.ser.ContextualSerializer
|
||||
import com.mingliqiye.utils.json.api.type.JsonTypeReference
|
||||
import java.math.BigDecimal
|
||||
import java.math.BigInteger
|
||||
|
||||
/**
|
||||
* BaseJsonConverter 是一个通用的 JSON 转换器接口,用于定义对象之间的双向转换逻辑。
|
||||
* BaseJsonConverter 是一个泛型接口,用于定义 JSON 转换器的基本行为。
|
||||
*
|
||||
* @param F 源类型,表示需要被转换的对象类型。
|
||||
* @param T 目标类型,表示转换后的对象类型。
|
||||
* 该接口继承自 JackSonJsonConverter,并通过泛型参数 F 和 T 定义了转换的源类型和目标类型。
|
||||
* 实现该接口的类需要提供具体的 JSON 转换逻辑。
|
||||
*
|
||||
* @param F 源类型,表示需要被转换的数据类型。
|
||||
* @param T 目标类型,表示转换后的数据类型。
|
||||
*/
|
||||
interface BaseJsonConverter<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()
|
||||
|
||||
/**
|
||||
* 创建并返回一个 Jackson 模块,该模块包含自定义的序列化器和反序列化器。
|
||||
*
|
||||
* @return 配置了自定义序列化器和反序列化器的 SimpleModule 对象。
|
||||
*/
|
||||
fun getJacksonModule(): SimpleModule {
|
||||
// 创建一个 Jackson 模块,名称由源类型和目标类型组成
|
||||
val module = SimpleModule("${getFromClass().name}To${getToClass().name}")
|
||||
|
||||
// 定义自定义序列化器
|
||||
val serializer = object : JsonSerializer<F>(), ContextualSerializer {
|
||||
var property: BeanProperty? = null
|
||||
|
||||
/**
|
||||
* 序列化方法:将源对象转换为目标对象,并根据目标对象的类型写入 JSON。
|
||||
*
|
||||
* @param value 源对象,可能为 null。
|
||||
* @param gen JSON 生成器,用于写入 JSON 数据。
|
||||
* @param provider 序列化提供者,可选参数。
|
||||
*/
|
||||
override fun serialize(
|
||||
value: F?,
|
||||
gen: JsonGenerator,
|
||||
provider: SerializerProvider?
|
||||
) {
|
||||
// 执行转换逻辑
|
||||
val data: T? = 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")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上下文感知方法:设置当前属性信息。
|
||||
*
|
||||
* @param prov 序列化提供者,可选参数。
|
||||
* @param property 当前属性信息。
|
||||
* @return 返回当前序列化器实例。
|
||||
*/
|
||||
override fun createContextual(
|
||||
prov: SerializerProvider?,
|
||||
property: BeanProperty?
|
||||
): JsonSerializer<F> {
|
||||
this.property = property
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
// 定义自定义反序列化器
|
||||
val deserializer = object : JsonDeserializer<F>(), ContextualDeserializer {
|
||||
/**
|
||||
* 反序列化方法:从 JSON 解析数据并转换回源对象。
|
||||
*
|
||||
* @param p JSON 解析器。
|
||||
* @param ctxt 反序列化上下文,可选参数。
|
||||
* @return 转换后的源对象,可能为 null。
|
||||
*/
|
||||
override fun deserialize(
|
||||
p: JsonParser,
|
||||
ctxt: DeserializationContext?
|
||||
): F? {
|
||||
// 处理 null 值情况
|
||||
if (p.currentToken == JsonToken.VALUE_NULL) return null
|
||||
|
||||
// 执行反向转换逻辑
|
||||
return deConvert(p.readValueAs(getToClass()), object : AnnotationGetter {
|
||||
override fun <T : Annotation> get(clazz: Class<T>): T? = property?.getAnnotation(clazz)
|
||||
})
|
||||
}
|
||||
|
||||
var property: BeanProperty? = null
|
||||
|
||||
/**
|
||||
* 上下文感知方法:设置当前属性信息。
|
||||
*
|
||||
* @param ctxt 反序列化上下文,可选参数。
|
||||
* @param property 当前属性信息。
|
||||
* @return 返回当前反序列化器实例。
|
||||
*/
|
||||
override fun createContextual(
|
||||
ctxt: DeserializationContext?,
|
||||
property: BeanProperty?
|
||||
): JsonDeserializer<F> {
|
||||
this.property = property
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
// 将自定义序列化器和反序列化器注册到模块中
|
||||
return module
|
||||
.addSerializer(getFromClass(), serializer)
|
||||
.addDeserializer(getFromClass(), deserializer)
|
||||
}
|
||||
}
|
||||
interface BaseJsonConverter<F, T> : JackSonJsonConverter<F, T>
|
||||
|
||||
@@ -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 JackJsonDeserializer.kt
|
||||
* LastUpdate 2026-02-08 02:22:46
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.json.converters.base
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser
|
||||
import com.fasterxml.jackson.core.JsonToken
|
||||
import com.fasterxml.jackson.databind.BeanProperty
|
||||
import com.fasterxml.jackson.databind.DeserializationContext
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer
|
||||
import com.fasterxml.jackson.databind.deser.ContextualDeserializer
|
||||
|
||||
class JackJsonDeserializer<F, T>(
|
||||
val property: BeanProperty? = null,
|
||||
val jsonConverter: JsonConverter<F, T>
|
||||
) : JsonDeserializer<F>(), ContextualDeserializer {
|
||||
override fun deserialize(
|
||||
p: JsonParser,
|
||||
ctxt: DeserializationContext?
|
||||
): F? {
|
||||
if (p.currentToken == JsonToken.VALUE_NULL) return null
|
||||
return jsonConverter.deConvert(p.readValueAs(jsonConverter.getToClass()), object : AnnotationGetter {
|
||||
override fun <T : Annotation> get(clazz: Class<T>): T? = property?.getAnnotation(clazz)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 上下文感知方法:设置当前属性信息。
|
||||
*
|
||||
* @param ctxt 反序列化上下文,可选参数。
|
||||
* @param property 当前属性信息。
|
||||
* @return 返回当前反序列化器实例。
|
||||
*/
|
||||
override fun createContextual(
|
||||
ctxt: DeserializationContext?,
|
||||
property: BeanProperty?
|
||||
): JackJsonDeserializer<F, T> {
|
||||
return JackJsonDeserializer(property, jsonConverter)
|
||||
}
|
||||
}
|
||||
@@ -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 JackJsonSerializer.kt
|
||||
* LastUpdate 2026-02-08 02:27:52
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.json.converters.base
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator
|
||||
import com.fasterxml.jackson.databind.BeanProperty
|
||||
import com.fasterxml.jackson.databind.JsonSerializer
|
||||
import com.fasterxml.jackson.databind.SerializerProvider
|
||||
import com.fasterxml.jackson.databind.ser.ContextualSerializer
|
||||
import java.math.BigDecimal
|
||||
import java.math.BigInteger
|
||||
|
||||
class JackJsonSerializer<F, T>(
|
||||
val property: BeanProperty?,
|
||||
val jsonConverter: JsonConverter<F, T>
|
||||
) : JsonSerializer<F>(), ContextualSerializer {
|
||||
|
||||
/**
|
||||
* 序列化方法:将源对象转换为目标对象,并根据目标对象的类型写入 JSON。
|
||||
*
|
||||
* @param value 源对象,可能为 null。
|
||||
* @param gen JSON 生成器,用于写入 JSON 数据。
|
||||
* @param provider 序列化提供者,可选参数。
|
||||
*/
|
||||
override fun serialize(
|
||||
value: F?,
|
||||
gen: JsonGenerator,
|
||||
provider: SerializerProvider?
|
||||
) {
|
||||
// 执行转换逻辑
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上下文感知方法:设置当前属性信息。
|
||||
*
|
||||
* @param prov 序列化提供者,可选参数。
|
||||
* @param property 当前属性信息。
|
||||
* @return 返回当前序列化器实例。
|
||||
*/
|
||||
override fun createContextual(
|
||||
prov: SerializerProvider?,
|
||||
property: BeanProperty?
|
||||
): JsonSerializer<F> {
|
||||
return JackJsonSerializer(property, jsonConverter)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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 JackSonJsonConverter.kt
|
||||
* LastUpdate 2026-02-08 02:28:49
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.json.converters.base
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule
|
||||
|
||||
interface JackSonJsonConverter<F, T> : JsonConverter<F, T> {
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* 扩展ObjectMapper,用于注册指定类型的模块。
|
||||
*
|
||||
* 此函数通过反射创建指定类型的实例,并调用其getJacksonModule方法来获取Jackson模块,
|
||||
* 然后将该模块注册到当前ObjectMapper实例中。
|
||||
*
|
||||
* @param T 必须继承自BaseJsonConverter的泛型类型。
|
||||
* @return 返回注册了模块后的ObjectMapper实例。
|
||||
*/
|
||||
inline fun <reified T : BaseJsonConverter<*, *>> ObjectMapper.addJsonConverter(): ObjectMapper =
|
||||
this.registerModule(getJsonConverter<T>().getJacksonModule())
|
||||
|
||||
@JvmStatic
|
||||
fun <T : BaseJsonConverter<*, *>> ObjectMapper.addJsonConverter(clazz: Class<T>): ObjectMapper =
|
||||
this.registerModule(getJsonConverter(clazz).getJacksonModule())
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并返回一个 Jackson 模块,该模块包含自定义的序列化器和反序列化器。
|
||||
*
|
||||
* @return 配置了自定义序列化器和反序列化器的 SimpleModule 对象。
|
||||
*/
|
||||
fun getJacksonModule(): SimpleModule =
|
||||
SimpleModule("${getFromClass().name}To${getToClass().name}")
|
||||
.addSerializer(getFromClass(), JackJsonSerializer(null, this))
|
||||
.addDeserializer(getFromClass(), JackJsonDeserializer(null, this))
|
||||
}
|
||||
@@ -16,44 +16,76 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile JsonConverter.kt
|
||||
* LastUpdate 2026-02-05 11:18:33
|
||||
* LastUpdate 2026-02-07 22:30:18
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
@file:JvmName("JsonConverter")
|
||||
|
||||
package com.mingliqiye.utils.json.converters.base
|
||||
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import java.lang.reflect.ParameterizedType
|
||||
import java.lang.reflect.Type
|
||||
import com.mingliqiye.utils.json.api.type.JsonTypeReference
|
||||
|
||||
/**
|
||||
* 获取给定类型的实际类对象。
|
||||
* BaseJsonConverter 是一个通用的 JSON 转换器接口,用于定义对象之间的双向转换逻辑。
|
||||
*
|
||||
* @param type 类型对象,可以是Class、ParameterizedType或其他Type的实现。
|
||||
* @return 返回与给定类型对应的Class对象;如果无法解析,则返回null。
|
||||
* @param F 源类型,表示需要被转换的对象类型。
|
||||
* @param T 目标类型,表示转换后的对象类型。
|
||||
*/
|
||||
fun getClass(type: Type?): Class<*>? {
|
||||
// 尝试将type直接转换为Class类型,如果失败则检查是否为ParameterizedType
|
||||
val clazz: Class<*>? = type as? Class<*> ?: if (type is ParameterizedType) {
|
||||
// 如果是ParameterizedType,则递归获取其原始类型
|
||||
getClass(type.rawType)
|
||||
} else {
|
||||
// 其他情况返回null
|
||||
null
|
||||
}
|
||||
return clazz
|
||||
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()
|
||||
}
|
||||
|
||||
/**
|
||||
* 扩展ObjectMapper,用于注册指定类型的模块。
|
||||
*
|
||||
* 此函数通过反射创建指定类型的实例,并调用其getJacksonModule方法来获取Jackson模块,
|
||||
* 然后将该模块注册到当前ObjectMapper实例中。
|
||||
*
|
||||
* @param T 必须继承自BaseJsonConverter的泛型类型。
|
||||
* @return 返回注册了模块后的ObjectMapper实例。
|
||||
*/
|
||||
inline fun <reified T : BaseJsonConverter<*, *>> ObjectMapper.registerModule(): ObjectMapper =
|
||||
this.registerModule(T::class.java.newInstance().getJacksonModule())
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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 JsonConverterUtils.kt
|
||||
* LastUpdate 2026-02-07 22:33:09
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
@file:JvmName("JsonConverterUtils")
|
||||
|
||||
package com.mingliqiye.utils.json.converters.base
|
||||
|
||||
|
||||
import com.mingliqiye.utils.logger.MingLiLoggerFactory
|
||||
import java.lang.reflect.ParameterizedType
|
||||
import java.lang.reflect.Type
|
||||
|
||||
val log = MingLiLoggerFactory.getLogger("com.mingliqiye.utils.json.converters.base.JsonConverterUtils")
|
||||
|
||||
/**
|
||||
* 获取给定类型的实际类对象。
|
||||
*
|
||||
* @param type 类型对象,可以是Class、ParameterizedType或其他Type的实现。
|
||||
* @return 返回与给定类型对应的Class对象;如果无法解析,则返回null。
|
||||
*/
|
||||
fun getClass(type: Type?): Class<*>? {
|
||||
// 尝试将type直接转换为Class类型,如果失败则检查是否为ParameterizedType
|
||||
val clazz: Class<*>? = type as? Class<*> ?: if (type is ParameterizedType) {
|
||||
// 如果是ParameterizedType,则递归获取其原始类型
|
||||
getClass(type.rawType)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
return clazz
|
||||
}
|
||||
|
||||
inline fun <reified T : JsonConverter<*, *>> getJsonConverter(): T = getJsonConverter(T::class.java)
|
||||
|
||||
fun <T : JsonConverter<*, *>> getJsonConverter(clazz: Class<T>): T {
|
||||
try {
|
||||
return clazz.getDeclaredMethod(
|
||||
"getJsonConverter"
|
||||
).invoke(null) as T
|
||||
} catch (e: Exception) {
|
||||
log.error("实现于 JsonConverter<*, *> 的类 必须实现静态方法 JsonConverter<*, *> getJsonConverter()", e)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user