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

Serializing List with null values leads to corrupt CSV #83

@sothmann

Description

@sothmann

If I serialize a List of objects, e.g. a list of POJOs and the list contains null values, the next line after the null value is corrupt.

Example:

List<MyPojo> list = Arrays.asList(
      new MyPojo("foo", "bar", 123),
      null,
      new MyPojo("test", "abc", 42)
);

gets serialized to

foo,bar,123
,abc,42

instead of the expected output of

foo,bar,123
test,abc,42

Tested with 2.5.4 and 2.6.0-rc4.

Here is a complete test case:

public class JacksonCsvNullInListTest {
  @Test
  public void testNullInList() throws JsonProcessingException {
    CsvMapper mapper = new CsvMapper();
    CsvSchema schema = mapper.schemaFor(MyPojo.class);
    final ObjectWriter writer = mapper.writer(schema);

    List<MyPojo> list = Arrays.asList(
      new MyPojo("foo", "bar", 123),
      null,
      new MyPojo("test", "abc", 42)
    );

    String expectedCsv = "foo,bar,123\ntest,abc,42\n";
    String actualCsv = writer.writeValueAsString(list);
    assertEquals(expectedCsv, actualCsv);
  }

  private static class MyPojo {
    private String prop1;
    private String prop2;
    private Integer prop3;

    public MyPojo(String prop1, String prop2, Integer prop3) {
      this.prop1 = prop1;
      this.prop2 = prop2;
      this.prop3 = prop3;
    }

    public String getProp1() {
      return prop1;
    }

    public String getProp2() {
      return prop2;
    }

    public Integer getProp3() {
      return prop3;
    }
  }
}

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