2

I'm have a lib's enum look like this:

enum class VatRate {
    none,
    tax10,
    tax20
}

I can't modify this enum. And I need to deserialize VatRate from xml like this:

<testXml>
  <vat>vat10</vat>
</testXml>

I know how to create and use mixins with jackson. But how to tell jackson to use external function for deserialisation?

1 Answers1

1

If you just want to do it on your own:

fun stringToVat(value: String): VatId { return VatId.valueOf(value) }

An easier way would be to define your expected DTO and let jackson do the unmarshalling:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "testXml")
data class TestXml(
    @field:XmlElement(required = true)
    var vat: VatRate? = null
)

Edit: You can register your own custom Enum Serializer/Deserializer. Does this help?

tpschmidt
  • 2,479
  • 2
  • 17
  • 30