Parse CSV and map to a given class.
Parser parser = new Parser(); List<MyClass> results = parser.parse(file, MyClass.class);
Mark first row as header
parser.firstRowIsHeader();
Set Delimiter
parser.delimiter(',');
Note: Default is comma
Annotate private member variables with @CSVColumn. Set value equal to the name of the column as viewed in Excel
@CSVColumn("A")
Defining class map
public class MyClass {
@CSVColumn("A")
private String firstName;
@CSVColumn("B")
private String lastName;
@CSVColumn("C")
private int age;
// Getters & Setters
}
Converts value to Date
@CSVDate(pattern = "MM-dd-yyyy")
Data types supported
Format used by java.text.SimpleDateFormat
This library will automatically map CSV columns to an Object's annotated value.
Example:
| A | B | C |
|---|---|---|
| Alex | Ljunggren | 40 |
Will map to:
public class MyClass {
@CSVColumn("A")
private String firstName;
@CSVColumn("B")
private String lastName;
@CSVColumn("C")
private int age;
// Getters & Setters
}
In the event the code may need to normalize a CSV or swap values on the fly, a column mapping can be injected.
Example: In the CSV below, the age has moved from column C to column A.
| A | B | C |
|---|---|---|
| 40 | Alex | Ljunggren |
In order to map to MyClass, the following will need to be injected:
Map<String, String> map = new HashMap<>();
map.put("A", "B"); // MyClass column A to CSV column B
map.put("B", "C"); // MyClass column B to CSV column C
map.put("C", "A"); // MyClass column C to CSV column A
And instantiated with:
parser.parse(file, MyClass.class).columnMap(map);