Thomas W.
Thomas W.

Reputation: 51

Deserializing CSV with Jackson that might have lines enclosed with double quotes

Suppose I have a CSV file that looks like this

firstName,lastName,address
"John,Doe,"Imaginary St. 42, 1234 Hometown""
Jane,Doe,"Castle St. 1, 5678 Littletown" 

how would I be able to deserialize rows into a objects of the following class

public class PersonRecord {
  private String firstName;
  private String lastName;
  private String address;
}

using jackson-dataformat-csv?

Main challenge here is that lines might be completely enclosed by double quotes which should be ignored, however double-quotes should still be allowed to enclose address values in this case.

The naive solution would be to create a temporary file and strip the first and last character of each line that starts with double quotes. It looks like I can make the assumption that the first column will never be enclosed by quotes so it seems safe to take this shortcut.

However is there a smarter solution to this problem not requiring temporary files? So far I haven't been able to hook into the tokenization process and "update" the object tree for problematic likes so regular object tree processing can continue in a custom JsonDeserializer.

Upvotes: 0

Views: 129

Answers (0)

Related Questions