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

Null fields are always ignored when serializing list of objects #106

@morbrian

Description

@morbrian

When serializing a list of objects, any null values are skipped over as if they do not exist instead of writing an empty character or using a custom value in place of null.

Example 1 (Expected Default Behavior)

List list = Arrays.asList("d0", null, "d2");

Expected:

    d0,,d2

Actual:

    d0,d2

Example 2 (Expected Custom Behavior)

List list = Arrays.asList("d0", null, "d2");

Assuming a schema like: 

CsvSchema.builder().setNullValue("n/a").build();

Expected:

    d0,n/a,d2

Actual:

    d0,d2

Tested Versions

Versions: 2.6.3 - 2.7.0-rc2

Unit Tests

To demonstrate the issue, the test methods below may be added to NullWritingTest.java

public void testCustomNullValueInObjectList() throws Exception
{
    ObjectMapper mapper = mapperForCsv();
    CsvSchema schema = CsvSchema.builder()
        .setNullValue("n/a")
        .build();

    List list = Arrays.asList("d0", null, "d2");

    String result = mapper.writer(schema).writeValueAsString(list);
    // MUST use doubling for quotes!
    assertEquals("d0,n/a,d2\n", result);
}

public void testDefaultNullValueInObjectList() throws Exception
{
    ObjectMapper mapper = mapperForCsv();
    CsvSchema schema = CsvSchema.builder().build();

    List list = Arrays.asList("d0", null, "d2");

    String result = mapper.writer(schema).writeValueAsString(list);
    // MUST use doubling for quotes!
    assertEquals("d0,,d2\n", result);
}

Other Background

This issue may be related to an overlooked area of CsvGenerator.java when fixing #83 or #69

In that class, there are some related notes in method public void writeNull() throws IOException, the second condition in the block below is matched, but the write activity is commented out:

if (_arraySeparator >= 0) {
    _addToArray(_schema.getNullValueOrEmpty());
} else if (!_writeContext.inObject()) { // as per [#69]
    // note: 'root' not enough, for case of wrap-as array, or serialize List

    // or, to write 'empty Object' (for common case), would
    // write single null, then finish row, like so:
    /*
    _writer.writeNull(_columnIndex());
    finishRow();
   */
} else {
    _writer.writeNull(_columnIndex()); 
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions