https://kotlinlang.org logo
#serialization
Title
# serialization
p

Paul Woitaschek

11/15/2023, 12:58 PM
Why doesn't the compiler complain when something implementing a serializable interface isn't itself serializable?
j

Joshua Hansen

11/15/2023, 6:45 PM
Are you referring to something like this?
Copy code
@Serializable
sealed interface Person {
    val name: String
}

data class PersonWithAge(
    override val name: String,
    val age: Int,
) : Person
A compiler error here wouldn't really be appropriate, since the above code represents a case where you specifically don't want a particular subclass to be serializable. Perhaps a warning would be more appropriate, forcing the developer to explicity annotate their intention to have this subclass remain unserializable in order to suppress the warning.
p

Paul Woitaschek

11/15/2023, 6:46 PM
Yes. If I do Json.encodeToString(Person.serializer(), PersonWithAge()) it will crash
If you have a sealed class instead of an interface the compiler will fail. This is a reoccurring common mistake our developers make.