【问题标题】:Jackson XmlMaaper fails to decode null DoubleJackson XmlMaaper 无法解码 null Double
【发布时间】:2021-01-17 06:50:15
【问题描述】:

我遇到了一个奇怪的杰克逊问题。 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

【问题讨论】:

    标签: java jackson-dataformat-xml xmlmapper


    【解决方案1】:

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

    xmlMapper.readValue(bytes, ArrayList.class)
    

    然后你得到

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-30
      • 2015-05-23
      • 1970-01-01
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多