jackson-dataformats-text icon indicating copy to clipboard operation
jackson-dataformats-text copied to clipboard

Missing `null` writes when writing `Object[]` values

Open cowtowncoder opened this issue 8 years ago • 8 comments

(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());
    }

cowtowncoder avatar Mar 20 '17 17:03 cowtowncoder

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 avatar Mar 20 '17 17:03 cowtowncoder

@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

daliclass avatar Jun 02 '18 08:06 daliclass

@cowtowncoder Is there any workaround for the issue?

ganchurin avatar Aug 19 '19 12:08 ganchurin

@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.

cowtowncoder avatar Aug 19 '19 19:08 cowtowncoder

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.

gimiz avatar Dec 11 '19 12:12 gimiz