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

Commit ca2e81b

Browse files
committed
Fix #104
1 parent d2ebc75 commit ca2e81b

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

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

+21-8
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,10 @@ public final void writeStartArray() throws IOException
439439
* can not nest arrays in objects
440440
*/
441441
if (_writeContext.inObject()) {
442-
if (!_skipValue) {
442+
if ((_skipWithin == null)
443+
&& _skipValue && isEnabled(JsonGenerator.Feature.IGNORE_UNKNOWN)) {
444+
_skipWithin = _writeContext;
445+
} else if (!_skipValue) {
443446
// First: column may have its own separator
444447
String sep;
445448

@@ -463,11 +466,12 @@ public final void writeStartArray() throws IOException
463466
}
464467
_arrayElements = 0;
465468
}
466-
} else if (!_arraySeparator.isEmpty()) {
467-
// also: no nested arrays, yet
468-
_reportError("CSV generator does not support nested Array values");
469+
} else {
470+
if (!_arraySeparator.isEmpty()) {
471+
// also: no nested arrays, yet
472+
_reportError("CSV generator does not support nested Array values");
473+
}
469474
}
470-
471475
_writeContext = _writeContext.createChildArrayContext();
472476
// and that's about it, really
473477
}
@@ -478,11 +482,18 @@ public final void writeEndArray() throws IOException
478482
if (!_writeContext.inArray()) {
479483
_reportError("Current context not an ARRAY but "+_writeContext.getTypeDesc());
480484
}
485+
_writeContext = _writeContext.getParent();
486+
// 14-Dec-2015, tatu: To complete skipping of ignored structured value, need this:
487+
if (_skipWithin != null) {
488+
if (_writeContext == _skipWithin) {
489+
_skipWithin = null;
490+
}
491+
return;
492+
}
481493
if (!_arraySeparator.isEmpty()) {
482494
_arraySeparator = CsvSchema.NO_ARRAY_ELEMENT_SEPARATOR;
483495
_writer.write(_columnIndex(), _arrayContents.toString());
484496
}
485-
_writeContext = _writeContext.getParent();
486497
/* 20-Nov-2014, tatu: When doing "untyped"/"raw" output, this means that row
487498
* is now done. But not if writing such an array field, so:
488499
*/
@@ -517,8 +528,10 @@ public final void writeEndObject() throws IOException
517528
}
518529
_writeContext = _writeContext.getParent();
519530
// 14-Dec-2015, tatu: To complete skipping of ignored structured value, need this:
520-
if (_writeContext == _skipWithin) {
521-
_skipWithin = null;
531+
if (_skipWithin != null) {
532+
if (_writeContext == _skipWithin) {
533+
_skipWithin = null;
534+
}
522535
return;
523536
}
524537
// not 100% fool-proof, but chances are row should be done now

src/test/java/com/fasterxml/jackson/dataformat/csv/deser/BasicParserTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -423,5 +423,6 @@ public void testStrictColumnReturnsExpectedData() throws IOException {
423423
assertEquals(7, values.get(2).x);
424424
assertEquals(8, values.get(2).y.intValue());
425425
assertEquals(9, values.get(2).z.intValue());
426+
iter.close();
426427
}
427428
}

src/test/java/com/fasterxml/jackson/dataformat/csv/ser/GeneratorIgnoreUnknownTest.java

+38
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ public static class PointAndStuff {
3030

3131
public PointAndStuff(Object s) { stuff = s; }
3232
}
33+
34+
// for [dataformat-csv#104]
35+
@JsonPropertyOrder({ "x", "points", "y" })
36+
public static class PointAndArray {
37+
public int x = 1, y = 2;
38+
39+
public List<Point> points = new ArrayList<Point>();
40+
{
41+
points.add(new Point());
42+
points.add(new Point());
43+
}
44+
45+
protected PointAndArray() { }
46+
protected PointAndArray(int x, int y) {
47+
this.x = x;
48+
this.y = y;
49+
}
50+
}
3351

3452
/*
3553
/**********************************************************
@@ -89,4 +107,24 @@ public void testIgnoreObject() throws Exception
89107
assertNotNull(csv);
90108
assertEquals("1,2\n", csv);
91109
}
110+
111+
// for [dataformat-csv#104]
112+
public void testIgnoreNested() throws Exception
113+
{
114+
ObjectMapper mapper = mapperForCsv();
115+
final CsvSchema schema = CsvSchema.builder()
116+
.addColumn("x")
117+
.addColumn("y")
118+
.build();
119+
ObjectWriter writer = mapper.writerFor(PointAndArray.class)
120+
.with(schema)
121+
.with(JsonGenerator.Feature.IGNORE_UNKNOWN);
122+
123+
String csv = writer.writeValueAsString(new PointAndArray(3,5));
124+
125+
System.err. println("CSV:\n"+csv);
126+
127+
assertNotNull(csv);
128+
assertEquals("3,5\n", csv);
129+
}
92130
}

0 commit comments

Comments
 (0)