Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom interface serializer cannot be found at top-level #1291

Closed
fluidsonic opened this issue Jan 13, 2021 · 2 comments
Closed

Custom interface serializer cannot be found at top-level #1291

fluidsonic opened this issue Jan 13, 2021 · 2 comments

Comments

@fluidsonic
Copy link

Specifying a custom serializer for an interface doesn't work at top-level.
Related to #928 and maybe #1077.

To Reproduce

import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*


@Serializable(with = FooSerializer::class)
interface Foo {

    val value: String
}


class FooImpl(override val value: String) : Foo


object FooSerializer : KSerializer<Foo> {

    override val descriptor: SerialDescriptor =
        PrimitiveSerialDescriptor("Foo", PrimitiveKind.STRING)


    override fun deserialize(decoder: Decoder): Foo =
        FooImpl(decoder.decodeString())


    override fun serialize(encoder: Encoder, value: Foo) {
        encoder.encodeString(value.value)
    }
}


@Serializable
class Wrapper(val wrapped: Foo)


fun main() {
    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
@sandwwraith
Copy link
Member

Duplicate of #1207

@sandwwraith sandwwraith marked this as a duplicate of #1207 Feb 6, 2023
@sandwwraith
Copy link
Member

(closing as duplicate, don't know why I haven't done this earlier)

@sandwwraith sandwwraith closed this as not planned Won't fix, can't repro, duplicate, stale Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants