@@ -65,34 +65,40 @@ internal class KotlinAnnotationIntrospector(private val context: Module.SetupCon
65
65
// Find a serializer to handle the case where the getter returns an unboxed value from the value class.
66
66
override fun findSerializer (am : Annotated ): StdSerializer <* >? = when (am) {
67
67
is AnnotatedMethod -> {
68
- val getter = am.member.apply {
69
- // If the return value of the getter is a value class,
70
- // it will be serialized properly without doing anything.
71
- if (this .returnType.isUnboxableValueClass()) return null
72
- }
73
-
74
- val kotlinProperty = getter
75
- .declaringClass
76
- .kotlin
77
- .let {
78
- // KotlinReflectionInternalError is raised in GitHub167 test,
79
- // but it looks like an edge case, so it is ignored.
80
- try {
81
- it.memberProperties
82
- } catch (e: Error ) {
83
- null
68
+ when (KotlinVersion .CURRENT .isAtLeast(1 , 5 )) {
69
+ true -> {
70
+ val getter = am.member.apply {
71
+ // If the return value of the getter is a value class,
72
+ // it will be serialized properly without doing anything.
73
+ if (this .returnType.isUnboxableValueClass()) return null
84
74
}
85
- }?.find { it.javaGetter == getter }
86
75
87
- (kotlinProperty?.returnType?.classifier as ? KClass <* >)
88
- ?.takeIf { it.isValue }
89
- ?.java
90
- ?.let { outerClazz ->
91
- val innerClazz = getter.returnType
92
-
93
- ValueClassStaticJsonValueSerializer .createdOrNull(outerClazz, innerClazz)
94
- ? : @Suppress(" UNCHECKED_CAST" ) ValueClassBoxSerializer (outerClazz, innerClazz)
76
+ val kotlinProperty = getter
77
+ .declaringClass
78
+ .kotlin
79
+ .let {
80
+ // KotlinReflectionInternalError is raised in GitHub167 test,
81
+ // but it looks like an edge case, so it is ignored.
82
+ try {
83
+ it.memberProperties
84
+ } catch (e: Error ) {
85
+ null
86
+ }
87
+ }?.find { it.javaGetter == getter }
88
+
89
+ (kotlinProperty?.returnType?.classifier as ? KClass <* >)
90
+ ?.takeIf { it.isValue }
91
+ ?.java
92
+ ?.let { outerClazz ->
93
+ val innerClazz = getter.returnType
94
+
95
+ ValueClassStaticJsonValueSerializer .createdOrNull(outerClazz, innerClazz)
96
+ ? : @Suppress(" UNCHECKED_CAST" ) ValueClassBoxSerializer (outerClazz, innerClazz)
97
+ }
95
98
}
99
+ // Kotlin 1.4 and lower doesn't have value classes and we avoid the NoSuchMethodException on it.isValue
100
+ else -> null
101
+ }
96
102
}
97
103
// Ignore the case of AnnotatedField, because JvmField cannot be set in the field of value class.
98
104
else -> null
0 commit comments