I'm using Jackson CSV to parse a CSV file into POJOs. My issue is that if a row in the CSV has too few columns, the parser doesn't complain and just sets the rest of the fields to null.
Parsing code:
CsvMapper csvMapper = new CsvMapper();
csvMapper.addMixInAnnotations(Person.class, PersonCsvMixin.class);
CsvSchema schema = csvMapper.schemaFor(Person.class).withHeader();
MappingIterator<Person> it = csvMapper.reader(dataClass).with(schema).readValues(csv);
LinkedList<Person> output = new LinkedList<>();
while(it.hasNext()) {
output.push(it.next());
}
Mixin:
import com.fasterxml.jackson.annotation.*;
@JsonPropertyOrder(value = { "FirstName", "LastName", "Title"})
public abstract class Person {
@JsonProperty("LastName")
public abstract String getLastName();
@JsonProperty("FirstName")
public abstract String getFirstName();
@JsonProperty("Title")
public abstract String getTitle();
}
Data class:
public class OfficespaceInputEmployee implements Serializable{
protected String firstName;
protected String lastName;
protected String title;
// .. getters and setters
}
If I parse a file like the following, no errors occur even though the middle record is missing two fields. Instead, LastName and Title become null
"FirstName", "LastName", "Title"
"John", "Smith", "Mr"
"Mary"
"Peter", "Jones", "Dr"
Is there a feature to enable that will cause this to error instead?
You can throw an exception yourself when you build the output LinkedList inside the while loop: