From 3492030030fc54f7495851cb7b7d49f56051a8ae Mon Sep 17 00:00:00 2001 From: Cowtowncoder Date: Wed, 15 Apr 2015 09:27:23 -0700 Subject: [PATCH] Backport #75 in 2.5(.3) --- release-notes/CREDITS | 9 ++++++++- release-notes/VERSION | 5 +++++ .../jackson/dataformat/csv/impl/CsvDecoder.java | 6 ++++++ .../dataformat/csv/deser/TestParserEscapes.java | 11 +++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/release-notes/CREDITS b/release-notes/CREDITS index 764559c..7f73fd3 100644 --- a/release-notes/CREDITS +++ b/release-notes/CREDITS @@ -34,4 +34,11 @@ Luke Nezda (nezda@github) * Suggested #56: Support comments (either via `CsvSchema`, or using std `JsonParser.Feature.ALLOW_YAML_COMMENTS. (requested by nezda@github) - (2.5.0) \ No newline at end of file + (2.5.0) + +David Navas (davidnavas@github) + +* Contributed #75: Support escapes at beginning of the file + (2.5.3) + + diff --git a/release-notes/VERSION b/release-notes/VERSION index 10d88cb..4015685 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -4,6 +4,11 @@ Project: jackson-dataformat-csv === Releases === ------------------------------------------------------------------------ +2.5.3 (not yet released) + +#75: Support escapes at beginning of the file + (contributed by David N) + 2.5.2 (29-Mar-2015) #66: Deserializing an empty string as an array field return a non-empty list of one empty String diff --git a/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvDecoder.java b/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvDecoder.java index 6253bba..69864c5 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvDecoder.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvDecoder.java @@ -632,6 +632,12 @@ public String nextString() throws IOException outBuf[0] = (char) i; int outPtr = 1; + if (i == _escapeChar) { + // Reset the escaped character + outBuf[0] = _unescape(); + return _nextUnquotedString(outBuf, outPtr); + } + int ptr = _inputPtr; if (ptr >= _inputEnd) { if (!loadMore()) { // ok to have end-of-input but... diff --git a/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestParserEscapes.java b/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestParserEscapes.java index a08a6f6..7e7cf59 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestParserEscapes.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestParserEscapes.java @@ -51,4 +51,15 @@ public void testSimpleEscapesInUnquoted() throws Exception assertEquals("Desc with\nlinefeed", result.desc); } + public void testEscapesAtStartInUnquoted() throws Exception + { + CsvMapper mapper = mapperForCsv(); + CsvSchema schema = mapper.schemaFor(Desc.class).withColumnSeparator('|').withEscapeChar('\\'); + final String id = "\\|abcdef"; // doubled for javac + final String desc = "Desc with\\\nlinefeed"; + String input = id+"|"+desc+"\n"; + Desc result = mapper.reader(schema).withType(Desc.class).readValue(input); + assertEquals("|abcdef", result.id); + assertEquals("Desc with\nlinefeed", result.desc); + } }