Using `DraftSource.build(actionId = 0, messageId =...
# serialization
d
Using
DraftSource.build(actionId = 0, messageId = "id")
or also
as DraftSource
is failing with
Can't locate argument-less serializer for class
s
Try to pass serializer explicitly, e.g.
PolymorphicSerializer(DraftSource::class)
. Or try new library version with buillt-in support for sealed classes: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#sealed-classes (requires kotlin 1.3.60)
d
Thank you, I’ve kinda solved because the class was a parameter for another class, so serializing it directly is working. I was trying with
DraftSource::class.serizalizer()
😄 Oh, good to know that sealed class are “automatically” supported now. Some improvements has been applied? I had to refactor in the following way for get it working:
Copy code
sealed class DraftSource {
  ...
  open val messageId = generateDraftId()
👇
Copy code
sealed class DraftSource {
  abstract val messageId: String

 @Serializable
  @Suppress("DataClassPrivateConstructor") // Still accessible by `copy` function
  data class Plain private constructor(
    override val actionId: Int,
    override val messageId: String
  ): DraftSource() {
    constructor(actionId: Int) :
            this(actionId, generateDraftId())
  }
Otherwise
messageId
would generate a new id by
generateDraftId()
every time an object is deserialized