Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XmlMapper does not support multi-dimensional arrays #556

Open
schwarfl opened this issue Nov 14, 2022 · 1 comment
Open

XmlMapper does not support multi-dimensional arrays #556

schwarfl opened this issue Nov 14, 2022 · 1 comment

Comments

@schwarfl
Copy link

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]]
@schwarfl schwarfl added the to-evaluate Issue that has been received but not yet evaluated label Nov 14, 2022
@cowtowncoder cowtowncoder changed the title XmlMapper does not work correctly for multidimensional arrays XmlMapper does not support multi-dimensional arrays Nov 15, 2022
@cowtowncoder
Copy link
Member

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).

@cowtowncoder cowtowncoder transferred this issue from FasterXML/jackson-databind Nov 16, 2022
@cowtowncoder cowtowncoder removed the to-evaluate Issue that has been received but not yet evaluated label Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants