Skip to content

Commit

Permalink
Implementation in Converter, replacing Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
k163377 committed May 6, 2023
1 parent 81e9700 commit 4f3b782
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/main/kotlin/com/fasterxml/jackson/module/kotlin/Converters.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package com.fasterxml.jackson.module.kotlin

import com.fasterxml.jackson.databind.JavaType
import com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer
import com.fasterxml.jackson.databind.type.TypeFactory
import com.fasterxml.jackson.databind.util.StdConverter
import kotlin.reflect.KClass

internal class SequenceToIteratorConverter(private val input: JavaType) : StdConverter<Sequence<*>, Iterator<*>>() {
override fun convert(value: Sequence<*>): Iterator<*> = value.iterator()

override fun getInputType(typeFactory: TypeFactory): JavaType = input
// element-type may not be obtained, so a null check is required
override fun getOutputType(typeFactory: TypeFactory): JavaType = input.containedType(0)
?.let { typeFactory.constructCollectionLikeType(Iterator::class.java, it) }
?: typeFactory.constructType(Iterator::class.java)
}

// S is nullable because value corresponds to a nullable value class
// @see KotlinNamesAnnotationIntrospector.findNullSerializer
internal class ValueClassBoxConverter<S : Any?, D : Any>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,17 @@ internal class KotlinAnnotationIntrospector(private val context: Module.SetupCon
return super.findCreatorAnnotation(config, a)
}

// Find a converter to handle the case where the getter returns an unboxed value from the value class.
override fun findSerializationConverter(a: Annotated): Converter<*, *>? = (a as? AnnotatedMethod)?.let { _ ->
cache.findValueClassReturnType(a)?.let { cache.getValueClassBoxConverter(a.rawReturnType, it) }
override fun findSerializationConverter(a: Annotated): Converter<*, *>? = when (a) {
// Find a converter to handle the case where the getter returns an unboxed value from the value class.
is AnnotatedMethod -> cache.findValueClassReturnType(a)
?.let { cache.getValueClassBoxConverter(a.rawReturnType, it) }
?: a.takeIf { Sequence::class.java.isAssignableFrom(it.rawType) }
?.let { SequenceToIteratorConverter(it.type) }

is AnnotatedClass -> a
.takeIf { Sequence::class.java.isAssignableFrom(it.rawType) }
?.let { SequenceToIteratorConverter(it.type) }
else -> null
}

// Determine if the unbox result of value class is nullAable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ import java.lang.reflect.Method
import java.lang.reflect.Modifier
import java.math.BigInteger

object SequenceSerializer : StdSerializer<Sequence<*>>(Sequence::class.java) {
override fun serialize(value: Sequence<*>, gen: JsonGenerator, provider: SerializerProvider) {
provider.defaultSerializeValue(value.iterator(), gen)
}
}

object UByteSerializer : StdSerializer<UByte>(UByte::class.java) {
override fun serialize(value: UByte, gen: JsonGenerator, provider: SerializerProvider) =
gen.writeNumber(value.toShort())
Expand Down Expand Up @@ -98,7 +92,6 @@ internal class KotlinSerializers : Serializers.Base() {
val rawClass = type.rawClass

return when {
Sequence::class.java.isAssignableFrom(rawClass) -> SequenceSerializer
UByte::class.java.isAssignableFrom(rawClass) -> UByteSerializer
UShort::class.java.isAssignableFrom(rawClass) -> UShortSerializer
UInt::class.java.isAssignableFrom(rawClass) -> UIntSerializer
Expand Down

0 comments on commit 4f3b782

Please sign in to comment.