全部代码:
1. 添加排除注解
在一些data class
中,有些property
不需要被序列化,我们可通过为期添加注解;然后在获取KProperty
时,排除掉即可;
a. 添加注解类
@Target(AnnotationTarget.PROPERTY)
@Retention(AnnotationRetention.RUNTIME)
annotation class JsonExclude
b. 在serialize
方法添加过滤如下:
private inline fun StringBuilder.serializeObj(o: Any) {
// ==== 1. 获取类的所有`KProperty`
o.javaClass.kotlin.memberProperties.filter {
it.findAnnotation<JsonExclude>() == null }.
....
}
c. 测试代码
data class UserBean(@JsonExclude val username: String, val age: Int)
val user = UserBean("better", 30)
println(serialize(user)) // {"age":30}
2. 添加属性名替换注解
a. 新增注解类:
/**
* 替换名字
*/
@Target(AnnotationTarget.PROPERTY)
@Retention(AnnotationRetention.RUNTIME)
annotation class JsonName(val name: String)
b. 修改 serializeProperty
方法如下:
private inline fun StringBuilder.serializeProperty(property: KProperty1<Any,
*>, receiver: Any) {
// check Annotation JsonName
val jsonName = property.findAnnotation<JsonName>()?.name ?: property.name
serializeString(jsonName)
...
}
c. 测试一下
// change json key
data class UserBean(@JsonExclude val username: String, @JsonName("myAge") val age: Int)
3. 添加自定义解析器
比如:对于时间戳,序列化时,我们将其转换成 yyyy-MM-dd
;
a. 添加转换器与转换注解
interface ValueSerializer<in T> {
fun toJson(value: T?): Any? // 将值转换成某个具体类型
}
// ValueSerializer 协变,用来 表示 ValueSerializer 的子类
annotation class CustomSerializer(val kClazz: KClass<out ValueSerializer<*>>)
b. 修改 serializeProperty
方法,判断是否有 CustomSerializer
注解
private inline fun StringBuilder.serializeProperty(property: KProperty1<Any, *>, receiver: Any) {
...
// 获取属性值
var value = property.invoke(receiver)
// has custom serialize 判断是否有 CustomSerializer 注解
val customSerializeClass = property.findAnnotation<CustomSerializer>()?.kClazz
if(value != null && customSerializeClass != null) {
// 创建KClass实例
val valueSerializer = customSerializeClass.objectInstance ?: customSerializeClass.createInstance()
// * 需要强制转一下
value =(valueSerializer as ValueSerializer<Any?>).toJson(value)
}
serializePropertyValue(value)
}
再利用扩展函数优化一下:
private inline fun StringBuilder.serializeProperty(property: KProperty1<Any, *>, receiver: Any) {
...
val value = property.invoke(receiver)
val jsonValue = property.getCustomSerializer(property)?.toJson(value) ?: value
serializePropertyValue(jsonValue)
}
// 新增扩展函数
private inline fun KProperty<*>.getCustomSerializer(property:
KProperty1<Any, *>): ValueSerializer<Any?>? {
// has custom serialize
val customSerializeClass = property.findAnnotation<CustomSerializer>()?.kClazz
if (customSerializeClass != null) {
// 创建KClass 示例
val valueSerializer = customSerializeClass.objectInstance ?:
customSerializeClass.createInstance()
// * 需要强制转一下
return (valueSerializer as ValueSerializer<Any?>?)
}
return null
}
c. 测试代码
data class MyTime(@CustomSerializer(DateSerializer::class) val birthday: Long?, val name: String)
// 具体类
object DateSerializer : ValueSerializer<Long> {
private val dmt = SimpleDateFormat("yyyy-MM-dd")
override fun toJson(value: Long?): Any? = if (value == null) null else dmt.format(Date())
}
// 测试
val test = MyTime(Calendar.getInstance().timeInMillis, "better")
println(serialize(test))