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

Commit

Permalink
Fixed #69
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 12, 2015
1 parent bee4187 commit 599e2e7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 599e2e7

Please sign in to comment.