mirror of
https://github.moeyy.xyz/https://github.com/GeWuYou/forgeboot
synced 2025-10-27 23:08:55 +08:00
feat(core-serialization): 添加 Jackson序列化支持
- 新增 SerializerException 异常类用于处理序列化相关错误 - 实现 JacksonValueSerializer 类,提供基于 Jackson 的对象序列化和反序列化功能 - 定义 ValueSerializer 接口,规范序列化和反序列化方法 - 添加 Jackson 相关依赖到 build.gradle.kts 文件 - 创建 .gitattributes 和 .gitignore 文件,配置项目版本控制忽略项
This commit is contained in:
parent
82f2a50f48
commit
92b3eedb38
3
forgeboot-core/forgeboot-core-serialization/.gitattributes
vendored
Normal file
3
forgeboot-core/forgeboot-core-serialization/.gitattributes
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
/gradlew text eol=lf
|
||||
*.bat text eol=crlf
|
||||
*.jar binary
|
||||
40
forgeboot-core/forgeboot-core-serialization/.gitignore
vendored
Normal file
40
forgeboot-core/forgeboot-core-serialization/.gitignore
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
HELP.md
|
||||
.gradle
|
||||
build/
|
||||
!gradle/wrapper/gradle-wrapper.jar
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
bin/
|
||||
!**/src/main/**/bin/
|
||||
!**/src/test/**/bin/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
out/
|
||||
!**/src/main/**/out/
|
||||
!**/src/test/**/out/
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Kotlin ###
|
||||
.kotlin
|
||||
@ -0,0 +1,4 @@
|
||||
|
||||
dependencies {
|
||||
implementation(libs.jackson.databind)
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package com.gewuyou.forgeboot.core.serialization.exception
|
||||
|
||||
/**
|
||||
* 序列化异常类,用于封装与序列化操作相关的异常信息。
|
||||
*
|
||||
* @param message 异常的详细描述信息,默认为 null。
|
||||
* @param cause 导致此异常的底层异常,默认为 null。
|
||||
*
|
||||
* @since 2025-06-17 21:14:40
|
||||
* @author gewuyou
|
||||
*/
|
||||
class SerializerException(
|
||||
message: String? = null,
|
||||
cause: Throwable? = null,
|
||||
) : RuntimeException(
|
||||
message, cause
|
||||
)
|
||||
@ -0,0 +1,26 @@
|
||||
package com.gewuyou.forgeboot.core.serialization.serializer
|
||||
|
||||
/**
|
||||
* 值序列化器接口,用于定义将对象序列化为字符串以及从字符串反序列化为对象的方法。
|
||||
*
|
||||
* @since 2025-06-16 22:16:19
|
||||
* @author gewuyou
|
||||
*/
|
||||
interface ValueSerializer {
|
||||
|
||||
/**
|
||||
* 将给定的对象序列化为字符串。
|
||||
*
|
||||
* @param value 要序列化的对象
|
||||
* @return 序列化后的字符串表示形式
|
||||
*/
|
||||
fun serialize(value: Any): String
|
||||
|
||||
/**
|
||||
* 将给定的字符串反序列化为对象。
|
||||
*
|
||||
* @param value 要反序列化的字符串
|
||||
* @return 反序列化后的对象
|
||||
*/
|
||||
fun <T> deserialize(value: String, type: Class<T>): T
|
||||
}
|
||||
@ -0,0 +1,70 @@
|
||||
package com.gewuyou.forgeboot.core.serialization.serializer.impl.serializer
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.gewuyou.forgeboot.core.serialization.exception.SerializerException
|
||||
import com.gewuyou.forgeboot.core.serialization.serializer.ValueSerializer
|
||||
|
||||
|
||||
/**
|
||||
* Jackson 值序列化器,用于将对象序列化为 JSON 字符串或将 JSON 字符串反序列化为对象。
|
||||
*
|
||||
* @param mapper 用于执行序列化和反序列化的 Jackson ObjectMapper 实例
|
||||
* @since 2025-06-18 12:37:59
|
||||
* @author gewuyou
|
||||
*/
|
||||
class JacksonValueSerializer(
|
||||
private val mapper: ObjectMapper,
|
||||
) : ValueSerializer {
|
||||
/**
|
||||
* 将给定的对象序列化为字符串。
|
||||
*
|
||||
* @param value 要序列化的对象
|
||||
* @return 序列化后的字符串表示形式
|
||||
*/
|
||||
override fun serialize(value: Any): String {
|
||||
// 对于基本类型或者 String,直接返回
|
||||
if (value is String || value.javaClass.isPrimitive) {
|
||||
return value.toString()
|
||||
}
|
||||
return try {
|
||||
mapper.writeValueAsString(value)
|
||||
} catch (e: JsonProcessingException) {
|
||||
throw SerializerException("Failed to serialize value: ${e.message}", e)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将给定的字符串反序列化为对象。
|
||||
*
|
||||
* @param value 要反序列化的字符串
|
||||
* @return 反序列化后的对象
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T> deserialize(value: String, type: Class<T>): T {
|
||||
// 对于 String 类型,直接返回
|
||||
if (type == String::class.java) {
|
||||
return value as T
|
||||
}
|
||||
// 对于基本类型,使用相应的 valueOf 方法进行转换
|
||||
if (type.isPrimitive) {
|
||||
return when (type) {
|
||||
Boolean::class.java -> value.toBoolean() as T
|
||||
Integer::class.java, Int::class.java -> value.toInt() as T
|
||||
Long::class.java, java.lang.Long::class.java -> value.toLong() as T
|
||||
Double::class.java, java.lang.Double::class.java -> value.toDouble() as T
|
||||
Float::class.java, java.lang.Float::class.java -> value.toFloat() as T
|
||||
Short::class.java, java.lang.Short::class.java -> value.toShort() as T
|
||||
Byte::class.java, java.lang.Byte::class.java -> value.toByte() as T
|
||||
Character::class.java -> value[0] as T // 取第一个字符作为字符
|
||||
else -> throw SerializerException("Unsupported primitive type: ${type.name}")
|
||||
}
|
||||
}
|
||||
return try {
|
||||
mapper.readValue(value, type)
|
||||
} catch (e: JsonProcessingException) {
|
||||
throw SerializerException("Failed to deserialize value: ${e.message}", e)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user