PolymorphismStyle.None resulting in an error #514
-
I saw that For the parsing process I have the following example model class: @Serializable
data class Specification(
val params: List<Param>,
)
@Serializable
sealed class Param {
abstract val name: String
@SerialName("desc") abstract val description: String
abstract val valueType: String
} Then I try to parse it with the following example: Yaml(
configuration = YamlConfiguration(
allowAnchorsAndAliases = true,
polymorphismStyle = PolymorphismStyle.None,
)
).decodeFromString(
deserializer = Specification.serializer(),
string = """
|params:
| - ¶m_EventStep
| name: event_step
| desc: "Something"
| valueType: string
""".trimMargin(),
) Unfortunately I receive an error which looks like:
Can you tell me what exactly the problem is? As far as I understood the new |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
In your case, I'd suggest creating a custom serializer for the |
Beta Was this translation helpful? Give feedback.
-
Exactly. I requested this feature because I wanted to serialize a polymorphic tree. The resulting yaml would be read by an external program that does not check types. |
Beta Was this translation helpful? Give feedback.
PolymorphismStyle.None
does not support deserializing at all - it is for serialization only.In your case, I'd suggest creating a custom serializer for the
Param
type that constructs the correct subtype ofParam
based on the value ofvalueType
. There's more information about this in the kotlinx.serialization docs here. (Note that the examples refer to JSON, but the same ideas apply for YAML.)