File tree 3 files changed +23
-10
lines changed
src/main/kotlin/com/fasterxml/jackson/module/kotlin
3 files changed +23
-10
lines changed Original file line number Diff line number Diff line change 1
1
package com.fasterxml.jackson.module.kotlin
2
2
3
+ import com.fasterxml.jackson.databind.JavaType
3
4
import com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer
5
+ import com.fasterxml.jackson.databind.type.TypeFactory
4
6
import com.fasterxml.jackson.databind.util.StdConverter
5
7
import kotlin.reflect.KClass
6
8
9
+ internal class SequenceToIteratorConverter (private val input : JavaType ) : StdConverter<Sequence<*>, Iterator<*>>() {
10
+ override fun convert (value : Sequence <* >): Iterator <* > = value.iterator()
11
+
12
+ override fun getInputType (typeFactory : TypeFactory ): JavaType = input
13
+ // element-type may not be obtained, so a null check is required
14
+ override fun getOutputType (typeFactory : TypeFactory ): JavaType = input.containedType(0 )
15
+ ?.let { typeFactory.constructCollectionLikeType(Iterator ::class .java, it) }
16
+ ? : typeFactory.constructType(Iterator ::class .java)
17
+ }
18
+
7
19
// S is nullable because value corresponds to a nullable value class
8
20
// @see KotlinNamesAnnotationIntrospector.findNullSerializer
9
21
internal class ValueClassBoxConverter <S : Any ?, D : Any >(
Original file line number Diff line number Diff line change @@ -64,9 +64,17 @@ internal class KotlinAnnotationIntrospector(private val context: Module.SetupCon
64
64
return super .findCreatorAnnotation(config, a)
65
65
}
66
66
67
- // Find a converter to handle the case where the getter returns an unboxed value from the value class.
68
- override fun findSerializationConverter (a : Annotated ): Converter <* , * >? = (a as ? AnnotatedMethod )?.let { _ ->
69
- cache.findValueClassReturnType(a)?.let { cache.getValueClassBoxConverter(a.rawReturnType, it) }
67
+ override fun findSerializationConverter (a : Annotated ): Converter <* , * >? = when (a) {
68
+ // Find a converter to handle the case where the getter returns an unboxed value from the value class.
69
+ is AnnotatedMethod -> cache.findValueClassReturnType(a)
70
+ ?.let { cache.getValueClassBoxConverter(a.rawReturnType, it) }
71
+ ? : a.takeIf { Sequence ::class .java.isAssignableFrom(it.rawType) }
72
+ ?.let { SequenceToIteratorConverter (it.type) }
73
+
74
+ is AnnotatedClass -> a
75
+ .takeIf { Sequence ::class .java.isAssignableFrom(it.rawType) }
76
+ ?.let { SequenceToIteratorConverter (it.type) }
77
+ else -> null
70
78
}
71
79
72
80
// Determine if the unbox result of value class is nullAable
Original file line number Diff line number Diff line change @@ -13,12 +13,6 @@ import java.lang.reflect.Method
13
13
import java.lang.reflect.Modifier
14
14
import java.math.BigInteger
15
15
16
- object SequenceSerializer : StdSerializer<Sequence<*>>(Sequence : :class.java) {
17
- override fun serialize (value : Sequence <* >, gen : JsonGenerator , provider : SerializerProvider ) {
18
- provider.defaultSerializeValue(value.iterator(), gen)
19
- }
20
- }
21
-
22
16
object UByteSerializer : StdSerializer<UByte>(UByte : :class.java) {
23
17
override fun serialize (value : UByte , gen : JsonGenerator , provider : SerializerProvider ) =
24
18
gen.writeNumber(value.toShort())
@@ -98,7 +92,6 @@ internal class KotlinSerializers : Serializers.Base() {
98
92
val rawClass = type.rawClass
99
93
100
94
return when {
101
- Sequence ::class .java.isAssignableFrom(rawClass) -> SequenceSerializer
102
95
UByte ::class .java.isAssignableFrom(rawClass) -> UByteSerializer
103
96
UShort ::class .java.isAssignableFrom(rawClass) -> UShortSerializer
104
97
UInt ::class .java.isAssignableFrom(rawClass) -> UIntSerializer
You can’t perform that action at this time.
0 commit comments