Skip to content

Commit

Permalink
(csv) Support coercion of empty strings to null objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Carpenter-Rivers committed Oct 1, 2019
1 parent 770ef84 commit 1532adb
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ public enum Feature
* Feature is disabled by default.
*/
INSERT_NULLS_FOR_MISSING_COLUMNS(false),

/**
* Feature that enables coercing an empty {@link String} to `null`
*
* Feature is disabled by default
*/
EMPTY_STRING_AS_NULL(false)
;

final boolean _defaultState;
Expand Down Expand Up @@ -1056,6 +1063,9 @@ public String getText() throws IOException {
if (_currToken == JsonToken.FIELD_NAME) {
return _currentName;
}
if (_currentValue.equals("")) {
return isEnabled(CsvParser.Feature.EMPTY_STRING_AS_NULL) ? null : _currentValue;
}
return _currentValue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.fasterxml.jackson.dataformat.csv.deser;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvParser;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.*;

/**
* Test for {@link CsvParser.Feature#EMPTY_STRING_AS_NULL}
*/
public class EmptyStringAsNullTest {


@JsonPropertyOrder({"firstName", "middleName", "lastName"})
static class TestUser {
public String firstName, middleName, lastName;
}

@Test
public void givenFeatureDisabledByDefault_whenColumnIsEmptyString_thenParseAsEmptyString() throws IOException {
// setup test data
TestUser expectedTestUser = new TestUser();
expectedTestUser.firstName = "Grace";
expectedTestUser.middleName = "";
expectedTestUser.lastName = "Hopper";
CsvMapper csvMapper = CsvMapper.builder().build();
ObjectReader objectReader = csvMapper.readerFor(TestUser.class).with(csvMapper.schemaFor(TestUser.class));
String csv = "Grace,,Hopper";

// execute
TestUser actualTestUser = objectReader.readValue(csv);

// test
assertNotNull(actualTestUser);
assertEquals(expectedTestUser.firstName, actualTestUser.firstName);
assertEquals(expectedTestUser.middleName, actualTestUser.middleName);
assertEquals(expectedTestUser.lastName, actualTestUser.lastName);
}

@Test
public void givenFeatureEnabled_whenColumnIsEmptyString_thenParseAsNull() throws IOException {
// setup test data
TestUser expectedTestUser = new TestUser();
expectedTestUser.firstName = "Grace";
expectedTestUser.lastName = "Hopper";
CsvMapper csvMapper = CsvMapper.builder().enable(CsvParser.Feature.EMPTY_STRING_AS_NULL).build();
ObjectReader objectReader = csvMapper.readerFor(TestUser.class).with(csvMapper.schemaFor(TestUser.class));
String csv = "Grace,,Hopper";

// execute
TestUser actualTestUser = objectReader.readValue(csv);

// test
assertNotNull(actualTestUser);
assertEquals(expectedTestUser.firstName, actualTestUser.firstName);
assertNull("The column that contains an empty String should be deserialized as null ", actualTestUser.middleName);
assertEquals(expectedTestUser.lastName, actualTestUser.lastName);
}
}

0 comments on commit 1532adb

Please sign in to comment.