From b249d536f8f48829d5c900f4f9cc8cdab914f21f Mon Sep 17 00:00:00 2001 From: Cowtowncoder Date: Mon, 29 Jun 2015 13:14:16 -0700 Subject: [PATCH] Add more tests for #72; fix an accidental exclusion of empty String for deserialization --- .../jackson/dataformat/csv/CsvParser.java | 2 +- .../jackson/dataformat/csv/CsvSchema.java | 4 +- .../dataformat/csv/deser/NullRead72Test.java | 40 -------- .../dataformat/csv/deser/NullReadTest.java | 98 +++++++++++++++++++ 4 files changed, 101 insertions(+), 43 deletions(-) delete mode 100644 src/test/java/com/fasterxml/jackson/dataformat/csv/deser/NullRead72Test.java create mode 100644 src/test/java/com/fasterxml/jackson/dataformat/csv/deser/NullReadTest.java diff --git a/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java b/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java index eae9df3..5169b61 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java @@ -316,7 +316,7 @@ public void setSchema(FormatSchema schema) if (schema instanceof CsvSchema) { _schema = (CsvSchema) schema; String str = _schema.getNullValueString(); - _nullValue = str.isEmpty() ? null : str; + _nullValue = str; } else if (schema == null) { schema = EMPTY_SCHEMA; } else { diff --git a/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvSchema.java b/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvSchema.java index 7b98533..8649347 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvSchema.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvSchema.java @@ -41,7 +41,7 @@ * *
  • nullValue (String) [default: "" (empty String)]: When asked to write Java `null`, * this String value will be used instead.
    - * NOTE: NOT used for reading at this point (this may change in future) + * With 2.6, value will also be recognized during value reads. *
  • * *

    @@ -545,7 +545,7 @@ public Builder setLineSeparator(char lf) { } public Builder setNullValue(String nvl) { - return setNullValue(nvl.toCharArray()); + return setNullValue((nvl == null) ? null : nvl.toCharArray()); } public Builder setNullValue(char[] nvl) { diff --git a/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/NullRead72Test.java b/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/NullRead72Test.java deleted file mode 100644 index 829ff57..0000000 --- a/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/NullRead72Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fasterxml.jackson.dataformat.csv.deser; - -import com.fasterxml.jackson.databind.*; -import com.fasterxml.jackson.dataformat.csv.*; - -public class NullRead72Test extends ModuleTestBase -{ - final CsvMapper MAPPER = mapperForCsv(); - - // For [dataformat-csv#72]: recognize "null value" for reading too - public void testReadNullValue() throws Exception - { - CsvSchema schema = CsvSchema.builder() - .setNullValue("n/a") - .addColumn("id") - .addColumn("desc") - .build(); - - // start by writing, first - String csv = MAPPER.writer(schema).writeValueAsString(new IdDesc("id", null)); - // MUST use doubling for quotes! - assertEquals("id,n/a\n", csv); - - // but read back - - ObjectReader r = MAPPER.readerFor(IdDesc.class) - .with(schema); - - IdDesc result = r.readValue(csv); - assertNotNull(result); - assertEquals("id", result.id); - assertNull(result.desc); - - // also try the other combination - result = r.readValue("n/a,Whatevs\n"); - assertNotNull(result); - assertNull(result.id); - assertEquals("Whatevs", result.desc); - } -} diff --git a/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/NullReadTest.java b/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/NullReadTest.java new file mode 100644 index 0000000..de3e6cd --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/NullReadTest.java @@ -0,0 +1,98 @@ +package com.fasterxml.jackson.dataformat.csv.deser; + +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.dataformat.csv.*; + +public class NullReadTest extends ModuleTestBase +{ + final CsvMapper MAPPER = mapperForCsv(); + + // For [dataformat-csv#72]: recognize "null value" for reading too + public void testReadNullValue72() throws Exception + { + CsvSchema schema = CsvSchema.builder() + .setNullValue("n/a") + .addColumn("id") + .addColumn("desc") + .build(); + + // start by writing, first + String csv = MAPPER.writer(schema).writeValueAsString(new IdDesc("id", null)); + // MUST use doubling for quotes! + assertEquals("id,n/a\n", csv); + + // but read back + + ObjectReader r = MAPPER.readerFor(IdDesc.class) + .with(schema); + + IdDesc result = r.readValue(csv); + assertNotNull(result); + assertEquals("id", result.id); + assertNull(result.desc); + + // also try the other combination + result = r.readValue("n/a,Whatevs\n"); + assertNotNull(result); + assertNull(result.id); + assertEquals("Whatevs", result.desc); + } + + public void testReadNullValueFromEmptyString() throws Exception + { + // first: empty String should work as default + CsvSchema schemaWithDefault = CsvSchema.builder() + .addColumn("id") + .addColumn("desc") + .build(); + + // start by writing, first + String csv = MAPPER.writer(schemaWithDefault).writeValueAsString(new IdDesc("id", null)); + assertEquals("id,\n", csv); + + // but read back + + ObjectReader r = MAPPER.readerFor(IdDesc.class).with(schemaWithDefault); + + IdDesc result = r.readValue(csv); + assertNotNull(result); + assertEquals("id", result.id); + assertNull(result.desc); + + // also try the other combination + result = r.readValue(",Whatevs\n"); + assertNotNull(result); + assertNull(result.id); + assertEquals("Whatevs", result.desc); + + // And then with explicit Empty String + CsvSchema schemaWithExplicitEmpty = CsvSchema.builder() + .setNullValue("") + .addColumn("id") + .addColumn("desc") + .build(); + + csv = MAPPER.writer(schemaWithExplicitEmpty).writeValueAsString(new IdDesc("id", null)); + assertEquals("id,\n", csv); + r = MAPPER.readerFor(IdDesc.class).with(schemaWithExplicitEmpty); + result = r.readValue(csv); + assertNotNull(result); + assertEquals("id", result.id); + assertNull(result.desc); + + // and finally with explicit `null` + CsvSchema schemaWithExplicitNull = CsvSchema.builder() + .setNullValue((String) null) + .addColumn("id") + .addColumn("desc") + .build(); + + csv = MAPPER.writer(schemaWithExplicitNull).writeValueAsString(new IdDesc("id", null)); + assertEquals("id,\n", csv); + r = MAPPER.readerFor(IdDesc.class).with(schemaWithExplicitNull); + result = r.readValue(csv); + assertNotNull(result); + assertEquals("id", result.id); + assertNull(result.desc); + } +}