Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating a CSV with Jackson, no Quoted Char

Tags:

java

csv

jackson

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.

like image 618
Maurice Avatar asked Nov 17 '15 08:11

Maurice


1 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);
like image 127
Maurice Avatar answered Sep 28 '22 01:09

Maurice