Jackson XmlMaaper fails to decode null Double

61 Views Asked by At

I’m experiencing a strange Jackson issue. The XmlMapper could not deserialize what is serialized by itself. And the error only occurs when there’re nulls in the Double array. A sample test case to reproduce the issue is below:

@Test
public void testDerSer(){
    Double[] testDouble = new Double[]{null, null, null, 3.0d, 34.5d};
    try {
        ObjectMapper xmlMapper = new XmlMapper();
        byte[] bytes = xmlMapper.writeValueAsBytes(testDouble);
        System.out.println(new String(bytes));
        Double[] doubles = xmlMapper.readValue(bytes, Double[].class);
        System.out.println(doubles);
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

The serialized payload looks like this:

<Doubles><item/><item/><item/><item>3.0</item><item>34.5</item></Doubles>

And the error is:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.Double` out of START_OBJECT token
1

There are 1 best solutions below

0
On

If you replace Double[] with ArrayList you get result

xmlMapper.readValue(bytes, ArrayList.class)

then you get

[{}, {}, {}, 3.0, 34.5]