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

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

Closed
morbrian opened this issue Dec 31, 2015 · 2 comments
Closed

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

morbrian opened this issue Dec 31, 2015 · 2 comments
Milestone

Comments

@morbrian
Copy link

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()); 
}
@cowtowncoder
Copy link
Member

@morbrian Quick question: can you reproduce this with 2.7.0-rc2?

@morbrian
Copy link
Author

@cowtowncoder Yes, I tried 2.6.3, 2.6.4, 2.7.0-rc2, as well as the current master.

As I play with the code I am learning the issue may already be known about, but perhaps not easy to fix without impacting other desired behavior.

Uncommenting the code block described in the issue report fixes the problem for my current use cases, but doing so breaks other related Null tests: testNullToStream() and testNullToString()

The testNullToStream() test has comments saying nulls are ignored for now.

morbrian added a commit to morbrian/jackson-dataformat-csv that referenced this issue Jan 1, 2016
…ocessing null elements of lists as empty fields or the nullCharacter during serialization instead of the default behavior which skips null elements.
morbrian added a commit to morbrian/jackson-dataformat-csv that referenced this issue Jan 1, 2016
…ocessing null elements of lists as empty fields or the nullCharacter during serialization instead of the default behavior which skips null elements.
morbrian added a commit to morbrian/jackson-dataformat-csv that referenced this issue Jan 1, 2016
…ocessing null elements of lists as empty fields or the nullCharacter during serialization instead of the default behavior which skips null elements.
morbrian added a commit to morbrian/jackson-dataformat-csv that referenced this issue Jan 3, 2016
…process null values when processing nulls deeper than the root level.

[FasterXML#106] added unit test code to verify writeNull behavior for list elements.
cowtowncoder added a commit that referenced this issue Jan 5, 2016
@cowtowncoder cowtowncoder added this to the 2.7.0-rc3 milestone Jan 5, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants