@Serializable
sealed class Component : Tagged<UUID, Component> {
@Serializable @SerialName("name")
data class Name(val name: String, @Transient override val tag: Tag<Component> = Tag.Name) : Component()
@Serializable @SerialName("parent")
data class Parent(val parent: UUID, @Transient override val tag: Tag<Component> = Tag.Parent) : Component()
@Serializable @SerialName("entity_type")
sealed class EntityType : Component() {
@Transient override val tag = Tag.EntityType
@Serializable @SerialName("department") object Department : EntityType()
@Serializable @SerialName("function") object Function : EntityType()
}
@Serializable @SerialName("ordered_children")
data class OrderedChildren(val children: List<UUID> = listOf(), override val tag: Tag<Component> = Tag.OrderedChildren) : Component()
}
However, when serializing I get the following error:
Class 'Department' is not registered for polymorphic serialization in the scope of 'Component'.
Mark the base class as 'sealed' or register the serializer explicitly.
Why is that? Because ‘Department’ is clearly in the sealed class hierarchy.
Just stumbled into this issue as well. I was thinking on writing a custom JsonContentPolymorphicSerializer for that. I will try the workaround. Want to be subscribed to this topic.
So, my case is a bit different. Here is my class structure. Check the inclusion of JsonClassDiscriminator in the base class Component and also in nested base class EntityType. The fields type and sub_type both will be present in the sub classes of EntityType. I get an error on nested base class saying
Argument values for inheritable serial info annotation 'JsonClassDiscriminator' must be the same as the values in parent type 'Component'
is there any workarounds for this issue?
@Serializable
@JsonClassDiscriminator("base_type")
sealed class Component : Tagged<UUID, Component> {
@Serializable @SerialName("name")
data class Name(val name: String, @Transient override val tag: Tag<Component> = Tag.Name) : Component()
@Serializable @SerialName("parent")
data class Parent(val parent: UUID, @Transient override val tag: Tag<Component> = Tag.Parent) : Component()
@Serializable
**@JsonClassDiscriminator("sub_type")**
sealed class EntityType : Component() {
@Serializable @SerialName("department") data class Department : EntityType()
@Serializable @SerialName("function") data class Function : EntityType()
}
@Serializable @SerialName("ordered_children")
data class OrderedChildren(val children: List<UUID> = listOf(), override val tag: Tag<Component> = Tag.OrderedChildren) : Component()
}