You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Specifying a custom serializer for an interface doesn't work at top-level.
Related to #928 and maybe #1077.
To Reproduce
importkotlinx.serialization.*importkotlinx.serialization.descriptors.*importkotlinx.serialization.encoding.*importkotlinx.serialization.json.*
@Serializable(with =FooSerializer::class)
interfaceFoo {
val value:String
}
classFooImpl(overridevalvalue:String) : Foo
object FooSerializer : KSerializer<Foo> {
overrideval descriptor:SerialDescriptor=PrimitiveSerialDescriptor("Foo", PrimitiveKind.STRING)
overridefundeserialize(decoder:Decoder): Foo=FooImpl(decoder.decodeString())
overridefunserialize(encoder:Encoder, value:Foo) {
encoder.encodeString(value.value)
}
}
@Serializable
classWrapper(valwrapped:Foo)
funmain() {
val foo:Foo=FooImpl("abc")
println(Json.encodeToString(Wrapper(foo))) // {"wrapped":"abc"}println(Json.encodeToString(foo)) // Serializer for class 'Foo' is not found.
}
Exception in thread "main" kotlinx.serialization.SerializationException: Serializer for class 'Foo' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
at kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:91)
at kotlinx.serialization.internal.PlatformKt.platformSpecificSerializerNotRegistered(Platform.kt:29)
at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:59)
at kotlinx.serialization.SerializersKt.serializer(Unknown Source)
at MainKt.main(Main.kt:51)
at MainKt.main(Main.kt)
Expected behavior
The second encodeToString invocation should use the same serializer for Foo as the serializer for Wrapper does implicitly.
Environment
Kotlin version: 1.4.21
Library version: 1.0.1
Kotlin platforms: JVM
The text was updated successfully, but these errors were encountered:
Specifying a custom serializer for an interface doesn't work at top-level.
Related to #928 and maybe #1077.
To Reproduce
Expected behavior
The second
encodeToString
invocation should use the same serializer forFoo
as the serializer forWrapper
does implicitly.Environment
The text was updated successfully, but these errors were encountered: