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

Serializing List with null values leads to corrupt CSV #83

Closed
sothmann opened this issue Jul 16, 2015 · 6 comments
Closed

Serializing List with null values leads to corrupt CSV #83

sothmann opened this issue Jul 16, 2015 · 6 comments
Milestone

Comments

@sothmann
Copy link

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

Thank you for reporting this. A nasty bug indeed, will hope to fix it ASAP.

@cowtowncoder
Copy link
Member

Hmmh. Assuming this is related to #69 somehow. Investigating.

@cowtowncoder cowtowncoder added this to the 2.6.0 milestone Jul 17, 2015
@cowtowncoder
Copy link
Member

Seems like fix for #69 should be generalized slightly, to apply not only to root-level values but also to "wrap as array", or serializing arrays/Lists.

@sothmann
Copy link
Author

Thanks for fixing this issue!

@cowtowncoder
Copy link
Member

Np, thank you for reporting this. And right in time to be included in 2.6.0, to be released in a day or two.

@sothmann
Copy link
Author

Perfect, good to hear that 👍
Waiting for the 2.6.0 Release (or another RC that contains the fix)

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