add ValueClassBoxSerializer
MODIFY
src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinSerializers.kt
src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinSerializers.kt
@@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.ser.Serializers
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import java.math.BigInteger
+import kotlin.reflect.KClass
object SequenceSerializer : StdSerializer<Sequence<*>>(Sequence::class.java) {
override fun serialize(value: Sequence<*>, gen: JsonGenerator, provider: SerializerProvider) {
@@ -71,3 +72,20 @@ internal class KotlinSerializers : Serializers.Base() {
else -> null
}
}
+
+// This serializer is used to properly serialize the value class.
+// The getter generated for the value class is special,
+// so this class will not work properly when added to the Serializers
+// (it is configured from KotlinAnnotationIntrospector.findSerializer).
+internal class ValueClassBoxSerializer(
+ outerClazz: KClass<Any>, innerClazz: Class<*>
+) : StdSerializer<Any>(outerClazz.java) {
+ private val boxMethod = _handledType.getMethod("box-impl", innerClazz)
+
+ override fun serialize(value: Any, gen: JsonGenerator, provider: SerializerProvider) {
+ // Values retrieved from getter are considered validated and constructor-impl is not executed.
+ val boxed = boxMethod.invoke(null, value)
+
+ provider.findValueSerializer(_handledType).serialize(boxed, gen, provider)
+ }
+}

add ValueClassBoxSerializer