Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSV: Use escapeChar in nullValue #44

Open
tabalinas opened this issue Oct 20, 2017 · 3 comments
Open

CSV: Use escapeChar in nullValue #44

tabalinas opened this issue Oct 20, 2017 · 3 comments
Labels

Comments

@tabalinas
Copy link

I have an issue with CSV reader, when escapeChar is set to \ and nullValue to \N.

In this case reading a CSV like the following:

id,value
1,\N

results in value="N" instead of value=null.

I tried to replace nullValue with "\\\\N", but it doesn't help.

Any ideas?

Thank you in advance!

@cowtowncoder
Copy link
Member

I think a unit test (piece of code) that reproduces the issue would help here, to ensure specifics of how characters are escaped in code.

Also: which Jackson version is this with?

@tabalinas
Copy link
Author

tabalinas commented Oct 21, 2017

Thank you for the quick response!

Sure, a unit test to reproduce the issue:

@Test
public void escapeCharAndNullValueConflict() throws IOException {
    String csv = "id,value\n" +
            "1,\\N";

    InputStream in = new ByteArrayInputStream(csv.getBytes(StandardCharsets.UTF_8.name()));

    CsvSchema schema = CsvSchema
            .emptySchema()
            .withHeader()
            .withColumnSeparator(',')
            .withEscapeChar('\\')
            .withNullValue("\\N");

    MappingIterator<ObjectNode> it =
            new CsvMapper().readerFor(ObjectNode.class).with(schema).readValues(in);

    ObjectNode row = it.nextValue();

    assertThat(row.get("value"), is(nullValue()));
}

which fails with:

Expected: is null
     but: was <"N">
java.lang.AssertionError: 
Expected: is null
     but: was <"N">
	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
	at org.junit.Assert.assertThat(Assert.java:865)
	at org.junit.Assert.assertThat(Assert.java:832)

We use Jackson v2.8.8.

@cowtowncoder
Copy link
Member

Hmmh. I am not 100% sure what the logic should be, and due to the way tokenization is done it may be that this is considered feature, not bug. The problem is that replacement of null value is higher level operation and occurs after tokenization; escape characters are, however, handled during tokenization.

But I can see why this may be problematic so I will keep this open to see if handling could be improved.

@cowtowncoder cowtowncoder added 2.10 and removed 2.9 labels Oct 5, 2019
@cowtowncoder cowtowncoder removed the 2.10 label Nov 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants