jackson-dataformats-text
jackson-dataformats-text copied to clipboard
Missing `null` writes when writing `Object[]` values
(moved from https://github.com/FasterXML/jackson-dataformat-csv/issues/116 by @georgewfraser)
When writing arrays to CSV, if there is a null, columns get written out of order. Reproduced in 2.7.1 with this test: https://gist.github.com/georgewfraser/164647fbaca61aadf9ee :
@Test
public void objectArray() throws IOException {
CsvSchema schema = CsvSchema.builder()
.addColumn("a", CsvSchema.ColumnType.NUMBER)
.addColumn("b", CsvSchema.ColumnType.NUMBER)
.setUseHeader(true)
.build();
ObjectWriter writer = Config.CSV.writer(schema);
StringWriter out = new StringWriter();
SequenceWriter sequence = writer.writeValues(out);
sequence.write(new Object[]{ 1, 2 });
sequence.write(new Object[]{ null, 2 });
sequence.write(new Object[]{ null, null });
sequence.write(new Object[]{ 1, null });
assertEquals("\"a\",\"b\"\n" +
"1,2\n" +
",2\n" +
",\n" +
"1,\n", out.toString());
}
More likely related to #106: looks like nulls for "array in array" are suppressed. This could be somewhat problematic even for other entries since "null value" would not get written.
@cowtowncoder Hey, Is anyone working on this I would be happy to pick it up
Added a gist for the current behaviour in 2.9.0 and what I think the desired behaviour should be
https://gist.github.com/daliclass/19507eb79939e6ad716b30cd241c612b
@cowtowncoder Is there any workaround for the issue?
@ganchurin not aware of work-arounds unfortunately. If anyone has time to look into this, I would be happy to help validate fix, integrate, but right now I do not have time in short term. I will add this as something to (re-)evaluate tho as it is an important issue.
Workaround: Before writing change null value to an empty string. In not-quoted CSV empty string looks the same as null (separator only). If you need to use qouteChar, set CsvGenerator.Feature.ALWAYS_QUOTE_STRINGS to false to use quotes only when necessary. Empty string will stay not-quoted. If you need all values quoted, use another library.