com.fasterxml.jackson.module.kotlin.KotlinKeySerializers.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jackson-module-kotlin Show documentation
Show all versions of jackson-module-kotlin Show documentation
Add-on module for Jackson (https://github.com/FasterXML/jackson/) to support
Kotlin language, specifically introspection of method/constructor parameter names,
without having to add explicit property name annotation.
package com.fasterxml.jackson.module.kotlin
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.BeanDescription
import com.fasterxml.jackson.databind.JavaType
import com.fasterxml.jackson.databind.JsonSerializer
import com.fasterxml.jackson.databind.SerializationConfig
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.ser.Serializers
import com.fasterxml.jackson.databind.ser.std.StdSerializer
internal object ValueClassUnboxKeySerializer : StdSerializer(Any::class.java) {
override fun serialize(value: Any, gen: JsonGenerator, provider: SerializerProvider) {
val method = value::class.java.getMethod("unbox-impl")
val unboxed = method.invoke(value)
if (unboxed == null) {
val javaType = provider.typeFactory.constructType(method.genericReturnType)
provider.findNullKeySerializer(javaType, null).serialize(null, gen, provider)
return
}
provider.findKeySerializer(unboxed::class.java, null).serialize(unboxed, gen, provider)
}
}
internal class KotlinKeySerializers : Serializers.Base() {
override fun findSerializer(
config: SerializationConfig,
type: JavaType,
beanDesc: BeanDescription
): JsonSerializer<*>? = when {
type.rawClass.isUnboxableValueClass() -> ValueClassUnboxKeySerializer
else -> null
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy