Skip to content

Commit 9412e00

Browse files
committed
Allow to use CSVFormat.withMissingColumnValuesAreNull().
1 parent 4422a8b commit 9412e00

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,25 @@ private void addRecordValue(final boolean lastRecord) {
367367
if (lastRecord && inputClean.isEmpty() && this.format.getTrailingDelimiter()) {
368368
return;
369369
}
370-
final String nullString = this.format.getNullString();
371-
this.record.add(inputClean.equals(nullString) ? null : inputClean);
370+
this.record.add(isNull(inputClean) ? null : inputClean);
371+
}
372+
373+
private boolean isNull(final String input) {
374+
Assertions.notNull(input, "input");
375+
376+
if (input.equals(this.format.getNullString())) {
377+
return true;
378+
}
379+
380+
if (!this.format.getMissingColumnValuesAreNull()) {
381+
return false; // MUST keep backward compatibility
382+
}
383+
384+
if (this.reusableToken.isQuoted) {
385+
return false; // distinguish quoted strings from null
386+
}
387+
388+
return input.isEmpty();
372389
}
373390

374391
/**

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,47 @@ public void testTrim() throws Exception {
961961
Assert.assertEquals(3, record.size());
962962
}
963963

964+
@Test
965+
public void testMissingColumnValuesAreNull() throws Exception {
966+
final String code = ",,\"\",";
967+
968+
// check backward compatibility
969+
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) {
970+
final CSVRecord record = parser.iterator().next();
971+
972+
assertNotNull(record.get(0));
973+
assertNotNull(record.get(1));
974+
assertNotNull(record.get(2));
975+
assertNotNull(record.get(3));
976+
Assert.assertEquals(4, record.size());
977+
}
978+
979+
// check withMissingColumnValuesAreNull
980+
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT
981+
.withMissingColumnValuesAreNull())) {
982+
final CSVRecord record = parser.iterator().next();
983+
984+
assertNull(record.get(0));
985+
assertNull(record.get(1));
986+
assertNotNull(record.get(2));
987+
assertNull(record.get(3));
988+
Assert.assertEquals(4, record.size());
989+
}
990+
991+
// check combination of withNullString and withMissingColumnValuesAreNull
992+
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT
993+
.withNullString("")
994+
.withMissingColumnValuesAreNull())) {
995+
final CSVRecord record = parser.iterator().next();
996+
997+
assertNull(record.get(0));
998+
assertNull(record.get(1));
999+
assertNull(record.get(2));
1000+
assertNull(record.get(3));
1001+
Assert.assertEquals(4, record.size());
1002+
}
1003+
}
1004+
9641005
private void validateLineNumbers(final String lineSeparator) throws IOException {
9651006
try (final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c",
9661007
CSVFormat.DEFAULT.withRecordSeparator(lineSeparator))) {

0 commit comments

Comments
 (0)