Menu

#175 StatefulBeanToCsv wraps null values in quotes

v1.0 (example)
closed-invalid
None
5
2018-09-28
2018-06-28
Brett
No

When calling StatefulBeanToCsv.write(), empty strings are written as ,"", rather than ,,

. Looking at a post by Conway in 2017, this shouldn't be the case:

https://stackoverflow.com/questions/44708379/opencsv-csvwriter-does-not-add-quote-character-for-null-element

I don't see anything in the documentation that shows how to produce empty/blanks for null values (e.g. ,,) other than setting ".withApplyQuotesToAll()" with false, which would not be a desireable solution.

Discussion

  • Scott Conway

    Scott Conway - 2018-06-29

    Hello Bret - This is Conway <bg>. I think you misread the post because by default empty strings are surrounded by double quotes and null values are not. That is how opencsv differentiates between the two values when reading them afterwards. </bg>

    In 4.2 you can create a CSVParserBuilder and control what is considered a null field but your would still need the applyQuotesToAll as false as seen in this test from CSVParserWriterSpec.

    ~~~
    @Unroll
    def 'withFieldAsNull writes correctly with the CSVParserWriter #nullFieldIndicator'(CSVReaderNullFieldIndicator nullFieldIndicator, String expectedResult) {
    given:
    String[] values = ["First", null, "", "Last"]

        ICSVParser csvParser = csvParserBuilder
                .withFieldAsNull(nullFieldIndicator)
                .build()
        StringWriter csvParserStringWriter = new StringWriter(INITIAL_SIZE)
        CSVWriterBuilder csvParserWriterBuilder = new CSVWriterBuilder(csvParserStringWriter)
        ICSVWriter csvParserWriter = csvParserWriterBuilder.withParser(csvParser).build()
    
        expect:
    
        csvParserWriter.writeNext(values, false)
        csvParserStringWriter.toString() == expectedResult
        csvParserStringWriter.toString().length() == expectedResult.length()
    
        where:
        nullFieldIndicator                           | expectedResult
        CSVReaderNullFieldIndicator.EMPTY_QUOTES     | "First,\"\",,Last\n"
        CSVReaderNullFieldIndicator.EMPTY_SEPARATORS | "First,,,Last\n"
        CSVReaderNullFieldIndicator.BOTH             | "First,,,Last\n"
        CSVReaderNullFieldIndicator.NEITHER          | "First,null,,Last\n"
    
    }
    

    ~~~

    I am curious as to why you feel the applyQuotesToAll is not desireable.  When set to false only items that Need quotes will have them - for RFC4180 only fields that contain doublequotes or a separator or for the default parser doublequotes, separator or the escape character.
    
    Scott :)
    
     
  • Scott Conway

    Scott Conway - 2018-06-29
    • assigned_to: Scott Conway
     
  • Andrew Rucker Jones

    • status: open --> closed-invalid
     
  • Andrew Rucker Jones

    Closed for lack of a response.

     

Log in to post a comment.

MongoDB Logo MongoDB