Thread
#serialization
    ankushg

    ankushg

    1 year ago
    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

    1 year ago
    What about something like
    @Serializable
    sealed class Foo(
      @Transient val _type: String
    )
    
    @Serializable
    class A(...) : Foo("A")
    e

    ephemient

    1 year ago
    you could do something like
    @Serializable
    sealed class Foo {
        abstract val type: String
    }
    @Serializable
    sealed class A : Foo() {
        override val type: String
           get() = serializer().descriptor.serialName
    }
    r

    Richard Gomez

    1 year ago
    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

    ephemient

    1 year ago
    it's a singleton if there's no generics