4

Using the OpenCSV library, calling StatefulBeanToCsv.write() my null values are being wrapped in quotes.

Example:

String[] columns = new String[] {
    "Col1",
    "Col2",
    "Col3"
};

ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setColumnMapping(columns);

Writer writer = new FileWriter(outputFilePath);
StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer)
        .withMappingStrategy(strat)
        .build();
beanToCsv.write(items);
writer.close();

will produce:

1,"",3

When I expect:

"1",,"3"

I have not set quotes to all fields via .withApplyQuotesToAll(true).

If I do use .withApplyQuotesToAll(true), I end up with

"1","","3"

At one point, it appears the library the opposite of this:

OpenCSV CSVWriter does not add quote character for null element

How can I null values written as a blank/empty value, rather than an empty string?

Brett
  • 719
  • 1
  • 10
  • 16
  • I have not used this library before , but it seems as though a simple solution to your problem would be to take each empty string from the gathered parsing, and then strip the quotes from around it before you use the generated values. Another alternative if you are working with an array of strings would be to find empty Strings in the parsed data and then just set the value in the array to null instead of an empty string – ViaTech Jun 30 '18 at 12:53
  • I appreciate the input but this would not be the appropriate way to solve the issue at hand. The change would need to be inside the internals of the OpenCSV library but I am hoping that is not necessary since this is such a common use case. – Brett Jul 01 '18 at 14:33

2 Answers2

2

It looks like the method that you mentioned not calling actually takes a boolean. Have you tried the following?

.withApplyQuotesToAll(false)
Jared Stewart
  • 571
  • 3
  • 10
  • No, I would like all other values (not null) to be quoted, and null values to not be quoted. Calling the builder with `.withApplyQuotesToAll(false)` would result in all values being written without quotes, which isn't acceptable. – Brett Jun 29 '18 at 01:20
  • The example of what you're expecting as output in the original question doesn't have any quotes in it. Can you flesh out the example a bit more to show what you expect? – Jared Stewart Jun 29 '18 at 01:30
  • So ideally you want to see "1",,"3". You want quotes around everything EXCEPT null values? – Scott Conway Jun 29 '18 at 19:46
  • Yes, Scott, I would like "1",,"3". I've updated the issue to make that clear. – Brett Jun 30 '18 at 12:04
0

There is a way to do that. Setting .withApplyQuotesToAll(false) tells OpenCSV to only quote elements that has special characters, but we can change what OpenCSV understands by that, extending CSVWrite class like this:

public class CustomCsvWriter extends CSVWriter {

    public CustomCsvWriter(Writer writer) {
        super(writer);
    }

    @Override
    protected boolean stringContainsSpecialCharacters(String line) {
        return !line.isEmpty();
     }
}

So, you can create a StatefulBeanToCsv like this:

new StatefulBeanToCsvBuilder<>(new CustomCsvWriter(writer))
   .withMappingStrategy(strat)
   .withApplyQuotesToAll(false)
   .build();

Tested with OpenCSV 5.3

Sivelli
  • 61
  • 1
  • 1