Skip to content

Deserialization of heterogeneous list #787

@cowtowncoder

Description

@cowtowncoder

Discussed in #786

Originally posted by kristofvb November 19, 2025
Hi,

I have some questions about the following sample:

class ParseTest {

    @Test
    void parse() throws Exception {
        var mapper = new XmlMapper();
        var root = mapper.readValue("""
                <root>
                    <foos>
                        <foo sequenceNr="1">somefoo</foo>
                        <foo sequenceNr="2">otherfoo</foo>
                        <bar>something very different</bar>
                    </foos>
                </root>
                """, Root.class);
        System.out.println(root);
    }

    private static class Root {
        @JacksonXmlProperty(localName = "foos")
        private List<Foo> foos = new ArrayList<>();

        public String toString() {
            return "Root{" +
                    "foos=" + foos +
                    '}';
        }
    }

    private static class Foo {
        @JacksonXmlText
        String text;

        @JacksonXmlProperty(isAttribute = true)
        Integer sequenceNr;

        public String toString() {
            return "Foo{" +
                    "text='" + text + '\'' +
                    ", sequenceNr=" + sequenceNr +
                    '}';
        }
    }

}
  • is there a way to make sure the foos list only contains Foo instances for actual foo elements and avoid turning the bar element into a Foo instance?
  • is there a way (annotation) to sort the foos list on sequenceNr of the elements? Currently I sort the list in the foos setter (which I omitted in the sample).

Thanks for looking into this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.xIssue planned for 3.x (and not 2.x)gen-aiPR created with Generative AI (whole or assisted) (or issue for which gen-ai seems suitable)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions