Can I deserialize a list of heterogenous XML elements into records and sealed types with Jackson XmlMapper?

21 Views Asked by At

Let's say I have a XML structure where one level contains mixed elements, like this:

<bowl>
    <banana />
    <apple />
    <banana />
</bowl>

Using XmlMapper, is there any way of deserialize this to a structure like this?

sealed interface Fruit {
    record Banana() implements Fruit {}
    record Apple() implements Fruit {}
}

record Bowl(
    List<Fruit> fruits
) {

I have been trying the annotations (@JsonTypeInfo, @JsonSubTypes) that I usually use for these situations when decoding JSON, but can't find a combination that works.

Do I have to use a custom deserializer like suggested in this answer, or is there a way to do this using @JsonSubTypes?

0

There are 0 best solutions below