Deserialise enum with different names
tagy opened this issue · comments
The following deserialises fine:
val dogXML = """
<dog name="fluffy">
<age>22</age>
<breed>pug</breed>
</dog>
""".trimIndent()
@Serializable
@XmlSerialName(value = "breed")
enum class Breed {
pug,
corgi,
poodle
}
@Serializable
class Dog(
@XmlElement(false) val name: String,
@XmlElement(true) val age: Int,
@XmlElement(true) val breed: Breed
)
But if the names of your enum do not match the xml I cannot see a way to rename them.
eg changing the enum to below does not work. It seems like the XmlSerialName
is ignored in this case.
@Serializable
enum class Breed {
@XmlSerialName(value = "pug")
PUG,
@XmlSerialName(value = "corgi")
CORGI,
@XmlSerialName(value = "poodle")
POODLE
}
Not sure if this should be possible? It looks like it is with regular Kotlinx serialisation and json.
In my case, the I have enum that are integers in the xml (0, 1, 2) which I would like to deserialise straight to an enum with useful names.
For enum constant names you have to use the regular @SerialName
as that is what is picked up by the EnumSerializer (part of kotlinx.serialization). Yes, this means that enum constants can not map to qnames at this point.