【问题标题】:jackson CSV allowComments only work for multiple line message?jackson CSV allowComments 仅适用于多行消息?
【发布时间】:2019-10-17 05:19:02
【问题描述】:

示例 1(失败):

final String line = "# 134";  
CsvMapper mapper = mapperForCsv(); 
String[] it = mapper.readerFor(String[].class) 
       .with(mapper.schema().withComments()).readValue(line);

示例 2(成功):

final String line = "# 134";
CsvMapper mapper = mapperForCsv();
MappingIterator<String[]> its = mapper.readerFor(String[].class) 
       .with(mapper.schema().withComments()).readValues(line);

检查代码后,我发现 withComments 仅适用于多行消息。这是错误还是预期?

【问题讨论】:

    标签: java csv jackson comments jackson-databind


    【解决方案1】:

    当你创建MappingIterator 对象时,实际上你还没有反序列化任何东西。您需要遍历所有行或使用its.readAll() 方法开始反序列化过程。

    当您使用readValue 方法时,您期望只有一个对象。如果它不可用,则会抛出异常以通知您它是不可能的。它不能只返回null,因为这将是一个静默失败,并可能在以后导致NPE

    MappingIterator 是一个不同的场景,因为您需要对其进行迭代,而API 允许您检查下一个元素。因此,很明显,如果没有元素,我们就可以停止迭代。

    所以,在我看来,这不是一个错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-09
      • 2017-03-18
      • 1970-01-01
      • 2021-08-21
      • 2016-01-27
      • 2015-10-24
      • 1970-01-01
      • 2019-06-12
      相关资源
      最近更新 更多