From 9039e78a00259ce801a6fb7da6e1a2f430fa5bde Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Thu, 4 Apr 2024 10:17:47 -0700 Subject: [PATCH] Merge part of #471 ahead of it (to reduce diff) (#472) --- .../jackson/dataformat/csv/CsvParser.java | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java index 25e5d60a..6d9720c5 100644 --- a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java +++ b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java @@ -1021,12 +1021,7 @@ protected JsonToken _handleNamedValue() throws IOException } } _state = STATE_NEXT_ENTRY; - if (_nullValue != null) { - if (_nullValue.equals(_currentValue)) { - return JsonToken.VALUE_NULL; - } - } - if (_cfgEmptyStringAsNull && "".equals(_currentValue)) { + if (_isNullValue(_currentValue)) { return JsonToken.VALUE_NULL; } return JsonToken.VALUE_STRING; @@ -1048,12 +1043,7 @@ protected JsonToken _handleUnnamedValue() throws IOException // state remains the same _currentValue = next; ++_columnIndex; - if (_nullValue != null) { - if (_nullValue.equals(next)) { - return JsonToken.VALUE_NULL; - } - } - if (_cfgEmptyStringAsNull && "".equals(_currentValue)) { + if (_isNullValue(next)) { return JsonToken.VALUE_NULL; } return JsonToken.VALUE_STRING; @@ -1093,12 +1083,7 @@ protected JsonToken _handleArrayValue() throws IOException if (isEnabled(Feature.TRIM_SPACES)) { _currentValue = _currentValue.trim(); } - if (_nullValue != null) { - if (_nullValue.equals(_currentValue)) { - return JsonToken.VALUE_NULL; - } - } - if (_cfgEmptyStringAsNull && "".equals(_currentValue)) { + if (_isNullValue(_currentValue)) { return JsonToken.VALUE_NULL; } return JsonToken.VALUE_STRING; @@ -1448,4 +1433,23 @@ protected void _startArray(CsvSchema.Column column) } _arraySeparator = sep; } + + + /** + * Helper method called to check whether specified String value should be considered + * "null" value, if so configured. + * + * @since 2.17.1 + */ + protected boolean _isNullValue(String value) { + if (_nullValue != null) { + if (_nullValue.equals(value)) { + return true; + } + } + if (_cfgEmptyStringAsNull && _currentValue.isEmpty()) { + return true; + } + return false; + } }