Skip to content

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fasterxml.jackson.module.kotlin
2+
3+
import com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer
4+
import com.fasterxml.jackson.databind.util.StdConverter
5+
import kotlin.reflect.KClass
6+
7+
// S is nullable because value corresponds to a nullable value class
8+
// @see KotlinNamesAnnotationIntrospector.findNullSerializer
9+
internal class ValueClassBoxConverter<S : Any?, D : Any>(
10+
unboxedClass: Class<S>,
11+
valueClass: KClass<D>
12+
) : StdConverter<S, D>() {
13+
private val boxMethod = valueClass.java.getDeclaredMethod("box-impl", unboxedClass).apply {
14+
if (!this.isAccessible) this.isAccessible = true
15+
}
16+
17+
@Suppress("UNCHECKED_CAST")
18+
override fun convert(value: S): D = boxMethod.invoke(null, value) as D
19+
20+
val delegatingSerializer: StdDelegatingSerializer by lazy { StdDelegatingSerializer(this) }
21+
}

0 commit comments

Comments
 (0)