CSV Parser
| src | 6 months ago | ||
| .gitignore | 5 years ago | ||
| Jenkinsfile | 2 years ago | ||
| README.md | 6 months ago | ||
| lombok.config | 5 years ago | ||
| pom.xml | 6 months ago | ||
A small, fluent CSV parser for Java that maps rows to either:
List<List<String>> rows,List<String>, or@CSVColumn.Built on top of Apache Commons CSV.
"A", "B", "AA", ...)String-constructor mapper)@CSVDate(pattern = "...")CsvParseException with the original cause preservedThe project is published to the Ljunggren Nexus repository. Add the dependency:
<dependency>
<groupId>io.ljunggren</groupId>
<artifactId>csv-parser</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Annotate the POJO fields with @CSVColumn. The value is the column reference in the CSV, using either Excel-style letters ("A", "B", …) or a numeric zero-based index.
import io.ljunggren.csv.parser.annotation.CSVColumn;
public class Person {
@CSVColumn("A") private Long id;
@CSVColumn("B") private String firstName;
@CSVColumn("C") private String lastName;
@CSVColumn("D") private int age;
// getters & setters
}
Parser parser = new Parser().firstRowIsHeader(); List<Person> people = parser.parse(file, Person.class);
List<List<String>> rows = new Parser().parse(file);
List<String>List<String> ids = new Parser().parse(file, String.class);
List<String> headers = new Parser().firstRowIsHeader().parseHeaders(file);
Parser parser = new Parser()
.delimiter('|') // default is ','
.firstRowIsHeader() // skip the first row
.columnMap(remapping); // optional column remapping
columnMap translates the column reference declared on the POJO to the actual column reference in the file. The map is defensively copied; mutating the source map after passing it to columnMap(...) has no effect on the parser. The getter, getColumnMap(), returns an unmodifiable view.
CSV (columns out of order):
| A | B | C | D | E |
|---|---|---|---|---|
| US | Indiana | English | Alex | 1 |
POJO declares id at column "A", name at column "B", state at "D". To make the POJO line up with the file, supply:
Map<String, String> remap = new HashMap<>();
remap.put("A", "E"); // POJO "A" reads file column E
remap.put("B", "D"); // POJO "B" reads file column D
remap.put("D", "B"); // POJO "D" reads file column B
new Parser().firstRowIsHeader().columnMap(remap).parse(file, Person.class);
Annotate Date fields with @CSVDate(pattern = "..."). The pattern is interpreted by java.text.SimpleDateFormat in strict (non-lenient) mode, so malformed values fail fast.
import io.ljunggren.csv.parser.annotation.CSVDate;
public class Order {
@CSVColumn("A") private Long id;
@CSVColumn("B") @CSVDate(pattern = "MM-dd-yyyy") private Date placedOn;
}
The mapper chain runs in this order:
DateMapper — handles fields annotated with @CSVDate.CatchAllMapper — for everything else, invokes the field type's T(String) constructor (primitive types are converted via their wrapper).Any field type that exposes a single-argument String constructor is supported out of the box: String, Integer, Long, Double, BigDecimal, Boolean, etc.
If you need to support a type without a String constructor (e.g. an enum, LocalDate, UUID), implement your own MapperChain subclass — see the io.ljunggren.csv.parser.mapper package.
All parsing failures are wrapped in io.ljunggren.csv.parser.exception.CsvParseException, which is an unchecked exception. The original cause (e.g. IOException, ParseException, NoSuchMethodException) is always preserved.
try {
new Parser().firstRowIsHeader().parse(file, Person.class);
} catch (CsvParseException e) {
log.error("could not parse {}: {}", file, e.getMessage(), e);
}
Argument validation: passing null for the file or class throws NullPointerException immediately.
Parser instances are not thread-safe — each thread should construct its own instance. A single instance can be reused sequentially across multiple parse(...) calls.