0

我遇到了一个奇怪的杰克逊问题。XmlMapper 无法反序列化自己序列化的内容。并且仅当 Double 数组中有空值时才会发生错误。重现该问题的示例测试用例如下:

@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();
    }
}

序列化的有效载荷如下所示:

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

错误是:

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

1 回答 1

1

如果你替换Double[]ArrayList你得到结果

xmlMapper.readValue(bytes, ArrayList.class)

然后你得到

[{}, {}, {}, 3.0, 34.5]
于 2020-10-01T14:24:59.263 回答