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

Commit 788e437

Browse files
committed
Fix #45
1 parent a71c990 commit 788e437

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

release-notes/VERSION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Version: 2.4.0 (xx-xxx-2014)
66
#32: Allow disabling of quoteChar
77
(suggested by Jason D)
88
#40: Allow (re)ordering of columns of Schema, using `CsvSchema.sortedBy(...)`
9-
#45: Change Default Outputting Behavior to Include Final Comma Regardless of Value.
9+
#45: Change outputting behavior to include final commas even if values are
10+
missing; also add `CsvGenerator.OMIT_MISSING_TAIL_COLUMNS`
1011

1112
------------------------------------------------------------------------
1213
=== History: ===

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ public enum Feature {
3939
* @since 2.4
4040
*/
4141
STRICT_CHECK_FOR_QUOTING(false),
42+
43+
/**
44+
* Feature that determines whether columns without matching value may be omitted,
45+
* when they are the last values of the row.
46+
* If <code>true</code>, values and separators between values may be omitted, to slightly reduce
47+
* length of the row; if <code>false</code>, separators need to stay in place and values
48+
* are indicated by empty Strings.
49+
*
50+
* @since 2.4
51+
*/
52+
OMIT_MISSING_TAIL_COLUMNS(false),
4253
;
4354

4455
protected final boolean _defaultState;

src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvWriter.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,16 @@ public class CsvWriter
6464
/**
6565
* Marker flag used to determine if to do optimal (aka "strict") quoting
6666
* checks or not (looser conservative check)
67+
*
68+
* @since 2.4
6769
*/
6870
protected boolean _cfgOptimalQuoting;
69-
71+
72+
/**
73+
* @since 2.4
74+
*/
75+
protected boolean _cfgIncludeMissingTail;
76+
7077
/*
7178
/**********************************************************
7279
/* Output state
@@ -142,6 +149,7 @@ public CsvWriter(IOContext ctxt, int csvFeatures, Writer out, CsvSchema schema)
142149
_ioContext = ctxt;
143150
_csvFeatures = csvFeatures;
144151
_cfgOptimalQuoting = CsvGenerator.Feature.STRICT_CHECK_FOR_QUOTING.enabledIn(csvFeatures);
152+
_cfgIncludeMissingTail = !CsvGenerator.Feature.OMIT_MISSING_TAIL_COLUMNS.enabledIn(_csvFeatures);
145153

146154
_outputBuffer = ctxt.allocConcatBuffer();
147155
_bufferRecyclable = true;
@@ -174,6 +182,7 @@ public CsvWriter(IOContext ctxt, int csvFeatures, Writer out,
174182
_ioContext = ctxt;
175183
_csvFeatures = csvFeatures;
176184
_cfgOptimalQuoting = CsvGenerator.Feature.STRICT_CHECK_FOR_QUOTING.enabledIn(csvFeatures);
185+
_cfgIncludeMissingTail = !CsvGenerator.Feature.OMIT_MISSING_TAIL_COLUMNS.enabledIn(_csvFeatures);
177186

178187
_outputBuffer = ctxt.allocConcatBuffer();
179188
_bufferRecyclable = true;
@@ -198,6 +207,7 @@ public CsvWriter(CsvWriter base, CsvSchema newSchema)
198207
_ioContext = base._ioContext;
199208
_csvFeatures = base._csvFeatures;
200209
_cfgOptimalQuoting = base._cfgOptimalQuoting;
210+
_cfgIncludeMissingTail = base._cfgIncludeMissingTail;
201211

202212
_outputBuffer = base._outputBuffer;
203213
_bufferRecyclable = base._bufferRecyclable;
@@ -356,9 +366,11 @@ public void endRow() throws IOException
356366
}
357367
// Any missing values?
358368
if (_nextColumnToWrite < _columnCount) {
359-
do {
360-
appendColumnSeparator();
361-
} while (++_nextColumnToWrite < _columnCount);
369+
if (_cfgIncludeMissingTail) {
370+
do {
371+
appendColumnSeparator();
372+
} while (++_nextColumnToWrite < _columnCount);
373+
}
362374
}
363375
// write line separator
364376
_nextColumnToWrite = 0;

src/test/java/com/fasterxml/jackson/dataformat/csv/TestWriterWithMissingValues.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,19 @@ public void testWrite_NullSecondColumn() throws JsonProcessingException {
4545
}
4646

4747
@Test
48-
public void testWrite_NullThirdColumn() throws JsonProcessingException {
49-
final String csv = WRITER.writeValueAsString(
48+
public void testWrite_NullThirdColumn() throws JsonProcessingException
49+
{
50+
CsvMapper mapper = new CsvMapper();
51+
assertFalse(mapper.getFactory().isEnabled(CsvGenerator.Feature.OMIT_MISSING_TAIL_COLUMNS));
52+
String csv = mapper.writer().withSchema(SCHEMA).writeValueAsString(
5053
ImmutableMap.of("timestamp", "2014-03-10T23:32:47+00:00",
5154
"value", 42));
5255

5356
assertEquals("\"2014-03-10T23:32:47+00:00\",42,\n", csv);
57+
mapper.getFactory().enable(CsvGenerator.Feature.OMIT_MISSING_TAIL_COLUMNS);
58+
csv = mapper.writer().withSchema(SCHEMA).writeValueAsString(
59+
ImmutableMap.of("timestamp", "2014-03-10T23:32:47+00:00",
60+
"value", 42));
61+
assertEquals("\"2014-03-10T23:32:47+00:00\",42\n", csv);
5462
}
5563
}

0 commit comments

Comments
 (0)