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

fixes #15 SKIP_EMPTY_LINES feature for csv parser #153

Merged
merged 3 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions csv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ Jackson supports the following extension or variations:
* Linefeed character: when generating content, the default linefeed String used is "`\n`" but this may be changed
* Null value: by default, null values are serialized as empty Strings (""), but any other String value be configured to be used instead (for example, "null", "N/A" etc)
* Use of first row as a set of column names: as explained earlier, it is possible to configure `CsvSchema` to indicate that the contents of the first (non-comment) document row is taken to mean the set of column names to use
* Comments
* When enabled (via `CsvSchema`, or enabling `JsonParser.Feature.ALLOW_YAML_COMMENTS`), if a row starts with a `#` character, it will be considered a comment and skipped
* Comments: when enabled (via `CsvSchema`, or enabling `CsvParser.Feature.ALLOW_COMMENTS`), if a row starts with a `#` character, it will be considered a comment and skipped
* Blank lines: when enabled (using `CsvParser.Feature.SKIP_BLANK_LINES`) rows that are empty or composed only of whitespaces are skipped

# Limitations

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.fasterxml.jackson.dataformat.csv;

import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.base.ParserMinimalBase;
import com.fasterxml.jackson.core.json.DupDetector;
Expand All @@ -13,6 +9,12 @@
import com.fasterxml.jackson.dataformat.csv.impl.CsvIOContext;
import com.fasterxml.jackson.dataformat.csv.impl.TextBuffer;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
* {@link JsonParser} implementation used to expose CSV documents
* in form that allows other Jackson functionality to deal
Expand Down Expand Up @@ -71,11 +73,13 @@ public enum Feature
IGNORE_TRAILING_UNMAPPABLE(false),

/**
* Feature that allows skipping input lines that are completely empty, instead
* of being decoded as lines of just a single column with empty String value (or,
* Feature that allows skipping input lines that are completely empty or blank (composed only of whitespace),
* instead of being decoded as lines of just a single column with an empty/blank String value (or,
* depending on binding, `null`).
*<p>
* Feature is disabled by default.
*
* @since 2.10
*/
SKIP_EMPTY_LINES(false),

Expand Down Expand Up @@ -744,17 +748,18 @@ protected void _readHeaderLine() throws IOException {
*/
protected JsonToken _handleStartDoc() throws IOException
{
// also, if comments enabled, may need to skip leading ones
_reader.skipLeadingComments();
// also, if comments enabled, or skip empty lines, may need to skip leading ones
_reader.skipLinesWhenNeeded();

// First things first: are we expecting header line? If so, read, process
if (_schema.usesHeader()) {
_readHeaderLine();
_reader.skipLeadingComments();
_reader.skipLinesWhenNeeded();
}
// and if we are to skip the first data line, skip it
if (_schema.skipsFirstDataRow()) {
_reader.skipLine();
_reader.skipLeadingComments();
_reader.skipLinesWhenNeeded();
}

// Only one real complication, actually; empty documents (zero bytes).
Expand Down