Skip to content

Commit

Permalink
Documentation for null vs empty column parsing preference
Browse files Browse the repository at this point in the history
Fixes #102
  • Loading branch information
ash211 committed Sep 7, 2016
1 parent 30ef366 commit 8394bbe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/site/apt/index.apt
Expand Up @@ -40,7 +40,7 @@ Super CSV

** Highly configurable

Choose your own delimiter, quote character and line separator - or just use one of the predefined configurations.
Choose your own delimiter, quote character, escape character, and line separator - or just use one of the predefined configurations.
Comma-separated, tab-separated, semicolon-separated (Germany/Denmark) - it's all possible.

** Data conversion
Expand Down Expand Up @@ -75,4 +75,4 @@ Super CSV
filenames as arguments rather than Reader and Writer objects. Design patterns such as <chain of responsibility> and the
<null object pattern> can also be found in the code. Feel free to have a look!

<Kasper>
<Kasper>
19 changes: 17 additions & 2 deletions src/site/apt/preferences.apt
Expand Up @@ -44,6 +44,10 @@ CSV Preferences

[]

[emptyColumnParsing] Controls how to handle empty columns, whether they should be treated as null, or as an empty String: "". Valid values are ParseEmptyColumnsAsNull and ParseEmptyColumnsAsEmptyString. The default value is ParseEmptyColumnsAsNull.

[]

[maxLinesPerRow] The maximum number of lines a row of CSV can span (useful for debugging large files with mismatched quotes, as it ensures fast failure and prevents OutOfMemoryErrors when trying to find the matching quote).

[]
Expand Down Expand Up @@ -140,18 +144,29 @@ private static final CsvPreference STANDARD_SURROUNDING_SPACES_NEED_QUOTES =
empty lines. If you with to disable this behaviour and allow empty lines to be read you can use the <<<ignoreEmptyLines>>> preference:

+----------------------------------------------------------------------------------------------------+
private static final CsvPreference ALLOW_EMPTY_LINES =
private static final CsvPreference ALLOW_EMPTY_LINES =
new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE).ignoreEmptyLines(false).build();
+----------------------------------------------------------------------------------------------------+

* Handling empty columns

By default, an empty column is treated as null when reading CSV. Sometimes however you need an empty column to be treated as an empty String
instead: "". If you wish to change this behavior and treat empty columns as empty String instead of null, you can use the <<<emptyColumnParsing>>> preference:

+----------------------------------------------------------------------------------------------------+
private static final CsvPreference ALLOW_EMPTY_COLUMNS =
new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE)
.setEmptyColumnParsing(EmptyColumnParsing.ParseEmptyColumnsAsEmptyString).build();
+----------------------------------------------------------------------------------------------------+

* Limiting the maximum number of lines per row when reading CSV

If your CSV file isn't quoted property (is missing a trailing quote, for example), then it's possible that Super CSV will keep reading
lines in an attempt to locate the end of the row - for large files this can cause an <<<OutOfMemoryException>>>. By setting the <<<maxLinesPerRow>>>
preference Super CSV will fail fast, giving you a chance to locate the row in error and fix it:

+----------------------------------------------------------------------------------------------------+
private static final CsvPreference DISABLE_MULTILINE_ROWS =
private static final CsvPreference DISABLE_MULTILINE_ROWS =
new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE).maxLinesPerRow(10).build();
+----------------------------------------------------------------------------------------------------+

Expand Down

0 comments on commit 8394bbe

Please sign in to comment.