1. Is it possible to use a field as a discriminato...
# serialization
a
2. Is it possible to use a field as a discriminator and also have it in the deserialized object? I.e., we are successfully using
type
as the discriminator, but we'd also like to have a
val type
inside the deserialized object, containing the same content as
type
from the serialization logic. I imagine that the tricky part here would be that there's no guarantees that the
val type
would always line up with the class discriminator when serializing or mutating a deserialized class?
c
What about something like
Copy code
@Serializable
sealed class Foo(
  @Transient val _type: String
)

@Serializable
class A(...) : Foo("A")
e
you could do something like
Copy code
@Serializable
sealed class Foo {
    abstract val type: String
}
@Serializable
sealed class A : Foo() {
    override val type: String
       get() = serializer().descriptor.serialName
}
🤔 1
r
Would
by lazy { }
be preferable to
get()
? I assume the latter would instantiate a new serializer each time it's called, but it may be a singleton.
e
it's a singleton if there's no generics
👍 1