@CustomSerializer associates a property with a value serializer when default JKid behavior would represent a nonprimitive value as a nested object. The serializer converts between the Kotlin value and its JSON representation. section 12.1.7
Applying a date serializer
data class Person( val name: String, @CustomSerializer(DateSerializer::class) val birthDate: Date ) DateSerializer implements ValueSerializer<Date>, providing toJsonValue and fromJsonValue. The annotation selects it only for birthDate; other properties retain normal serialization. section 12.1.7
The annotation accepts a class reference constrained to ValueSerializer implementations. KClass<out ValueSerializer<*>> permits serializers for different property types while rejecting invalid arguments such as Date::class. section 12.1.7
Reflection discovers the annotation on each KProperty. The serializer class is instantiated through objectInstance or createInstance, then applied to the property value before ordinary JSON serialization; without an annotation, the original value is used. section 12.2.3
interface ValueSerializer<T> {
fun toJsonValue(value: T): Any?
fun fromJsonValue(jsonValue: Any?): T
}
ValueSerializer defines the bidirectional conversion contract used by @CustomSerializer. section 12.1.7
annotation class CustomSerializer( val serializerClass: KClass<out ValueSerializer<*>> )
The annotation declaration shows how the generic serializer constraint is expressed as a Kotlin class reference. section 12.1.7
