refactor(time): 重构 DateTime 类并添加新功能
- 从 Java 文件中删除了旧的 DateTime 类 - 新增 Kotlin 版本的 DateTime 类,具有以下改进: - 添加了时间格式化枚举类 Formatter - 新增时间位移类 DateTimeOffset - 重构了 parse 和 format 方法,支持新的 Formatter 枚举 - 优化了文件时间转换方法,使用纳秒精度 - 删除了旧的 DateTimeJsonConverter 类
This commit is contained in:
@@ -0,0 +1,393 @@
|
||||
/*
|
||||
* Copyright 2025 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 JsonApi.kt
|
||||
* LastUpdate 2025-09-15 11:10:59
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.json
|
||||
|
||||
import com.mingliqiye.utils.json.converters.JsonConverter
|
||||
import com.mingliqiye.utils.json.converters.JsonStringConverter
|
||||
import java.io.*
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
|
||||
|
||||
/**
|
||||
* JSON处理接口,提供JSON字符串与Java对象之间的相互转换功能
|
||||
*/
|
||||
interface JsonApi {
|
||||
/**
|
||||
* 将JSON字符串解析为指定类型的对象
|
||||
*
|
||||
* @param json 待解析的JSON字符串
|
||||
* @param clazz 目标对象的Class类型
|
||||
* @param <T> 泛型参数,表示目标对象的类型
|
||||
* @return 解析后的对象实例
|
||||
</T> */
|
||||
fun <T> parse(json: String, clazz: Class<T>): T
|
||||
|
||||
/**
|
||||
* 将JSON字符串解析为指定泛型类型对象
|
||||
*
|
||||
* @param json 待解析的JSON字符串
|
||||
* @param type 目标对象的Type类型(支持泛型)
|
||||
* @param <T> 泛型参数,表示目标对象的类型
|
||||
* @return 解析后的对象实例
|
||||
</T> */
|
||||
fun <T> parse(json: String, type: JsonTypeReference<T>): T
|
||||
|
||||
/**
|
||||
* 将对象格式化为JSON字符串
|
||||
*
|
||||
* @param obj 待格式化的对象
|
||||
* @return 格式化后的JSON字符串
|
||||
*/
|
||||
fun format(obj: Any): String
|
||||
|
||||
fun formatUnicode(obj: Any): String
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun <T> parseFrom(path: String, clazz: Class<T>): T {
|
||||
return parseFrom(Paths.get(path), clazz)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun <T> parseFrom(path: Path, clazz: Class<T>): T {
|
||||
return parseFrom(path.toFile(), clazz)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun <T> parseFrom(file: File, clazz: Class<T>): T {
|
||||
Files.newInputStream(file.toPath()).use { inputStream ->
|
||||
return parseFrom(inputStream, clazz)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun <T> parseFrom(inputStream: InputStream, clazz: Class<T>): T {
|
||||
val bytes = ByteArray(1024)
|
||||
ByteArrayOutputStream().use { bos ->
|
||||
var readlength: Int
|
||||
while ((inputStream.read(bytes).also { readlength = it }) != -1) {
|
||||
bos.write(bytes, 0, readlength)
|
||||
}
|
||||
return parse(bos.toByteArray(), clazz)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun <T> parseFrom(path: String, type: JsonTypeReference<T>): T {
|
||||
return parseFrom(Paths.get(path), type)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun <T> parseFrom(path: Path, type: JsonTypeReference<T>): T {
|
||||
return parseFrom(path.toFile(), type)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun <T> parseFrom(file: File, type: JsonTypeReference<T>): T {
|
||||
Files.newInputStream(file.toPath()).use { inputStream ->
|
||||
return parseFrom<T>(inputStream, type)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun <T> parseFrom(inputStream: InputStream, type: JsonTypeReference<T>): T {
|
||||
val bytes = ByteArray(1024)
|
||||
ByteArrayOutputStream().use { bos ->
|
||||
var readlength: Int
|
||||
while ((inputStream.read(bytes).also { readlength = it }) != -1) {
|
||||
bos.write(bytes, 0, readlength)
|
||||
}
|
||||
return parse(bos.toByteArray(), type)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字节数组形式的JSON解析为指定类型的对象
|
||||
*
|
||||
* @param json 待解析的JSON字节数组
|
||||
* @param clazz 目标对象的Class类型
|
||||
* @param <T> 泛型参数,表示目标对象的类型
|
||||
* @return 解析后的对象实例
|
||||
</T> */
|
||||
fun <T> parse(json: ByteArray, clazz: Class<T>): T {
|
||||
return parse(String(json), clazz)
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字节数组形式的JSON解析为指定泛型类型对象
|
||||
*
|
||||
* @param json 待解析的JSON字节数组
|
||||
* @param type 目标对象的Type类型(支持泛型)
|
||||
* @param <T> 泛型参数,表示目标对象的类型
|
||||
* @return 解析后的对象实例
|
||||
</T> */
|
||||
fun <T> parse(json: ByteArray, type: JsonTypeReference<T>): T {
|
||||
return parse(String(json), type)
|
||||
}
|
||||
|
||||
/**
|
||||
* 将JSON字符串解析为指定类型的对象,解析失败时返回默认值
|
||||
*
|
||||
* @param json 待解析的JSON字符串
|
||||
* @param clazz 目标对象的Class类型
|
||||
* @param defaultValue 解析失败时返回的默认值
|
||||
* @param <T> 泛型参数,表示目标对象的类型
|
||||
* @return 解析后的对象实例或默认值
|
||||
</T> */
|
||||
fun <T> parse(json: String, clazz: Class<T>, defaultValue: T): T {
|
||||
try {
|
||||
return parse(json, clazz)
|
||||
} catch (e: Exception) {
|
||||
return defaultValue
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将JSON字符串解析为指定泛型类型对象,解析失败时返回默认值
|
||||
*
|
||||
* @param json 待解析的JSON字符串
|
||||
* @param type 目标对象的Type类型(支持泛型)
|
||||
* @param defaultValue 解析失败时返回的默认值
|
||||
* @param <T> 泛型参数,表示目标对象的类型
|
||||
* @return 解析后的对象实例或默认值
|
||||
**/
|
||||
fun <T> parse(
|
||||
json: String,
|
||||
type: JsonTypeReference<T>,
|
||||
defaultValue: T
|
||||
): T {
|
||||
try {
|
||||
return parse<T>(json, type)
|
||||
} catch (e: Exception) {
|
||||
return defaultValue
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象格式化为美化格式的JSON字符串(带缩进和换行)
|
||||
*
|
||||
* @param object 待格式化的对象
|
||||
* @return 格式化后的美化JSON字符串
|
||||
*/
|
||||
fun formatPretty(obj: Any): String
|
||||
|
||||
fun formatPrettyBytes(obj: Any): ByteArray {
|
||||
return formatPretty(obj)!!.toByteArray()
|
||||
}
|
||||
|
||||
fun formatPrettyUnicode(obj: Any): String
|
||||
|
||||
fun formatPrettyUnicodeBytes(obj: Any): ByteArray {
|
||||
return formatPrettyUnicode(obj)!!.toByteArray()
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun formatPretty(obj: Any, file: String) {
|
||||
formatPretty(obj, Paths.get(file))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun formatPretty(obj: Any, file: Path) {
|
||||
formatPretty(obj, file.toFile())
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun formatPretty(obj: Any, file: File) {
|
||||
FileOutputStream(file).use { fos ->
|
||||
formatPretty(obj, fos)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun formatPretty(obj: Any, stream: OutputStream) {
|
||||
stream.write(formatPrettyBytes(obj))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun formatPrettyUnicode(obj: Any, file: String) {
|
||||
formatPrettyUnicode(obj, Paths.get(file))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun formatPrettyUnicode(obj: Any, file: Path) {
|
||||
formatPrettyUnicode(obj, file.toFile())
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun formatPrettyUnicode(obj: Any, file: File) {
|
||||
FileOutputStream(file).use { fos ->
|
||||
formatPrettyUnicode(obj, fos)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun formatPrettyUnicode(obj: Any, stream: OutputStream) {
|
||||
stream.write(formatPrettyUnicodeBytes(obj))
|
||||
}
|
||||
|
||||
fun formatBytes(obj: Any): ByteArray {
|
||||
return format(obj)!!.toByteArray()
|
||||
}
|
||||
|
||||
fun formatUnicodeBytes(obj: Any): ByteArray {
|
||||
return formatUnicode(obj)!!.toByteArray()
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun format(obj: Any, file: String) {
|
||||
format(obj, Paths.get(file))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun format(obj: Any, file: Path) {
|
||||
format(obj, file.toFile())
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun format(obj: Any, file: File) {
|
||||
FileOutputStream(file).use { fos ->
|
||||
format(obj, fos)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun format(obj: Any, stream: OutputStream) {
|
||||
stream.write(formatPrettyBytes(obj))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun formatUnicode(obj: Any, file: String) {
|
||||
formatUnicode(obj, Paths.get(file))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun formatUnicode(obj: Any, file: Path) {
|
||||
formatUnicode(obj, file.toFile())
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun formatUnicode(obj: Any, file: File) {
|
||||
FileOutputStream(file).use { fos ->
|
||||
formatUnicode(obj, fos)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun formatUnicode(obj: Any, stream: OutputStream) {
|
||||
stream.write(formatPrettyUnicodeBytes(obj))
|
||||
}
|
||||
|
||||
/**
|
||||
* 将JSON字符串解析为指定元素类型的List集合
|
||||
*
|
||||
* @param json 待解析的JSON字符串
|
||||
* @param elementType List中元素的类型
|
||||
* @param <T> 泛型参数,表示List中元素的类型
|
||||
* @return 解析后的List集合
|
||||
</T> */
|
||||
fun <T> parseList(json: String, elementType: Class<T>): MutableList<T> {
|
||||
return parse<MutableList<T>>(json, JsonTypeUtils.listType<T>(elementType))
|
||||
}
|
||||
|
||||
/**
|
||||
* 将JSON字符串解析为指定键值类型的Map集合
|
||||
*
|
||||
* @param json 待解析的JSON字符串
|
||||
* @param keyType Map中键的类型
|
||||
* @param valueType Map中值的类型
|
||||
* @param <K> 泛型参数,表示Map中键的类型
|
||||
* @param <V> 泛型参数,表示Map中值的类型
|
||||
* @return 解析后的Map集合
|
||||
</V></K> */
|
||||
fun <K, V> parseMap(
|
||||
json: String,
|
||||
keyType: Class<K>,
|
||||
valueType: Class<V>
|
||||
): MutableMap<K, V> {
|
||||
val mapType: JsonTypeReference<MutableMap<K, V>> = object : JsonTypeReference<MutableMap<K, V>>() {}
|
||||
return parse<MutableMap<K, V>>(json, mapType)
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证字符串是否为有效的JSON格式
|
||||
*
|
||||
* @param json 待验证的字符串
|
||||
* @return 如果是有效的JSON格式返回true,否则返回false
|
||||
*/
|
||||
fun isValidJson(json: String): Boolean
|
||||
|
||||
/**
|
||||
* 将对象转换为JSON字节数组
|
||||
*
|
||||
* @param object 待转换的对象
|
||||
* @return 转换后的JSON字节数组
|
||||
*/
|
||||
fun toBytes(obj: Any): ByteArray {
|
||||
return format(obj)!!.toByteArray()
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象转换为美化格式的JSON字节数组
|
||||
*
|
||||
* @param object 待转换的对象
|
||||
* @return 转换后的美化格式JSON字节数组
|
||||
*/
|
||||
fun toBytesPretty(obj: Any): ByteArray {
|
||||
return formatPretty(obj)!!.toByteArray()
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并多个JSON字符串为一个JSON对象
|
||||
*
|
||||
* @param jsons 待合并的JSON字符串数组
|
||||
* @return 合并后的JSON字符串
|
||||
*/
|
||||
fun merge(vararg jsons: String): String
|
||||
|
||||
/**
|
||||
* 获取JSON字符串中指定路径节点的值
|
||||
*
|
||||
* @param json JSON字符串
|
||||
* @param path 节点路径(如:"user.name")
|
||||
* @return 节点值的字符串表示
|
||||
*/
|
||||
fun getNodeValue(json: String, path: String): String
|
||||
|
||||
/**
|
||||
* 更新JSON字符串中指定路径节点的值
|
||||
*
|
||||
* @param json 原始JSON字符串
|
||||
* @param path 节点路径(如:"user.name")
|
||||
* @param newValue 新的节点值
|
||||
* @return 更新后的JSON字符串
|
||||
*/
|
||||
fun updateNodeValue(json: String, path: String, newValue: Any): String
|
||||
|
||||
fun <T, D> convert(source: T, destinationClass: Class<D>): D
|
||||
|
||||
fun <T, D> convert(source: T, destinationType: JsonTypeReference<D>): D
|
||||
|
||||
fun addJsonConverter(c: JsonConverter<*, *>)
|
||||
fun addJsonStringConverter(c: JsonStringConverter<*>)
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2025 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 2025-09-15 11:12:07
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.json.converters
|
||||
|
||||
interface JsonConverter<F, T> {
|
||||
fun convert(obj: F?): T?
|
||||
fun deConvert(obj: T?): F?
|
||||
val tClass: Class<F>
|
||||
|
||||
fun getStringConverter(): JsonStringConverter<F>? {
|
||||
if (this is JsonStringConverter<*>) {
|
||||
return this as JsonStringConverter<F>
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
/*
|
||||
* Copyright 2025 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 JsonStringConverter.kt
|
||||
* LastUpdate 2025-09-15 11:03:53
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.json.converters
|
||||
|
||||
import com.alibaba.fastjson2.JSONReader
|
||||
import com.alibaba.fastjson2.JSONWriter
|
||||
import com.alibaba.fastjson2.reader.ObjectReader
|
||||
import com.alibaba.fastjson2.writer.ObjectWriter
|
||||
import com.fasterxml.jackson.core.JsonGenerator
|
||||
import com.fasterxml.jackson.core.JsonParser
|
||||
import com.fasterxml.jackson.databind.*
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import com.mingliqiye.utils.time.DateTime
|
||||
import com.mingliqiye.utils.time.DateTime.Companion.parse
|
||||
import com.mingliqiye.utils.time.Formatter
|
||||
import com.mingliqiye.utils.uuid.UUID
|
||||
import com.mingliqiye.utils.uuid.UUID.Companion.of
|
||||
import java.io.IOException
|
||||
import java.lang.reflect.Type
|
||||
|
||||
/**
|
||||
* JSON转换器接口,提供对象与字符串之间的相互转换功能,并支持多种JSON库
|
||||
*
|
||||
* @param <T> 需要转换的对象类型
|
||||
</T> */
|
||||
abstract class JsonStringConverter<T> : JsonConverter<T, String> {
|
||||
val fastjsonJsonStringConverterAdapter: FastjsonJsonStringConverterAdapter<JsonStringConverter<T>, T>
|
||||
/**
|
||||
* 获取 Fastjson 的适配器
|
||||
* @return 适配器实例
|
||||
*/
|
||||
get() = FastjsonJsonStringConverterAdapter.of(this)
|
||||
|
||||
val gsonJsonStringConverterAdapter: GsonJsonStringConverterAdapter<JsonStringConverter<T>, T>
|
||||
/**
|
||||
* 获取 Gson 的适配器
|
||||
* @return 适配器实例
|
||||
*/
|
||||
get() = GsonJsonStringConverterAdapter.of(this)
|
||||
|
||||
val jacksonJsonStringConverterAdapter: JacksonJsonStringConverterAdapter<JsonStringConverter<T>, T>
|
||||
/**
|
||||
* 获取 Jackson 的适配器
|
||||
* @return 适配器实例
|
||||
*/
|
||||
get() = JacksonJsonStringConverterAdapter.of(this)
|
||||
}
|
||||
|
||||
|
||||
class JacksonJsonStringConverterAdapter<T : JsonStringConverter<TT>, TT
|
||||
> private constructor(val jsonStringConverter: T) {
|
||||
|
||||
val jacksonJsonDeserializer: JsonDeserializer<TT?>
|
||||
/**
|
||||
* 获取Jackson反序列化器
|
||||
*
|
||||
* @return Jackson的JsonDeserializer实例
|
||||
*/
|
||||
get() = object : JsonDeserializer<TT?>() {
|
||||
@Throws(IOException::class)
|
||||
override fun deserialize(p: JsonParser, ctxt: DeserializationContext?): TT? {
|
||||
if (p.isNaN) return null
|
||||
return jsonStringConverter.deConvert(p.valueAsString)
|
||||
}
|
||||
}
|
||||
|
||||
val jacksonJsonSerializer: JsonSerializer<TT?>
|
||||
/**
|
||||
* 获取Jackson序列化器
|
||||
*
|
||||
* @return Jackson的JsonSerializer实例
|
||||
*/
|
||||
get() = object : JsonSerializer<TT?>() {
|
||||
@Throws(IOException::class)
|
||||
override fun serialize(
|
||||
value: TT?,
|
||||
gen: JsonGenerator,
|
||||
serializers: SerializerProvider?
|
||||
) {
|
||||
if (value == null) {
|
||||
gen.writeNull()
|
||||
return
|
||||
}
|
||||
gen.writeString(jsonStringConverter.convert(value))
|
||||
}
|
||||
}
|
||||
|
||||
val jacksonModule: Module
|
||||
/**
|
||||
*
|
||||
* 获取 Jackson 的格式化模块
|
||||
*
|
||||
* @return 格式化模块
|
||||
*/
|
||||
get() {
|
||||
val tClass = jsonStringConverter.tClass
|
||||
val m = SimpleModule(tClass.getSimpleName())
|
||||
m.addSerializer<TT?>(tClass, this.jacksonJsonSerializer)
|
||||
m.addDeserializer<TT?>(tClass, this.jacksonJsonDeserializer)
|
||||
return m
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
*
|
||||
* @param t JSON转换器实例
|
||||
* @return JSON转换器的适配器
|
||||
* @param <T> JSON转换器
|
||||
* @param <TT> JSON转换器的泛型
|
||||
**/
|
||||
fun <T : JsonStringConverter<TT>, TT
|
||||
> of(t: T): JacksonJsonStringConverterAdapter<T, TT> {
|
||||
return JacksonJsonStringConverterAdapter(t)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GsonJsonStringConverterAdapter<T : JsonStringConverter<TT>, TT
|
||||
>(val jsonStringConverter: T) {
|
||||
|
||||
val gsonTypeAdapter: TypeAdapter<TT?>
|
||||
/**
|
||||
* 获取Gson类型适配器
|
||||
*
|
||||
* @return Gson的TypeAdapter实例
|
||||
*/
|
||||
get() = object : TypeAdapter<TT?>() {
|
||||
@Throws(IOException::class)
|
||||
override fun write(out: JsonWriter, value: TT?) {
|
||||
if (value == null) {
|
||||
out.nullValue()
|
||||
return
|
||||
}
|
||||
out.value(jsonStringConverter.convert(value))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun read(`in`: JsonReader): TT? {
|
||||
val value = `in`.nextString()
|
||||
return jsonStringConverter.deConvert(value)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun <T : JsonStringConverter<TT>, TT
|
||||
> of(t: T): GsonJsonStringConverterAdapter<T, TT> {
|
||||
return GsonJsonStringConverterAdapter(t)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class FastjsonJsonStringConverterAdapter<T : JsonConverter<TT, String>, TT
|
||||
>(val jsonStringConverter: T) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val fastJsonObjectWriter: ObjectWriter<T>
|
||||
/**
|
||||
* 获取FastJson对象写入器
|
||||
*
|
||||
* @return FastJson的ObjectWriter实例
|
||||
*/
|
||||
get() = ObjectWriter { writer: JSONWriter?, obj: Any?, _: Any?, _: Type?, _: Long
|
||||
->
|
||||
// 如果对象为null则写入null
|
||||
if (obj == null) {
|
||||
writer!!.writeNull()
|
||||
return@ObjectWriter
|
||||
}
|
||||
writer!!.writeString(jsonStringConverter.convert(obj as TT))
|
||||
}
|
||||
|
||||
val fastJsonObjectReader: ObjectReader<TT>
|
||||
/**
|
||||
* 获取FastJson对象读取器
|
||||
*
|
||||
* @return FastJson的ObjectReader实例
|
||||
*/
|
||||
get() = ObjectReader { reader: JSONReader?, _: Type?, _: Any?, _: Long
|
||||
->
|
||||
val value = reader!!.readString()
|
||||
jsonStringConverter.deConvert(value)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun <T : JsonConverter<TT, String>, TT
|
||||
> of(t: T): FastjsonJsonStringConverterAdapter<T, TT> {
|
||||
return FastjsonJsonStringConverterAdapter(t)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class DateTimeJsonConverter : JsonStringConverter<DateTime>() {
|
||||
override val tClass = DateTime::class.java
|
||||
|
||||
override fun convert(obj: DateTime?): String? {
|
||||
if (obj == null) {
|
||||
return null
|
||||
}
|
||||
return obj.format(Formatter.STANDARD_DATETIME)
|
||||
}
|
||||
|
||||
override fun deConvert(obj: String?): DateTime? {
|
||||
if (obj == null) {
|
||||
return null
|
||||
}
|
||||
return parse(
|
||||
obj,
|
||||
Formatter.STANDARD_DATETIME_MILLISECOUND7,
|
||||
true
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
class UUIDJsonStringConverter : JsonStringConverter<UUID>() {
|
||||
override val tClass: Class<UUID> = UUID::class.java
|
||||
|
||||
override fun convert(obj: UUID?): String? {
|
||||
if (obj == null) {
|
||||
return null
|
||||
}
|
||||
return obj.getString()
|
||||
}
|
||||
|
||||
override fun deConvert(obj: String?): UUID? {
|
||||
if (obj == null) {
|
||||
return null
|
||||
}
|
||||
return of(obj)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user