Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Commit 599e2e7

Browse files
committed
Fixed #69
1 parent bee4187 commit 599e2e7

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Project: jackson-dataformat-csv
88

99
#66: Deserializing an empty string as an array field return a non-empty list of one empty String
1010
(reported by aksh3ll@github)
11+
#69: SequenceWriter#write(null) writes a single null, not an entire row of nulls
12+
(reported by georgewfraser@github)
1113

1214
2.5.1 (06-Feb-2015)
1315

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,13 @@ public void writeNull() throws IOException
621621
if (!_skipValue) {
622622
if (_arraySeparator >= 0) {
623623
_addToArray(_schema.getNullValue());
624+
} else if (_writeContext.inRoot()) { // as per [#69]
625+
// or, to write 'empty Object' (for common case), would
626+
// write single null, then finish row, like so:
627+
/*
628+
_writer.writeNull(_columnIndex());
629+
finishRow();
630+
*/
624631
} else {
625632
_writer.writeNull(_columnIndex());
626633
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.fasterxml.jackson.dataformat.csv.ser;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.StringWriter;
5+
6+
import com.fasterxml.jackson.databind.*;
7+
import com.fasterxml.jackson.dataformat.csv.*;
8+
9+
// for [dataformat-csv#69]
10+
public class NullWritingTest extends ModuleTestBase
11+
{
12+
private final CsvMapper csv = new CsvMapper();
13+
14+
public static class Nullable {
15+
public String a, b, c, d;
16+
}
17+
18+
public void testObjectWithNullMembersToString() throws Exception {
19+
CsvSchema schema = csv.schemaFor(Nullable.class).withUseHeader(true);
20+
ObjectWriter writer = csv.writer(schema);
21+
String nullMembers = writer.writeValueAsString(new Nullable());
22+
assertEquals("a,b,c,d\n,,,\n", nullMembers);
23+
}
24+
25+
public void testNullToString() throws Exception {
26+
CsvSchema schema = csv.schemaFor(Nullable.class).withUseHeader(true);
27+
ObjectWriter writer = csv.writer(schema);
28+
String nullObject = writer.writeValueAsString(null);
29+
assertEquals("a,b,c,d\n", nullObject);
30+
}
31+
32+
public void testObjectWithNullMembersToStream() throws Exception {
33+
CsvSchema schema = csv.schemaFor(Nullable.class).withUseHeader(true);
34+
ObjectWriter writer = csv.writer(schema);
35+
36+
// Write an object with null members
37+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
38+
SequenceWriter writeValues = writer.writeValues(stream);
39+
writeValues.write(new Nullable());
40+
writeValues.write(new Nullable());
41+
writeValues.flush();
42+
String nullMembers = stream.toString("UTF-8");
43+
assertEquals("a,b,c,d\n,,,\n,,,\n", nullMembers);
44+
}
45+
46+
public void testNullToStream() throws Exception {
47+
CsvSchema schema = csv.schemaFor(Nullable.class).withUseHeader(true);
48+
ObjectWriter writer = csv.writer(schema);
49+
50+
// Write a null value
51+
StringWriter sw = new StringWriter();
52+
SequenceWriter writeValues = writer.writeValues(sw);
53+
writeValues.write(null);
54+
writeValues.write(null);
55+
writeValues.flush();
56+
String nullObject = sw.toString();
57+
/* 11-Feb-2015, tatu: Two ways to go; either nulls get ignored, or they trigger serialization of
58+
* empty Object. For now, former occurs:
59+
*/
60+
61+
assertEquals("a,b,c,d\n", nullObject);
62+
// assertEquals("a,b,c,d\n\n\n", nullObject);
63+
}
64+
}

0 commit comments

Comments
 (0)