Home » Java » Generating a CSV with Jackson, no Quoted Char

Generating a CSV with Jackson, no Quoted Char

Posted by: admin December 28, 2021 Leave a comment

Questions:

I am using Jackson CSV to generate a CSV file, but I want to have the quote everywhere. I couldn’t find any information on the ApiDoc.

CSV writer

CsvMapper mapper = new CsvMapper();
//objects is a list
CsvSchema schema = mapper.schemaFor(objects).withHeader();
schema = schema.withQuoteChar('\"');

Expected Output

"name","value"
"fieldName1","5"
"fieldName2","2"
"fieldName3","5"

Actual Output

name,value
fieldName1,5
fieldName2,2
fieldName3,5

pom.xml

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-csv</artifactId>
        <version>2.6.3</version>
    </dependency>

If it’s not possible with Jackson I am open to try another library.
Thanks.

Answers:

Ok I finally found it by looking at the ApiDoc and searching with Google on how I could put it all together.
Here the solution to enabling the feature that I wanted:

 mapper.configure(CsvGenerator.Feature.ALWAYS_QUOTE_STRINGS, true);