Shubham A.
Shubham A.

Reputation: 2534

Parsing double quote with new line from CSV using jackson-dataformat-csv

I have the following CSV format:

id,name,description,age
23,Anna,"Self-made
Chef
Shoemaker",23

The double-quotes are only present if the attribute is multi-line. While I am already been able to read the normal CSV correctly:

@Bean
public CsvMapper csvMapper() {
    CsvMapper csvMapper = new CsvMapper();
    csvMapper.registerModule(new JavaTimeModule());
    csvMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    return csvMapper;
}

I tried adding a new feature:

csvMapper.configure(Feature.FAIL_ON_MISSING_COLUMNS, true);

But it makes the library skip failed rows. How do I parse the given format and get the whole Self-made\n Chef\n Shoemaker into the description attribute?

Upvotes: 2

Views: 887

Answers (1)

Stephen C
Stephen C

Reputation: 718986

According to my reading of the code in the CSVDecoder class (link), the Jackson CSV parser in version 2.14 will correctly parse double-quoted strings that have embedded line-breaks. Look for the _nextQuotedString method. No attribute needs to be set to enable this. It happens unconditionally.

Indeed, it looks like the relevant lines of the code have not changed in at least 5 years.

So ... if the CSV parser is not working for you (without that Feature), my diagnosis is:

  • either you are using a really old version of Jackson,
  • or the apparently incomplete values you are seeing are injected somewhere / somehow after CSV parsing.

If this doesn't solve your problem, please add a minimal reproducible example to the Question, and tell us the version(s) of the Jackson dependencies you are using.

Upvotes: 0

Related Questions