Closed
Description
Please refer to http://stackoverflow.com/questions/20673153/using-jackson-to-deserialize-a-collection-of-children-with-the-same-type-as-the
I can not figure out how to deserialize a document like this:
<test id="0">
<test id="0.1">
<test id="0.1.1" />
</test>
<test id="0.2" />
<test id="0.3">
<test id="0.3.1" />
</test>
</test>
Where each test
element is deserialized into a simple POJO.
Here is the code I am dealing with.
public class UnitTest {
@Test
public void test() throws Exception {
final ObjectMapper mapper = new XmlMapper();
final XmlTest before =
new XmlTest("0", Lists.newArrayList(new XmlTest("0.1", null),
new XmlTest("0.2", Lists.newArrayList(new XmlTest("0.2.1", null)))));
final String xml = mapper.writeValueAsString(before);
System.out.println(xml);
final XmlTest after = mapper.readValue(xml, XmlTest.class);
}
@JacksonXmlRootElement(localName = "test")
public static class XmlTest {
@JacksonXmlProperty(localName = "id", isAttribute = true)
private String id;
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "test")
private List<XmlTest> children;
public XmlTest() {}
public XmlTest(final String id, final List<XmlTest> children) {
this.id = id;
this.children = children;
}
}
}
My imported dependencies:
compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: jacksonVersion
compile group: 'com.fasterxml.jackson.jaxrs', name: 'jackson-jaxrs-xml-provider', version: jacksonVersion
jacksonVersion=2.2.+
Metadata
Metadata
Assignees
Labels
No labels