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

DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT doesn't work #112

Closed
hassomehide opened this issue Jan 15, 2016 · 8 comments
Closed

Comments

@hassomehide
Copy link

Empty Strings are not deserialized into null objects, even if ACCEPT_EMPTY_STRING_AS_NULL_OBJECT is enabled. Sample code is here:

ObjectReader:

private ObjectReader getObjectReader() {
  CsvMapper mapper = new CsvMapper();
  mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
  CsvSchema schema = mapper.schema().withHeader().withColumnSeparator(';');
  return mapper.reader(schema).forType(TestRow.class);
}

Object:

private static class TestRow {
  private String testString;
  private Integer testInteger;
  private Float testFloat;
  private TestEnum testEnum;

  // getters/setters
}

CSV file (first row is the header, second row is the data):

testString;testInteger;testFloat;testEnum
;;;

Test case:

@Test
public void testNulls() throws IOException {
  // map from CSV to TestRow
  assertNull(row.getTestInteger()); // succeeds
  assertNull(row.getTestFloat()); // succeeds
  assertNull(row.getTestEnum()); // succeeds
  assertNull(row.getTestString()); // fails
}
@cowtowncoder
Copy link
Member

I assume what you get here is an exception for last case?

@hassomehide
Copy link
Author

An AssertionError, because the value of testString is "", no matter if ACCEPT_EMPTY_STRING_AS_NULL_OBJECT is enabled or not.

@cowtowncoder
Copy link
Member

Ok, I don't think that is an error, then. String in context of Jackson is considered a scalar type, and not Object in that sense -- I realize it is java.lang.Object, but within Jackson terminology it is not Object. Feature should be named ACCEPT_EMPTY_STRINGS_AS_NULL_POJO to reflect this.

However; for CSV handling specifically it would be possible to add separate CsvParser.Feature to force coercion from Empty String into null. If this isn't already the case

@cowtowncoder
Copy link
Member

Actually: come to think of this, there is already a mechanism for this: when constructing CsvSchema, use withNullValue("") to specify that empty String should be read as Java null. This works for columns of all types.

@hassomehide
Copy link
Author

Right, this works. Thanks!

@robmoore
Copy link

I'm already using withNullValue() for NULL so I was hoping that the DeserializationFeature approach would work in conjunction with the withNullValue() setting. Any chance that withNullValues() might be added to support multiple items?

@cowtowncoder
Copy link
Member

@robmoore I am hesitant to add support for a larger set of transformations. But if it'd be enough to just support empty String and one alternative notation (like "null"), I think that this combination should work (or be made to work if not yet) -- coercion of "" to null Object / default scalar value is something jackson-databind supports.

But I think you may want to file a separate issue for your specific use case, partly since this issue is closed, and partly since your usage/expectation may be slightly different.
This would be good time to file a new issue, too, as I am hoping to complete 2.9.0.pr2 and in particular trying to decide what incremental improvements could still be made in CSV module.

@robmoore
Copy link

@cowtowncoder The solution you propose would work fine for this use case. I'll open a separate ticket.

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

3 participants