From 0b32adc07b844abeb47ecfba9d6ae7dac40e8065 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 3 Dec 2023 13:15:29 +0900 Subject: [PATCH 1/2] Fix problem with Serializable objects not implementing readResolve SequenceSerializer has not been changed as it will be removed in 2.17 --- .../jackson/module/kotlin/KotlinDeserializers.kt | 12 ++++++++++++ .../jackson/module/kotlin/KotlinKeyDeserializers.kt | 10 ++++++++++ .../jackson/module/kotlin/KotlinKeySerializers.kt | 2 ++ .../jackson/module/kotlin/KotlinSerializers.kt | 10 ++++++++++ 4 files changed, 34 insertions(+) diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinDeserializers.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinDeserializers.kt index 0a927c9a..ce8bcf4f 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinDeserializers.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinDeserializers.kt @@ -13,12 +13,16 @@ import com.fasterxml.jackson.databind.deser.std.StdDeserializer import kotlin.time.Duration as KotlinDuration object SequenceDeserializer : StdDeserializer>(Sequence::class.java) { + private fun readResolve(): Any = SequenceDeserializer + override fun deserialize(p: JsonParser, ctxt: DeserializationContext): Sequence<*> { return ctxt.readValue(p, List::class.java).asSequence() } } object RegexDeserializer : StdDeserializer(Regex::class.java) { + private fun readResolve(): Any = RegexDeserializer + override fun deserialize(p: JsonParser, ctxt: DeserializationContext): Regex { val node = ctxt.readTree(p) @@ -43,6 +47,8 @@ object RegexDeserializer : StdDeserializer(Regex::class.java) { } object UByteDeserializer : StdDeserializer(UByte::class.java) { + private fun readResolve(): Any = UByteDeserializer + override fun deserialize(p: JsonParser, ctxt: DeserializationContext) = p.shortValue.asUByte() ?: throw InputCoercionException( p, @@ -53,6 +59,8 @@ object UByteDeserializer : StdDeserializer(UByte::class.java) { } object UShortDeserializer : StdDeserializer(UShort::class.java) { + private fun readResolve(): Any = UShortDeserializer + override fun deserialize(p: JsonParser, ctxt: DeserializationContext) = p.intValue.asUShort() ?: throw InputCoercionException( p, @@ -63,6 +71,8 @@ object UShortDeserializer : StdDeserializer(UShort::class.java) { } object UIntDeserializer : StdDeserializer(UInt::class.java) { + private fun readResolve(): Any = UIntDeserializer + override fun deserialize(p: JsonParser, ctxt: DeserializationContext) = p.longValue.asUInt() ?: throw InputCoercionException( p, @@ -73,6 +83,8 @@ object UIntDeserializer : StdDeserializer(UInt::class.java) { } object ULongDeserializer : StdDeserializer(ULong::class.java) { + private fun readResolve(): Any = ULongDeserializer + override fun deserialize(p: JsonParser, ctxt: DeserializationContext) = p.bigIntegerValue.asULong() ?: throw InputCoercionException( p, diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinKeyDeserializers.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinKeyDeserializers.kt index fdf44a65..70e2e35a 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinKeyDeserializers.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinKeyDeserializers.kt @@ -10,6 +10,8 @@ import com.fasterxml.jackson.databind.deser.std.StdKeyDeserializers // If StdKeyDeserializer is modified, need to modify this too. internal object UByteKeyDeserializer : StdKeyDeserializer(TYPE_SHORT, UByte::class.java) { + private fun readResolve(): Any = UByteKeyDeserializer + override fun deserializeKey(key: String?, ctxt: DeserializationContext): UByte? = super.deserializeKey(key, ctxt) ?.let { (it as Short).asUByte() ?: throw InputCoercionException( @@ -22,6 +24,8 @@ internal object UByteKeyDeserializer : StdKeyDeserializer(TYPE_SHORT, UByte::cla } internal object UShortKeyDeserializer : StdKeyDeserializer(TYPE_INT, UShort::class.java) { + private fun readResolve(): Any = UShortKeyDeserializer + override fun deserializeKey(key: String?, ctxt: DeserializationContext): UShort? = super.deserializeKey(key, ctxt) ?.let { (it as Int).asUShort() ?: throw InputCoercionException( @@ -34,6 +38,8 @@ internal object UShortKeyDeserializer : StdKeyDeserializer(TYPE_INT, UShort::cla } internal object UIntKeyDeserializer : StdKeyDeserializer(TYPE_LONG, UInt::class.java) { + private fun readResolve(): Any = UIntKeyDeserializer + override fun deserializeKey(key: String?, ctxt: DeserializationContext): UInt? = super.deserializeKey(key, ctxt) ?.let { (it as Long).asUInt() ?: throw InputCoercionException( @@ -47,6 +53,8 @@ internal object UIntKeyDeserializer : StdKeyDeserializer(TYPE_LONG, UInt::class. // kind parameter is dummy. internal object ULongKeyDeserializer : StdKeyDeserializer(TYPE_LONG, ULong::class.java) { + private fun readResolve(): Any = ULongKeyDeserializer + override fun deserializeKey(key: String?, ctxt: DeserializationContext): ULong? = key?.let { it.toBigInteger().asULong() ?: throw InputCoercionException( null, @@ -58,6 +66,8 @@ internal object ULongKeyDeserializer : StdKeyDeserializer(TYPE_LONG, ULong::clas } internal object KotlinKeyDeserializers : StdKeyDeserializers() { + private fun readResolve(): Any = KotlinKeyDeserializers + override fun findKeyDeserializer( type: JavaType, config: DeserializationConfig?, diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinKeySerializers.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinKeySerializers.kt index 06ddb96e..c6a5d774 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinKeySerializers.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinKeySerializers.kt @@ -13,6 +13,8 @@ import java.lang.reflect.Method import java.lang.reflect.Modifier internal object ValueClassUnboxKeySerializer : StdSerializer(Any::class.java) { + private fun readResolve(): Any = ValueClassUnboxKeySerializer + override fun serialize(value: Any, gen: JsonGenerator, provider: SerializerProvider) { val method = value::class.java.getMethod("unbox-impl") val unboxed = method.invoke(value) diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinSerializers.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinSerializers.kt index 6821796a..edfe23ea 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinSerializers.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinSerializers.kt @@ -24,21 +24,29 @@ object SequenceSerializer : StdSerializer>(Sequence::class.java) { } object UByteSerializer : StdSerializer(UByte::class.java) { + private fun readResolve(): Any = UByteSerializer + override fun serialize(value: UByte, gen: JsonGenerator, provider: SerializerProvider) = gen.writeNumber(value.toShort()) } object UShortSerializer : StdSerializer(UShort::class.java) { + private fun readResolve(): Any = UShortSerializer + override fun serialize(value: UShort, gen: JsonGenerator, provider: SerializerProvider) = gen.writeNumber(value.toInt()) } object UIntSerializer : StdSerializer(UInt::class.java) { + private fun readResolve(): Any = UIntSerializer + override fun serialize(value: UInt, gen: JsonGenerator, provider: SerializerProvider) = gen.writeNumber(value.toLong()) } object ULongSerializer : StdSerializer(ULong::class.java) { + private fun readResolve(): Any = ULongSerializer + override fun serialize(value: ULong, gen: JsonGenerator, provider: SerializerProvider) { val longValue = value.toLong() when { @@ -54,6 +62,8 @@ private fun Class<*>.getStaticJsonValueGetter(): Method? = this.declaredMethods. } object ValueClassUnboxSerializer : StdSerializer(Any::class.java) { + private fun readResolve(): Any = ValueClassUnboxSerializer + override fun serialize(value: Any, gen: JsonGenerator, provider: SerializerProvider) { val unboxed = value::class.java.getMethod("unbox-impl").invoke(value) From 230640858fdef577d3d16532ad6d236f5fb58a5c Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 3 Dec 2023 13:20:10 +0900 Subject: [PATCH 2/2] Update release notes wrt #733 --- release-notes/CREDITS-2.x | 5 +++++ release-notes/VERSION-2.x | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 5ee73efe..f650a00d 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -17,6 +17,11 @@ Contributors: # 2.17.0 (not yet released) +# 2.16.1 (not yet released) + +WrongWrong (@k163377) +* #733: Fix problem with Serializable objects not implementing readResolve + # 2.16.0 kkurczewski diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index e0b4b0fc..c82ca862 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -16,6 +16,10 @@ Co-maintainers: === Releases === ------------------------------------------------------------------------ +2.16.1 (not yet released) + +#733: Fix problem with Serializable objects not implementing readResolve. + 2.16.0 (15-Nov-2023) #707: If JsonSetter(nulls = Nulls.SKIP) is specified, the default argument is now used when null.