Menu

#120 Add option to ignore empty lines to CsvToBeanBuilder / CsvToBean

closed
None
5
2020-03-02
2020-01-08
Martin
No

It happens that I have to parse CSV files which contain one (or a few more) empty line(s) (usually at the end of a file).
Opencsv 5.0 throws a CsvRequiredFieldEmptyException in HeaderNameBaseMappingStrategy#verifyLineLength (invoked by AbstractMappingStrategy#populateNewBean invoked by ProcessCsvLine#processLine) if it stumbles over an empty line.

My current solution add a Filter to CsvToBeanBuilder which ignores empty lines. (see snipplet below)

Drawbacks:

  • CsvToBeanBuilder#withFilter is deprecated in opencsv 5.0
  • CsvToBeanFilter deprecated in opencsv 5.0
  • It might be a useful feature also for other people to ignore empty lines.

Proposal/Request:
Please add an option #withIgnoreEmptyLine() to CsvToBeanBuilder, potentially also to other CsvReaders, which allows to activate similar behaviour with one easy API function.

new CsvToBeanBuilder(reader)
    .withType(MyBeanType.class)
    .withFilter(new CsvToBeanFilter() {
        // This filter ignores empty lines from the input
        @Override
        public boolean allowLine(String[] strings) {
            for (String one : strings) {
                if (one != null && one.length() > 0) {
                    return true;
                }
            }
            return false;
        }
    }).build().parse();

Discussion

  • Scott Conway

    Scott Conway - 2020-02-01
    • status: open --> pending
    • assigned_to: Scott Conway
     
  • Scott Conway

    Scott Conway - 2020-02-01

    Hello Martin.

    Sorry to take so long but life has been pretty busy so far this year :)

    I went ahead and added and merged a withIgnoreEmptyLines to the CsvToBeanBuilder so try it out.

    I will go ahead and undeprecate the CsvToBeanFilter. When we created the BeanVerifier we honestly thought it would handle all the use cases that the CsvToBeanFilter was doing but you showed us the one we missed and even though the functionality is now in the CsvToBean who knows what else is out there.

    Scott :)

     
    👍
    1
  • Scott Conway

    Scott Conway - 2020-03-02
    • status: pending --> closed
     

Log in to post a comment.