kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

@Contextual loses priority over @UseSerializers when used on generic argument types

Open sandwwraith opened this issue 2 years ago • 0 comments

To Reproduce

@file:UseSerializers(BruhSerializer1::class)

class Bruh

@Serializable
class HolderB(val b: List<@Contextual Bruh>)

object BruhSerializer1: KSerializer<Bruh> {
    override val descriptor: SerialDescriptor
        get() = TODO("descriptor1")

    override fun serialize(encoder: Encoder, value: Bruh) {
        TODO("serialize1")
    }

    override fun deserialize(decoder: Decoder): Bruh {
        TODO("deserialize1")
    }
}

object BruhSerializer2: KSerializer<Bruh> {
    override val descriptor: SerialDescriptor
        get() = TODO("descriptor2")

    override fun serialize(encoder: Encoder, value: Bruh) {
        TODO("serialize2")
    }

    override fun deserialize(decoder: Decoder): Bruh {
        TODO("deserialize2")
    }
}

fun bruhTest() {
    val h = HolderB(listOf(Bruh()))
    val j = Json { serializersModule = serializersModuleOf(BruhSerializer2) }
    j.encodeToString(h) 
}

Expected behavior

Here, not implemented: serialize1 is thrown, but it has to be serialize2.

Environment

  • Kotlin version: 1.8.10
  • Library version: 1.5.0-RC
  • Kotlin platforms: JVM

sandwwraith avatar Feb 16 '23 14:02 sandwwraith