diff --git a/release-notes/VERSION b/release-notes/VERSION index 3527664..427165c 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -8,6 +8,8 @@ Project: jackson-dataformat-csv #66: Deserializing an empty string as an array field return a non-empty list of one empty String (reported by aksh3ll@github) +#69: SequenceWriter#write(null) writes a single null, not an entire row of nulls + (reported by georgewfraser@github) 2.5.1 (06-Feb-2015) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvGenerator.java b/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvGenerator.java index aa157aa..8380ef8 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvGenerator.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvGenerator.java @@ -621,6 +621,13 @@ public void writeNull() throws IOException if (!_skipValue) { if (_arraySeparator >= 0) { _addToArray(_schema.getNullValue()); + } else if (_writeContext.inRoot()) { // as per [#69] + // or, to write 'empty Object' (for common case), would + // write single null, then finish row, like so: + /* + _writer.writeNull(_columnIndex()); + finishRow(); +*/ } else { _writer.writeNull(_columnIndex()); } diff --git a/src/test/java/com/fasterxml/jackson/dataformat/csv/ser/NullWritingTest.java b/src/test/java/com/fasterxml/jackson/dataformat/csv/ser/NullWritingTest.java new file mode 100644 index 0000000..0bd24a2 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/dataformat/csv/ser/NullWritingTest.java @@ -0,0 +1,64 @@ +package com.fasterxml.jackson.dataformat.csv.ser; + +import java.io.ByteArrayOutputStream; +import java.io.StringWriter; + +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.dataformat.csv.*; + +// for [dataformat-csv#69] +public class NullWritingTest extends ModuleTestBase +{ + private final CsvMapper csv = new CsvMapper(); + + public static class Nullable { + public String a, b, c, d; + } + + public void testObjectWithNullMembersToString() throws Exception { + CsvSchema schema = csv.schemaFor(Nullable.class).withUseHeader(true); + ObjectWriter writer = csv.writer(schema); + String nullMembers = writer.writeValueAsString(new Nullable()); + assertEquals("a,b,c,d\n,,,\n", nullMembers); + } + + public void testNullToString() throws Exception { + CsvSchema schema = csv.schemaFor(Nullable.class).withUseHeader(true); + ObjectWriter writer = csv.writer(schema); + String nullObject = writer.writeValueAsString(null); + assertEquals("a,b,c,d\n", nullObject); + } + + public void testObjectWithNullMembersToStream() throws Exception { + CsvSchema schema = csv.schemaFor(Nullable.class).withUseHeader(true); + ObjectWriter writer = csv.writer(schema); + + // Write an object with null members + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + SequenceWriter writeValues = writer.writeValues(stream); + writeValues.write(new Nullable()); + writeValues.write(new Nullable()); + writeValues.flush(); + String nullMembers = stream.toString("UTF-8"); + assertEquals("a,b,c,d\n,,,\n,,,\n", nullMembers); + } + + public void testNullToStream() throws Exception { + CsvSchema schema = csv.schemaFor(Nullable.class).withUseHeader(true); + ObjectWriter writer = csv.writer(schema); + + // Write a null value + StringWriter sw = new StringWriter(); + SequenceWriter writeValues = writer.writeValues(sw); + writeValues.write(null); + writeValues.write(null); + writeValues.flush(); + String nullObject = sw.toString(); + /* 11-Feb-2015, tatu: Two ways to go; either nulls get ignored, or they trigger serialization of + * empty Object. For now, former occurs: + */ + + assertEquals("a,b,c,d\n", nullObject); +// assertEquals("a,b,c,d\n\n\n", nullObject); + } +}