You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Jackson version: 2.9.0
Occurs with and without woodstox.
I am trying to deserialize some external XML containing the attribute xml:space="preserve". This fails with MismatchedInputException: Cannot construct instance of TestDeserialization$Inner (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('Content 2')
This works fine without xml:space or with xml:space on the <beta> tags.
publicclassTestDeserialization {
staticclassOuter {
@JacksonXmlElementWrapper(useWrapping = false) publicList<Inner> inner;
}
staticclassInner {
publicStringalpha;
publicStringbeta;
}
publicstaticvoidmain(String[] args) throwsIOException {
//Same xml as aboveStringxml = "<Outer><inner><alpha xml:space=\"preserve\">Content 1</alpha><beta>Content 2</beta></inner><inner><alpha xml:space=\"preserve\">Content 3</alpha><beta>Content 4</beta></inner></Outer>";
ObjectMappermapper = newXmlMapper();
StringnoSpace = xml.replace(" xml:space=\"preserve\"", "");
System.out.println("Works fine without xml:space:" + noSpace);
System.out.println(mapper.readValue(noSpace, Outer.class).inner.get(0).beta);
StringspaceBeta = noSpace.replace("<beta", "<beta xml:space=\"preserve\"");
System.out.println("Works fine with xml:space on <beta" + spaceBeta);
System.out.println(mapper.readValue(spaceBeta, Outer.class).inner.get(0).beta);
//This throws MismatchedInputException: Cannot construct instance of `TestDeserialization$Inner`System.out.println(mapper.readValue(xml, Outer.class).inner.get(0).beta);
}
}
Below is some more code for serialization and deserialization round-trips. It works fine if the List in Outer has useWrapping = true, it works fine if you replace List with Inner. It also works if you swap the beta and alpha fields in the inner class - as shown above xml:space is fine on the last element in the XML. The order of the class fields does only matter because its the order of the fields in the XML.
publicclassTest {
staticclassOuter {
//It works fine if useWrapping = true//It also works if inner is a simple field instead of a collection@JacksonXmlElementWrapper(useWrapping = false) publicList<Inner> inner;
}
staticclassInner {
publicStringalpha;
//It works if beta is before alpha or the beta is missing from this classpublicStringbeta;
}
publicstaticvoidmain(String[] args) throwsIOException {
ObjectMappermapper = newXmlMapper();
Innerinner = newInner();
inner.alpha = "Content 1";
inner.beta = "Content 2";
Outerouter = newOuter();
outer.inner = Arrays.asList(inner, inner);
Stringxml = mapper.writeValueAsString(outer);
//This worksSystem.out.println(mapper.readValue(xml, Outer.class).inner.get(0).beta);
StringxmlWithSpace = xml.replace("<alpha>", "<alpha xml:space=\"preserve\">");
System.out.println(xmlWithSpace);
//This throws MismatchedInputException: Cannot construct instance of `Test2$Inner`System.out.println(mapper.readValue(xmlWithSpace, Outer.class).inner.get(0).beta);
}
}
The text was updated successfully, but these errors were encountered:
cowtowncoder
changed the title
Deserialization fails of lists containing elements with xml:space
Deserialization fails of lists containing elements with xml:space attribute
Jun 7, 2020
Jackson version: 2.9.0
Occurs with and without woodstox.
I am trying to deserialize some external XML containing the attribute
xml:space="preserve"
. This fails with MismatchedInputException: Cannot construct instance ofTestDeserialization$Inner
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('Content 2')XML Example:
This works fine without xml:space or with xml:space on the
<beta>
tags.Below is some more code for serialization and deserialization round-trips. It works fine if the List in Outer has useWrapping = true, it works fine if you replace List with Inner. It also works if you swap the beta and alpha fields in the inner class - as shown above xml:space is fine on the last element in the XML. The order of the class fields does only matter because its the order of the fields in the XML.
The text was updated successfully, but these errors were encountered: