custom xml deserializer in jackson for enum

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?

Add Comment
1 Answer(s)

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?

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.