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
Serializing multidimensional arrays to XML looses dimension information
When serializing an two dimensional array to XML, one dimension gets lost. In other words, serializing an two dimensional array to XML produces the same result as serializing an one dimensional array. When serializing to JSON, the information is NOT lost.
Version information
2.14 (jackson-dataformat-xml-2.14.0.jar)
To Reproduce
public static void main(String[] args) throws JsonProcessingException {
final ObjectMapper xmlMapper = new XmlMapper();
final ObjectMapper mapper = new ObjectMapper();
boolean[] booleanArray = new boolean[] {
true, false
};
boolean[][] boolean2DArray = new boolean[][] {
{
true
}, {
false
}
};
serialize(xmlMapper, booleanArray, "1DArray to XML");
serialize(xmlMapper, boolean2DArray, "2DArray to XML");
serialize(mapper, booleanArray, "1DArray to JSON");
serialize(mapper, boolean2DArray, "2DArray to JSON");
}
private static void serialize(ObjectMapper mapper, Object object, String text) throws JsonProcessingException {
String singleArrayResult = mapper.writeValueAsString(object);
System.out.println(text + ": " + singleArrayResult);
}
will print
1DArray to XML: <booleans><item>true</item><item>false</item></booleans>
2DArray to XML: <booleans><item>true</item><item>false</item></booleans>
1DArray to JSON: [true,false]
2DArray to JSON: [[true],[false]]
The text was updated successfully, but these errors were encountered:
cowtowncoder
changed the title
XmlMapper does not work correctly for multidimensional arraysXmlMapper does not support multi-dimensional arrays
Nov 15, 2022
Correct: in general, "immediately" nested Collections, arrays and Maps will not work -- as with most XML libraries that try to use natural-style XML, it is usually necessary to have one layer of POJOs in-between arrays.
(by "natural-style" I mean that XML structured should closely match Java Object structure, without artificial wrappers).
So following would not work either:
List<List<String>>
but
List<PojoWithLists>
would work (wherein PojoWithLists can have List / array members)
Ideally it would be possible to support this case; but at very least there should be an immediate failure if usage is attempted, instead of quietly losing data.
Will leave this open for future improvements (and maybe documentation for anyone else trying to work with multi-dimensional arrays).
Serializing multidimensional arrays to XML looses dimension information
When serializing an two dimensional array to XML, one dimension gets lost. In other words, serializing an two dimensional array to XML produces the same result as serializing an one dimensional array. When serializing to JSON, the information is NOT lost.
Version information
2.14 (jackson-dataformat-xml-2.14.0.jar)
To Reproduce
will print
The text was updated successfully, but these errors were encountered: