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
CLOVIS
08/18/2021, 7:45 PM
What about something like
Copy code
@Serializable
sealed class Foo(
@Transient val _type: String
)
@Serializable
class A(...) : Foo("A")
e
ephemient
08/18/2021, 8:55 PM
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
Richard Gomez
08/18/2021, 8:59 PM
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.