I have a simple hierarchy containing of the following:
- abstract class BaseItem
- open class Item : BaseItem
- class Backpack : Item
They should all work with Kotlinx Serialization. It went fine until I added the Backpack
class.
I use version 1.4.32
of Kotlinx Serialization.
Here is my class hierarchy in detail
// Items.kt
@Serializable
sealed class BaseItem {
abstract val id: String
abstract val type: ItemType
abstract var brand: String
abstract var model: String
abstract var imageLink: String
abstract var traits: MutableList<Trait>
abstract var implicitTraits: MutableList<Trait>
abstract var details: MutableMap<String, String>
}
@Serializable
open class Item(
override val id: String = UUID.randomUUID().toString(),
override val type: ItemType = ItemType.UNDEFINED,
override var brand: String,
override var model: String,
override var imageLink: String,
override var traits: MutableList<Trait>,
override var implicitTraits: MutableList<Trait>,
override var details: MutableMap<String, String>,
) : BaseItem()
@Serializable // is marked as incorrect
class Backpack(
brand: String,
model: String,
imageLink: String,
traits: MutableList<Trait>,
implicitTraits: MutableList<Trait>,
details: MutableMap<String, String>,
var volume: Int
) : Item(
type = ItemType.BACKPACK,
brand = brand,
model = model,
imageLink = imageLink,
traits = traits,
implicitTraits = implicitTraits,
details = details
)
The IDE is showing me the following message for the @Serialization
annotation attached to the Backpack
class.
This class is not serializable automatically because it has primary constructor parameters that are not properties
I was not able to find out what the working way it is to make this correct