Skip to content

Commit

Permalink
Changes wrt databind/#3043: rename SerializerProvider as Serializatio…
Browse files Browse the repository at this point in the history
…nContext (#862)
  • Loading branch information
cowtowncoder authored Nov 29, 2024
1 parent 70b497c commit 68525e2
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import tools.jackson.core.JsonGenerator
import tools.jackson.databind.BeanDescription
import tools.jackson.databind.JavaType
import tools.jackson.databind.SerializationConfig
import tools.jackson.databind.SerializerProvider
import tools.jackson.databind.SerializationContext
import tools.jackson.databind.ValueSerializer
import tools.jackson.databind.ser.Serializers
import tools.jackson.databind.ser.std.StdSerializer
import java.lang.reflect.Method
import java.lang.reflect.Modifier

internal object ValueClassUnboxKeySerializer : StdSerializer<Any>(Any::class.java) {
override fun serialize(value: Any, gen: JsonGenerator, provider: SerializerProvider) {
override fun serialize(value: Any, gen: JsonGenerator, ctxt: SerializationContext) {
val method = value::class.java.getMethod("unbox-impl")
val unboxed = method.invoke(value)

if (unboxed == null) {
val javaType = provider.typeFactory.constructType(method.genericReturnType)
provider.findNullKeySerializer(javaType, null).serialize(null, gen, provider)
val javaType = ctxt.typeFactory.constructType(method.genericReturnType)
ctxt.findNullKeySerializer(javaType, null).serialize(null, gen, ctxt)
return
}

provider.findKeySerializer(unboxed::class.java, null).serialize(unboxed, gen, provider)
ctxt.findKeySerializer(unboxed::class.java, null).serialize(unboxed, gen, ctxt)
}
}

Expand All @@ -40,15 +40,15 @@ internal class ValueClassStaticJsonKeySerializer<T>(
private val keyType: Class<*> = staticJsonKeyGetter.returnType
private val unboxMethod: Method = t.getMethod("unbox-impl")

override fun serialize(value: T, gen: JsonGenerator, provider: SerializerProvider) {
override fun serialize(value: T, gen: JsonGenerator, ctxt: SerializationContext) {
val unboxed = unboxMethod.invoke(value)
val jsonKey: Any? = staticJsonKeyGetter.invoke(null, unboxed)

val serializer = jsonKey
?.let { provider.findKeySerializer(keyType, null) }
?: provider.findNullKeySerializer(provider.constructType(keyType), null)
?.let { ctxt.findKeySerializer(keyType, null) }
?: ctxt.findNullKeySerializer(ctxt.constructType(keyType), null)

serializer.serialize(jsonKey, gen, provider)
serializer.serialize(jsonKey, gen, ctxt)
}

companion object {
Expand Down
22 changes: 11 additions & 11 deletions src/main/kotlin/tools/jackson/module/kotlin/KotlinSerializers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import tools.jackson.core.JsonGenerator
import tools.jackson.databind.BeanDescription
import tools.jackson.databind.JavaType
import tools.jackson.databind.SerializationConfig
import tools.jackson.databind.SerializerProvider
import tools.jackson.databind.SerializationContext
import tools.jackson.databind.ValueSerializer
import tools.jackson.databind.ser.Serializers
import tools.jackson.databind.ser.std.StdSerializer
Expand All @@ -15,25 +15,25 @@ import java.lang.reflect.Modifier
import java.math.BigInteger

object UByteSerializer : StdSerializer<UByte>(UByte::class.java) {
override fun serialize(value: UByte, gen: JsonGenerator, provider: SerializerProvider) {
override fun serialize(value: UByte, gen: JsonGenerator, ctxt: SerializationContext) {
gen.writeNumber(value.toShort())
}
}

object UShortSerializer : StdSerializer<UShort>(UShort::class.java) {
override fun serialize(value: UShort, gen: JsonGenerator, provider: SerializerProvider) {
override fun serialize(value: UShort, gen: JsonGenerator, ctxt: SerializationContext) {
gen.writeNumber(value.toInt())
}
}

object UIntSerializer : StdSerializer<UInt>(UInt::class.java) {
override fun serialize(value: UInt, gen: JsonGenerator, provider: SerializerProvider) {
override fun serialize(value: UInt, gen: JsonGenerator, ctxt: SerializationContext) {
gen.writeNumber(value.toLong())
}
}

object ULongSerializer : StdSerializer<ULong>(ULong::class.java) {
override fun serialize(value: ULong, gen: JsonGenerator, provider: SerializerProvider) {
override fun serialize(value: ULong, gen: JsonGenerator, ctxt: SerializationContext) {
val longValue = value.toLong()
when {
longValue >= 0 -> gen.writeNumber(longValue)
Expand All @@ -48,15 +48,15 @@ private fun Class<*>.getStaticJsonValueGetter(): Method? = this.declaredMethods.
}

object ValueClassUnboxSerializer : StdSerializer<Any>(Any::class.java) {
override fun serialize(value: Any, gen: JsonGenerator, provider: SerializerProvider) {
override fun serialize(value: Any, gen: JsonGenerator, ctxt: SerializationContext) {
val unboxed = value::class.java.getMethod("unbox-impl").invoke(value)

if (unboxed == null) {
provider.findNullValueSerializer(null).serialize(null, gen, provider)
ctxt.findNullValueSerializer(null).serialize(null, gen, ctxt)
return
}

provider.findValueSerializer(unboxed::class.java).serialize(unboxed, gen, provider)
ctxt.findValueSerializer(unboxed::class.java).serialize(unboxed, gen, ctxt)
}
}

Expand All @@ -66,13 +66,13 @@ internal sealed class ValueClassSerializer<T : Any>(t: Class<T>) : StdSerializer
) : ValueClassSerializer<T>(t) {
private val unboxMethod: Method = t.getMethod("unbox-impl")

override fun serialize(value: T, gen: JsonGenerator, provider: SerializerProvider) {
override fun serialize(value: T, gen: JsonGenerator, ctxt: SerializationContext) {
val unboxed = unboxMethod.invoke(value)
// As shown in the processing of the factory function, jsonValueGetter is always a static method.
val jsonValue: Any? = staticJsonValueGetter.invoke(null, unboxed)
jsonValue
?.let { provider.findValueSerializer(it::class.java).serialize(it, gen, provider) }
?: provider.findNullValueSerializer(null).serialize(null, gen, provider)
?.let { ctxt.findValueSerializer(it::class.java).serialize(it, gen, ctxt) }
?: ctxt.findNullValueSerializer(null).serialize(null, gen, ctxt)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tools.jackson.module.kotlin.test
import tools.jackson.core.JsonGenerator
import tools.jackson.core.JsonParser
import tools.jackson.databind.DeserializationContext
import tools.jackson.databind.SerializerProvider
import tools.jackson.databind.SerializationContext
import tools.jackson.databind.ValueDeserializer
import tools.jackson.databind.ValueSerializer
import tools.jackson.databind.module.SimpleModule
Expand Down Expand Up @@ -51,7 +51,7 @@ data class TestDoubleData(
)

class RoundingSerializer : ValueSerializer<Double>() {
override fun serialize(value: Double?, gen: JsonGenerator?, serializers: SerializerProvider?) {
override fun serialize(value: Double?, gen: JsonGenerator?, ctxt: SerializationContext?) {
value?.let {
gen?.writeNumber(BigDecimal(it).setScale(2, RoundingMode.HALF_UP))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class TestPropertyRequiredness {
}

private fun introspectSerialization(type: Class<*>, mapper: ObjectMapper): BeanDescription =
mapper._serializerProvider().introspectBeanDescription(mapper.constructType(type))
mapper._serializationContext().introspectBeanDescription(mapper.constructType(type))

private fun introspectDeserialization(type: Class<*>, mapper: ObjectMapper): BeanDescription =
mapper._deserializationContext().introspectBeanDescription(mapper.constructType(type))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.junit.Test
import tools.jackson.module.kotlin.jacksonObjectMapper
import tools.jackson.module.kotlin.readValue
import tools.jackson.core.JsonGenerator
import tools.jackson.databind.SerializerProvider
import tools.jackson.databind.SerializationContext
import tools.jackson.databind.annotation.JsonSerialize
import tools.jackson.databind.ser.std.StdSerializer
import kotlin.test.assertEquals
Expand Down Expand Up @@ -47,8 +47,8 @@ class TestSequenceDeserializer {
}

class ContentSer : StdSerializer<String>(String::class.java) {
override fun serialize(value: String, gen: JsonGenerator, provider: SerializerProvider) {
provider.writeValue(gen, "$value-ser")
override fun serialize(value: String, gen: JsonGenerator, ctxt: SerializationContext) {
ctxt.writeValue(gen, "$value-ser")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tools.jackson.module.kotlin.test.github

import tools.jackson.core.JsonGenerator
import tools.jackson.databind.SerializerProvider
import tools.jackson.databind.SerializationContext
import tools.jackson.databind.annotation.JsonSerialize
import tools.jackson.databind.module.SimpleModule
import tools.jackson.databind.ser.std.StdSerializer
Expand All @@ -17,7 +17,7 @@ class GitHub524 {
@JvmInline
value class HasSerializer(val value: Int?)
class Serializer : StdSerializer<HasSerializer>(HasSerializer::class.java) {
override fun serialize(value: HasSerializer, gen: JsonGenerator, provider: SerializerProvider) {
override fun serialize(value: HasSerializer, gen: JsonGenerator, ctxt: SerializationContext) {
gen.writeString(value.toString())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tools.jackson.module.kotlin.test.github

import org.junit.Test
import tools.jackson.core.JsonGenerator
import tools.jackson.databind.SerializerProvider
import tools.jackson.databind.SerializationContext
import tools.jackson.databind.annotation.JsonSerialize
import tools.jackson.databind.ser.std.StdSerializer
import tools.jackson.module.kotlin.jacksonObjectMapper
Expand All @@ -13,7 +13,7 @@ class GitHub618 {
@JvmInline
value class V(val value: String) {
class Serializer : StdSerializer<V>(V::class.java) {
override fun serialize(p0: V, p1: JsonGenerator, p2: SerializerProvider) {
override fun serialize(p0: V, p1: JsonGenerator, p2: SerializationContext) {
p1.writeString(p0.toString())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tools.jackson.module.kotlin.test.github
import tools.jackson.core.JsonGenerator
import tools.jackson.databind.ObjectMapper
import tools.jackson.databind.ObjectWriter
import tools.jackson.databind.SerializerProvider
import tools.jackson.databind.SerializationContext
import tools.jackson.databind.json.JsonMapper
import tools.jackson.databind.module.SimpleModule
import tools.jackson.databind.ser.std.StdSerializer
Expand All @@ -17,7 +17,7 @@ import kotlin.test.assertEquals
class Github464 {
class UnboxTest {
object NullValueClassKeySerializer : StdSerializer<ValueClass>(ValueClass::class.java) {
override fun serialize(value: ValueClass?, gen: JsonGenerator, provider: SerializerProvider) {
override fun serialize(value: ValueClass?, gen: JsonGenerator, ctxt: SerializationContext) {
gen.writeName("null-key")
}
}
Expand Down Expand Up @@ -107,7 +107,7 @@ class Github464 {
}

object NullValueSerializer : StdSerializer<Any>(Any::class.java) {
override fun serialize(value: Any?, gen: JsonGenerator, provider: SerializerProvider) {
override fun serialize(value: Any?, gen: JsonGenerator, ctxt: SerializationContext) {
gen.writeString("null-value")
}
}
Expand Down Expand Up @@ -156,13 +156,13 @@ class Github464 {
value class ValueBySerializer(val value: Int)

object Serializer : StdSerializer<ValueBySerializer>(ValueBySerializer::class.java) {
override fun serialize(value: ValueBySerializer, gen: JsonGenerator, provider: SerializerProvider) {
override fun serialize(value: ValueBySerializer, gen: JsonGenerator, ctxt: SerializationContext) {
gen.writeString(value.value.toString())
}
}

object KeySerializer : StdSerializer<ValueBySerializer>(ValueBySerializer::class.java) {
override fun serialize(value: ValueBySerializer, gen: JsonGenerator, provider: SerializerProvider) {
override fun serialize(value: ValueBySerializer, gen: JsonGenerator, ctxt: SerializationContext) {
gen.writeName(value.value.toString())
}
}
Expand Down

0 comments on commit 68525e2

Please sign in to comment.