add serialization settings
MODIFY
src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinSerializers.kt
src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinSerializers.kt
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.*
import com.fasterxml.jackson.databind.ser.Serializers
import com.fasterxml.jackson.databind.ser.std.StdSerializer
+import com.fasterxml.jackson.module.kotlin.ValueClassUnboxSerializer.isUnboxableValueClass
import java.math.BigInteger
object SequenceSerializer : StdSerializer<Sequence<*>>(Sequence::class.java) {
@@ -40,6 +41,25 @@ object ULongSerializer : StdSerializer<ULong>(ULong::class.java) {
}
}
+object ValueClassUnboxSerializer : StdSerializer<Any>(Any::class.java) {
+ override fun serialize(value: Any, gen: JsonGenerator, provider: SerializerProvider) {
+ val unboxed = value::class.java.getMethod("unbox-impl").invoke(value)
+
+ if (unboxed == null) {
+ gen.writeNull()
+ return
+ }
+
+ provider.findValueSerializer(unboxed::class.java).serialize(unboxed, gen, provider)
+ }
+
+ // In the future, value class without JvmInline will be available, and unbox may not be able to handle it.
+ // https://github.com/FasterXML/jackson-module-kotlin/issues/464
+ // The JvmInline annotation can be given to Java class,
+ // so the isKotlinClass decision is necessary (the order is preferable in terms of possible frequency).
+ fun Class<*>.isUnboxableValueClass() = annotations.any { it is JvmInline } && this.isKotlinClass()
+}
+
@Suppress("EXPERIMENTAL_API_USAGE")
internal class KotlinSerializers : Serializers.Base() {
override fun findSerializer(
@@ -52,6 +72,8 @@ internal class KotlinSerializers : Serializers.Base() {
UShort::class.java.isAssignableFrom(type.rawClass) -> UShortSerializer
UInt::class.java.isAssignableFrom(type.rawClass) -> UIntSerializer
ULong::class.java.isAssignableFrom(type.rawClass) -> ULongSerializer
+ // The priority of Unboxing needs to be lowered so as not to break the serialization of Unsigned Integers.
+ type.rawClass.isUnboxableValueClass() -> ValueClassUnboxSerializer
else -> null
}
}

add serialization settings