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

Commit

Permalink
Fix #90
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 23, 2015
1 parent 73879b8 commit 92a070b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
5 changes: 5 additions & 0 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ sothmann@github)

* Reported #83: Serializing List with null values leads to corrupt CSV
(2.6.0)

Jonathan Cheseaux (cheseaux@github)

* Reported #90: Unexpected output with arrays starting with a null/empty element
(2.6.4)
5 changes: 5 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Project: jackson-dataformat-csv
=== Releases ===
------------------------------------------------------------------------

2.6.4 (not yet released)

#90: Unexpected output with arrays starting with a null/empty element
(reported by Jonathan C)

2.6.3 (12-Oct-2015)

#91: Ensure that "too many columns" is recoverable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,19 @@ private Feature(boolean defaultState) {
*/
protected int _arraySeparator = -1;

/**
* Accumulated contents of an array cell, if any
*/
protected StringBuilder _arrayContents;

/**
* Additional counter that indicates number of value entries in the
* array. Needed because `null` entries do not add content, but need
* to be separated by array cell separator
*
* @since 2.7
*/
protected int _arrayElements;

/*
/**********************************************************
Expand Down Expand Up @@ -424,6 +436,7 @@ public final void writeStartArray() throws IOException
} else {
_arrayContents.setLength(0);
}
_arrayElements = 0;
}
} else if (_arraySeparator >= 0) {
// also: no nested arrays, yet
Expand Down Expand Up @@ -863,16 +876,18 @@ protected void _handleFirstLine() throws IOException
}

protected void _addToArray(String value) {
if (_arrayContents.length() > 0) {
if (_arrayElements > 0) {
_arrayContents.append((char) _arraySeparator);
}
++_arrayElements;
_arrayContents.append(value);
}

protected void _addToArray(char[] value) {
if (_arrayContents.length() > 0) {
if (_arrayElements > 0) {
_arrayContents.append((char) _arraySeparator);
}
++_arrayElements;
_arrayContents.append(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public void testIgnoringOptionalTrailing() throws Exception
.addColumn("second")
.build();

@SuppressWarnings("resource")
MappingIterator<Map<?,?>> it = mapper.reader(schema).forType(Map.class).readValues(
"a,b\nc,d,\ne,f, \nfoo,bar,x\n");
assertTrue(it.hasNext());
Expand Down Expand Up @@ -59,6 +58,7 @@ public void testIgnoringOptionalTrailing() throws Exception
} catch (JsonParseException e) {
verifyException(e, "Too many entries");
}
it.close();
}

// also ensure [databind-csv#1] also works appropriately for failing case
Expand All @@ -70,7 +70,6 @@ public void testOptionalTrailFailing() throws Exception
.addColumn("second")
.build();

@SuppressWarnings("resource")
MappingIterator<Map<?,?>> it = mapper.reader(schema).forType(Map.class).readValues(
"a,b,\nc,d,,,\n");
assertTrue(it.hasNext());
Expand All @@ -88,5 +87,6 @@ public void testOptionalTrailFailing() throws Exception
} catch (JsonProcessingException e) {
verifyException(e, "Too many entries: expected at most 2");
}
it.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import com.fasterxml.jackson.dataformat.csv.*;

// for [dataformat-csv#57]
// Tests for verifying that it is possible to write "simple" arrays
// (ones with scalar serializations) as CSV
public class ArrayWriteTest extends ModuleTestBase
{
@JsonPropertyOrder({"id", "values", "extra"})
Expand All @@ -19,6 +20,16 @@ public ValueEntry(String id, String extra, int... v) {
}
}

@JsonPropertyOrder({"a", "b", "c"})
static class Pojo90 {

public String[] a = new String[]{"", "foo"};

public String[] b = new String[]{null, "bar"};

public String[] c = new String[]{"baz",null};
}

/*
/**********************************************************************
/* Test methods
Expand Down Expand Up @@ -49,4 +60,17 @@ public void testSeparatorOverride() throws Exception
// gets quoted due to white space
assertEquals("foo,\"1 2 3\",stuff", csv);
}

public void testArraysWithNulls() throws Exception
{
Pojo90 value = new Pojo90();
CsvMapper mapper = mapperForCsv();
String csvContent = mapper.writer(mapper.schemaFor(Pojo90.class)
.withHeader())
.writeValueAsString(value);
String[] lines = csvContent.split("\\n");
assertEquals(2, lines.length);
assertEquals("a,b,c", lines[0]);
assertEquals(";foo,;bar,baz;", lines[1]);
}
}

0 comments on commit 92a070b

Please sign in to comment.