Skip to content

Commit d8cf88e

Browse files
committed
Merge branch '2.11' of github.com:FasterXML/jackson-dataformats-text into 2.11
2 parents fa2ce20 + 25e04a0 commit d8cf88e

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ public enum Feature
132132
* @since 2.9
133133
*/
134134
INSERT_NULLS_FOR_MISSING_COLUMNS(false),
135+
136+
/**
137+
* Feature that enables coercing an empty {@link String} to `null`
138+
*
139+
* Feature is disabled by default
140+
*/
141+
EMPTY_STRING_AS_NULL(false)
135142
;
136143

137144
final boolean _defaultState;
@@ -1106,6 +1113,9 @@ public String getText() throws IOException {
11061113
if (_currToken == JsonToken.FIELD_NAME) {
11071114
return _currentName;
11081115
}
1116+
if (_currentValue.equals("")) {
1117+
return isEnabled(CsvParser.Feature.EMPTY_STRING_AS_NULL) ? null : _currentValue;
1118+
}
11091119
return _currentValue;
11101120
}
11111121

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.fasterxml.jackson.dataformat.csv.deser;
2+
3+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.databind.ObjectReader;
6+
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
7+
import com.fasterxml.jackson.dataformat.csv.CsvParser;
8+
import org.junit.Test;
9+
10+
import java.io.IOException;
11+
12+
import static org.junit.Assert.*;
13+
14+
/**
15+
* Test for {@link CsvParser.Feature#EMPTY_STRING_AS_NULL}
16+
*/
17+
public class EmptyStringAsNullTest {
18+
19+
20+
@JsonPropertyOrder({"firstName", "middleName", "lastName"})
21+
static class TestUser {
22+
public String firstName, middleName, lastName;
23+
}
24+
25+
@Test
26+
public void givenFeatureDisabledByDefault_whenColumnIsEmptyString_thenParseAsEmptyString() throws IOException {
27+
// setup test data
28+
TestUser expectedTestUser = new TestUser();
29+
expectedTestUser.firstName = "Grace";
30+
expectedTestUser.middleName = "";
31+
expectedTestUser.lastName = "Hopper";
32+
CsvMapper csvMapper = CsvMapper.builder().build();
33+
ObjectReader objectReader = csvMapper.readerFor(TestUser.class).with(csvMapper.schemaFor(TestUser.class));
34+
String csv = "Grace,,Hopper";
35+
36+
// execute
37+
TestUser actualTestUser = objectReader.readValue(csv);
38+
39+
// test
40+
assertNotNull(actualTestUser);
41+
assertEquals(expectedTestUser.firstName, actualTestUser.firstName);
42+
assertEquals(expectedTestUser.middleName, actualTestUser.middleName);
43+
assertEquals(expectedTestUser.lastName, actualTestUser.lastName);
44+
}
45+
46+
@Test
47+
public void givenFeatureEnabled_whenColumnIsEmptyString_thenParseAsNull() throws IOException {
48+
// setup test data
49+
TestUser expectedTestUser = new TestUser();
50+
expectedTestUser.firstName = "Grace";
51+
expectedTestUser.lastName = "Hopper";
52+
CsvMapper csvMapper = CsvMapper.builder().enable(CsvParser.Feature.EMPTY_STRING_AS_NULL).build();
53+
ObjectReader objectReader = csvMapper.readerFor(TestUser.class).with(csvMapper.schemaFor(TestUser.class));
54+
String csv = "Grace,,Hopper";
55+
56+
// execute
57+
TestUser actualTestUser = objectReader.readValue(csv);
58+
59+
// test
60+
assertNotNull(actualTestUser);
61+
assertEquals(expectedTestUser.firstName, actualTestUser.firstName);
62+
assertNull("The column that contains an empty String should be deserialized as null ", actualTestUser.middleName);
63+
assertEquals(expectedTestUser.lastName, actualTestUser.lastName);
64+
}
65+
}

release-notes/CREDITS-2.x

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,9 @@ Vincent Boulaye (vboulaye@github)
8484
* Implemented #15: Add a `CsvParser.Feature.SKIP_EMPTY_LINES` to allow
8585
skipping empty rows
8686
(2.10.1)
87+
88+
Tyler Carpenter-Rivers (tyler2cr@github)
89+
#7: Add `CsvParser.Feature.EMPTY_STRING_AS_NULL` to allow coercing empty Strings
90+
into `null` values
91+
(2.11.0)
92+

release-notes/VERSION-2.x

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ Modules:
1010

1111
2.11.0 (not yet released)
1212

13-
-
13+
#7: Add `CsvParser.Feature.EMPTY_STRING_AS_NULL` to allow coercing empty Strings
14+
into `null` values
15+
(contributed by Tyler C-R)
1416

1517
2.10.1 (not yet released)
1618

0 commit comments

Comments
 (0)