java.lang.Object
tools.jackson.core.JsonParser
tools.jackson.core.base.ParserMinimalBase
tools.jackson.dataformat.csv.CsvParser
- All Implemented Interfaces:
Closeable,AutoCloseable,tools.jackson.core.Versioned
public class CsvParser
extends tools.jackson.core.base.ParserMinimalBase
JsonParser implementation used to expose CSV documents
in form that allows other Jackson functionality to deal
with it.
Implementation is based on a state-machine that pulls information
using CsvDecoder.
-
Nested Class Summary
Nested classes/interfaces inherited from class tools.jackson.core.JsonParser
tools.jackson.core.JsonParser.NumberType, tools.jackson.core.JsonParser.NumberTypeFP -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Stringprotected StringContents of the cell, to be split into distinct array values.protected intPointer to the first character of the next array value to return.protected byte[]We will hold on to decoded binary data, for duration of current event, so that multiple calls togetBinaryValue(tools.jackson.core.Base64Variant)will not need to decode data more than once.protected tools.jackson.core.util.ByteArrayBuilderprotected final booleanprotected final booleanprotected final booleanprotected intNumber of columns defined by schema.protected intIndex of the column we are exposingprotected StringName of column that we exposed most recently, accessible afterJsonToken.PROPERTY_NAMEas well as value tokens immediately following property name.protected StringString value for the current column, if accessed.protected intprotected Stringprotected final CsvDecoderThing that actually reads the CSV contentprotected CsvSchemaDefinition of columns being read.protected intCurrent logical state of the parser; one ofSTATE_constants.protected tools.jackson.core.util.SimpleStreamReadContextInformation about parser context, context in which the next token is to be parsed (root, array, object).protected final tools.jackson.core.util.TextBufferBuffer that contains contents of all values after processing of doubled-quotes, escaped characters.private static final CsvSchemaprivate static final intprotected static final intState in which end marker is returned; either null (if no array wrapping), orJsonToken.END_ARRAYfor wrapping.protected static final intInitial state before anything is read from document.protected static final intState in which a column value has been determined to be of an array type, and will need to be split into multiple values.protected static final intState in which we should expose name token for a "missing column" (for which placeholder `null` value is to be added as well); seeCsvReadFeature.INSERT_NULLS_FOR_MISSING_COLUMNSfor details.protected static final intState in which we should expose `null` value token as a value for "missing" column; seeCsvReadFeature.INSERT_NULLS_FOR_MISSING_COLUMNSfor details.protected static final intState in which value matching property name will be returned.protected static final intState in which next entry will be available, returning eitherJsonToken.PROPERTY_NAMEor value (depending on whether entries are expressed as Objects or just Arrays); or matching close marker.protected static final intState before logical start of a record, in which next token to return will beJsonToken.START_OBJECT(or if no Schema is provided,JsonToken.START_ARRAY).protected static final intState in which we have encountered more column values than there should be, and need to basically skip extra values if callers tries to advance parser state.protected static final intState in which "unnamed" value (entry in an array) will be returned, if one available; otherwise end-array is returned.protected static final tools.jackson.core.util.JacksonFeatureSet<tools.jackson.core.StreamReadCapability> CSV is slightly different from defaults, having essentially untyped scalars except if indicated by schemaFields inherited from class tools.jackson.core.base.ParserMinimalBase
_closed, _currToken, _ioContext, _lastClearedToken, _objectReadContext, _streamReadConstraints, _streamReadFeatures, _tokenCount, _trackMaxTokenCount, BD_MAX_INT, BD_MAX_LONG, BD_MIN_INT, BD_MIN_LONG, BI_MAX_INT, BI_MAX_LONG, BI_MIN_INT, BI_MIN_LONG, CHAR_NULL, INT_0, INT_9, INT_APOS, INT_ASTERISK, INT_BACKSLASH, INT_COLON, INT_COMMA, INT_CR, INT_e, INT_E, INT_HASH, INT_LBRACKET, INT_LCURLY, INT_LF, INT_MINUS, INT_PERIOD, INT_PLUS, INT_QUOTE, INT_RBRACKET, INT_RCURLY, INT_RS, INT_SLASH, INT_SPACE, INT_TAB, MAX_BYTE_I, MAX_INT_D, MAX_INT_L, MAX_LONG_D, MAX_SHORT_I, MIN_BYTE_I, MIN_INT_D, MIN_INT_L, MIN_LONG_D, MIN_SHORT_I, NO_BYTES, NO_INTS, NR_BIGDECIMAL, NR_BIGINT, NR_DOUBLE, NR_FLOAT, NR_INT, NR_LONG, NR_UNKNOWN, STREAM_READ_FEATURE_DEFAULTSFields inherited from class tools.jackson.core.JsonParser
DEFAULT_READ_CAPABILITIES -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidtools.jackson.core.util.ByteArrayBuilderprotected tools.jackson.core.JsonTokenprotected voidprotected tools.jackson.core.JsonToken_handleExtraColumn(String value) Helper method called when an extraneous column value is found.protected tools.jackson.core.JsonTokenHelper method called when end of row occurs before finding values for all schema-specified columns.protected tools.jackson.core.JsonTokenprotected tools.jackson.core.JsonTokenprotected tools.jackson.core.JsonTokenprotected tools.jackson.core.JsonTokenprotected final tools.jackson.core.JsonTokenHelper method called to handle details of state update when end of logical record occurs.protected tools.jackson.core.JsonTokenprotected tools.jackson.core.JsonTokenMethod called to handle details of initializing things to return the very first token.protected tools.jackson.core.JsonTokenprotected boolean_isNullValue(String value) Helper method called to check whether specified String value should be considered "null" value, if so configured.protected voidMethod called to process the expected header lineprotected void<T> T_reportCsvReadError(String msg, Object... args) Method called when there is a problem related to mapping CSV columns to property names, i.e.<T> T_reportError(String msg) protected <T> T<T> T_reportUnexpectedCsvChar(int ch, String msg) protected void_setSchema(CsvSchema schema) protected final tools.jackson.core.JsonTokenprotected void_startArray(CsvSchema.Column column) protected voidtools.jackson.core.JacksonExceptionvoidbooleanbooleantools.jackson.core.TokenStreamLocationtools.jackson.core.TokenStreamLocationbyte[]getBinaryValue(tools.jackson.core.Base64Variant variant) doublefloatintlongtools.jackson.core.JsonParser.NumberTypeAccessor for getting active schema definition: it may be "empty" (no column definitions), but will never be null since it defaults to an empty schema (and default configuration)intchar[]intintbooleanbooleanMethod for checking whether specified CSVCsvReadFeatureis enabled.booleanbooleanWe need to override this method to support coercion from basic String value into array, in cases where schema does not specify actual type.booleanisNaN()nextName()booleannextName(tools.jackson.core.SerializableString str) tools.jackson.core.JsonTokenintreleaseBuffered(Writer out) tools.jackson.core.util.JacksonFeatureSet<tools.jackson.core.StreamReadCapability> tools.jackson.core.TokenStreamContexttools.jackson.core.Versionversion()Methods inherited from class tools.jackson.core.base.ParserMinimalBase
_constructInputCoercion, _constructNotNumericType, _currentLocationMinusOne, _decodeBase64, _getCharDesc, _hasTextualNull, _longIntegerDesc, _longNumberDesc, _nullSafeUpdateToken, _reportBadInputStream, _reportBadReader, _reportError, _reportError, _reportError, _reportInvalidEOF, _reportInvalidEOF, _reportInvalidEOFInValue, _reportInvalidNumber, _reportInvalidSpace, _reportInvalidUTF8Surrogate, _reportMissingRootWS, _reportOverflowByte, _reportOverflowInt, _reportOverflowInt, _reportOverflowInt, _reportOverflowLong, _reportOverflowLong, _reportOverflowLong, _reportOverflowShort, _reportUnexpectedChar, _reportUnexpectedNumberChar, _throwInternal, _updateToken, _updateTokenToNA, _updateTokenToNull, clearCurrentToken, close, currentNameMatch, currentToken, currentTokenCount, currentTokenId, finishToken, getBooleanValue, getByteValue, getLastClearedToken, getNumberTypeFP, getNumberValueDeferred, getShortValue, getValueAsBoolean, getValueAsDouble, getValueAsInt, getValueAsInt, getValueAsLong, getValueAsLong, getValueAsString, getValueAsString, hasCurrentToken, hasToken, hasTokenId, isClosed, isEnabled, isExpectedStartObjectToken, nextNameMatch, nextValue, objectReadContext, readValueAs, readValueAs, readValueAs, readValueAsTree, skipChildren, streamReadConstraints, streamReadFeaturesMethods inherited from class tools.jackson.core.JsonParser
_constructReadException, _constructReadException, _constructReadException, _constructReadException, _constructReadException, _constructReadException, _reportUnsupportedOperation, canParseAsync, getBinaryValue, getObjectId, getText, getTextCharacters, getTextLength, getTextOffset, getTypeId, getValueAsBoolean, getValueAsDouble, nextBooleanValue, nextIntValue, nextLongValue, nonBlockingInputFeeder, readBinaryValue, readBinaryValue, readString, releaseBuffered
-
Field Details
-
MAX_COLUMNS
private static final int MAX_COLUMNS- See Also:
-
EMPTY_SCHEMA
-
STREAM_READ_CAPABILITIES
protected static final tools.jackson.core.util.JacksonFeatureSet<tools.jackson.core.StreamReadCapability> STREAM_READ_CAPABILITIESCSV is slightly different from defaults, having essentially untyped scalars except if indicated by schema- Since:
- 2.12
-
STATE_DOC_START
protected static final int STATE_DOC_STARTInitial state before anything is read from document.- See Also:
-
STATE_RECORD_START
protected static final int STATE_RECORD_STARTState before logical start of a record, in which next token to return will beJsonToken.START_OBJECT(or if no Schema is provided,JsonToken.START_ARRAY).- See Also:
-
STATE_NEXT_ENTRY
protected static final int STATE_NEXT_ENTRYState in which next entry will be available, returning eitherJsonToken.PROPERTY_NAMEor value (depending on whether entries are expressed as Objects or just Arrays); or matching close marker.- See Also:
-
STATE_NAMED_VALUE
protected static final int STATE_NAMED_VALUEState in which value matching property name will be returned.- See Also:
-
STATE_UNNAMED_VALUE
protected static final int STATE_UNNAMED_VALUEState in which "unnamed" value (entry in an array) will be returned, if one available; otherwise end-array is returned.- See Also:
-
STATE_IN_ARRAY
protected static final int STATE_IN_ARRAYState in which a column value has been determined to be of an array type, and will need to be split into multiple values. This can currently only occur for named values.- See Also:
-
STATE_SKIP_EXTRA_COLUMNS
protected static final int STATE_SKIP_EXTRA_COLUMNSState in which we have encountered more column values than there should be, and need to basically skip extra values if callers tries to advance parser state.- See Also:
-
STATE_MISSING_NAME
protected static final int STATE_MISSING_NAMEState in which we should expose name token for a "missing column" (for which placeholder `null` value is to be added as well); seeCsvReadFeature.INSERT_NULLS_FOR_MISSING_COLUMNSfor details.- See Also:
-
STATE_MISSING_VALUE
protected static final int STATE_MISSING_VALUEState in which we should expose `null` value token as a value for "missing" column; seeCsvReadFeature.INSERT_NULLS_FOR_MISSING_COLUMNSfor details.- See Also:
-
STATE_DOC_END
protected static final int STATE_DOC_ENDState in which end marker is returned; either null (if no array wrapping), orJsonToken.END_ARRAYfor wrapping. This step will loop, returning series of nulls ifnextToken()is called multiple times.- See Also:
-
_formatFeatures
protected int _formatFeatures -
_schema
Definition of columns being read. -
_columnCount
protected int _columnCountNumber of columns defined by schema. -
_cfgEmptyStringAsNull
protected final boolean _cfgEmptyStringAsNull -
_cfgEmptyUnquotedStringAsNull
protected final boolean _cfgEmptyUnquotedStringAsNull- Since:
- 2.18
-
_cfgOnlyUnquotedNullValuesAsNull
protected final boolean _cfgOnlyUnquotedNullValuesAsNull- Since:
- 3.1
-
_streamReadContext
protected tools.jackson.core.util.SimpleStreamReadContext _streamReadContextInformation about parser context, context in which the next token is to be parsed (root, array, object). -
_currentName
Name of column that we exposed most recently, accessible afterJsonToken.PROPERTY_NAMEas well as value tokens immediately following property name. -
_currentValue
String value for the current column, if accessed. -
_columnIndex
protected int _columnIndexIndex of the column we are exposing -
_state
protected int _stateCurrent logical state of the parser; one ofSTATE_constants. -
_binaryValue
protected byte[] _binaryValueWe will hold on to decoded binary data, for duration of current event, so that multiple calls togetBinaryValue(tools.jackson.core.Base64Variant)will not need to decode data more than once. -
_arrayValueStart
protected int _arrayValueStartPointer to the first character of the next array value to return. -
_arrayValue
Contents of the cell, to be split into distinct array values. -
_arraySeparator
-
_nullValue
-
_reader
Thing that actually reads the CSV content -
_textBuffer
protected final tools.jackson.core.util.TextBuffer _textBufferBuffer that contains contents of all values after processing of doubled-quotes, escaped characters. -
_byteArrayBuilder
protected tools.jackson.core.util.ByteArrayBuilder _byteArrayBuilder
-
-
Constructor Details
-
CsvParser
-
-
Method Details
-
version
public tools.jackson.core.Version version()- Specified by:
versionin interfacetools.jackson.core.Versioned- Specified by:
versionin classtools.jackson.core.JsonParser
-
canReadObjectId
public boolean canReadObjectId()- Overrides:
canReadObjectIdin classtools.jackson.core.JsonParser
-
canReadTypeId
public boolean canReadTypeId()- Overrides:
canReadTypeIdin classtools.jackson.core.JsonParser
-
streamReadCapabilities
public tools.jackson.core.util.JacksonFeatureSet<tools.jackson.core.StreamReadCapability> streamReadCapabilities()- Overrides:
streamReadCapabilitiesin classtools.jackson.core.base.ParserMinimalBase
-
releaseBuffered
- Overrides:
releaseBufferedin classtools.jackson.core.JsonParser
-
_closeInput
- Specified by:
_closeInputin classtools.jackson.core.base.ParserMinimalBase- Throws:
IOException
-
_releaseBuffers
protected void _releaseBuffers()- Specified by:
_releaseBuffersin classtools.jackson.core.base.ParserMinimalBase
-
isEnabled
Method for checking whether specified CSVCsvReadFeatureis enabled. -
getSchema
Accessor for getting active schema definition: it may be "empty" (no column definitions), but will never be null since it defaults to an empty schema (and default configuration)- Overrides:
getSchemain classtools.jackson.core.JsonParser
-
streamReadContext
public tools.jackson.core.TokenStreamContext streamReadContext()- Specified by:
streamReadContextin classtools.jackson.core.JsonParser
-
assignCurrentValue
- Specified by:
assignCurrentValuein classtools.jackson.core.JsonParser
-
currentValue
- Specified by:
currentValuein classtools.jackson.core.JsonParser
-
currentTokenLocation
public tools.jackson.core.TokenStreamLocation currentTokenLocation()- Specified by:
currentTokenLocationin classtools.jackson.core.JsonParser
-
currentLocation
public tools.jackson.core.TokenStreamLocation currentLocation()- Specified by:
currentLocationin classtools.jackson.core.JsonParser
-
streamReadInputSource
- Specified by:
streamReadInputSourcein classtools.jackson.core.JsonParser
-
isExpectedStartArrayToken
public boolean isExpectedStartArrayToken()We need to override this method to support coercion from basic String value into array, in cases where schema does not specify actual type.- Overrides:
isExpectedStartArrayTokenin classtools.jackson.core.base.ParserMinimalBase
-
isExpectedNumberIntToken
public boolean isExpectedNumberIntToken()- Overrides:
isExpectedNumberIntTokenin classtools.jackson.core.base.ParserMinimalBase
-
currentName
- Specified by:
currentNamein classtools.jackson.core.JsonParser
-
nextToken
public tools.jackson.core.JsonToken nextToken() throws tools.jackson.core.JacksonException- Specified by:
nextTokenin classtools.jackson.core.JsonParser- Throws:
tools.jackson.core.JacksonException
-
nextName
public boolean nextName(tools.jackson.core.SerializableString str) throws tools.jackson.core.JacksonException - Overrides:
nextNamein classtools.jackson.core.base.ParserMinimalBase- Throws:
tools.jackson.core.JacksonException
-
nextName
- Overrides:
nextNamein classtools.jackson.core.base.ParserMinimalBase- Throws:
tools.jackson.core.JacksonException
-
nextStringValue
- Overrides:
nextStringValuein classtools.jackson.core.JsonParser- Throws:
tools.jackson.core.JacksonException
-
_readHeaderLine
protected void _readHeaderLine() throws tools.jackson.core.JacksonExceptionMethod called to process the expected header line- Throws:
tools.jackson.core.JacksonException
-
_handleStartDoc
protected tools.jackson.core.JsonToken _handleStartDoc() throws tools.jackson.core.JacksonExceptionMethod called to handle details of initializing things to return the very first token.- Throws:
tools.jackson.core.JacksonException
-
_handleRecordStart
protected tools.jackson.core.JsonToken _handleRecordStart() throws tools.jackson.core.JacksonException- Throws:
tools.jackson.core.JacksonException
-
_handleNextEntry
protected tools.jackson.core.JsonToken _handleNextEntry() throws tools.jackson.core.JacksonException- Throws:
tools.jackson.core.JacksonException
-
_handleNamedValue
protected tools.jackson.core.JsonToken _handleNamedValue() throws tools.jackson.core.JacksonException- Throws:
tools.jackson.core.JacksonException
-
_handleUnnamedValue
protected tools.jackson.core.JsonToken _handleUnnamedValue() throws tools.jackson.core.JacksonException- Throws:
tools.jackson.core.JacksonException
-
_handleArrayValue
protected tools.jackson.core.JsonToken _handleArrayValue() throws tools.jackson.core.JacksonException- Throws:
tools.jackson.core.JacksonException
-
_handleExtraColumn
protected tools.jackson.core.JsonToken _handleExtraColumn(String value) throws tools.jackson.core.JacksonException Helper method called when an extraneous column value is found. What happens then depends on configuration, but there are three main choices: ignore value (and rest of line); expose extra value as "any property" using configured name, or throw an exception.- Throws:
tools.jackson.core.JacksonException
-
_handleMissingColumns
protected tools.jackson.core.JsonToken _handleMissingColumns() throws tools.jackson.core.JacksonExceptionHelper method called when end of row occurs before finding values for all schema-specified columns.- Throws:
tools.jackson.core.JacksonException
-
_handleMissingName
protected tools.jackson.core.JsonToken _handleMissingName() throws tools.jackson.core.JacksonException- Throws:
tools.jackson.core.JacksonException
-
_handleMissingValue
protected tools.jackson.core.JsonToken _handleMissingValue() throws tools.jackson.core.JacksonException- Throws:
tools.jackson.core.JacksonException
-
_handleObjectRowEnd
protected final tools.jackson.core.JsonToken _handleObjectRowEnd() throws tools.jackson.core.JacksonExceptionHelper method called to handle details of state update when end of logical record occurs.- Throws:
tools.jackson.core.JacksonException
-
_skipUntilEndOfLine
protected final tools.jackson.core.JsonToken _skipUntilEndOfLine() throws tools.jackson.core.JacksonException- Throws:
tools.jackson.core.JacksonException
-
hasStringCharacters
public boolean hasStringCharacters()- Specified by:
hasStringCharactersin classtools.jackson.core.JsonParser
-
getString
- Specified by:
getStringin classtools.jackson.core.JsonParser- Throws:
tools.jackson.core.JacksonException
-
getStringCharacters
public char[] getStringCharacters() throws tools.jackson.core.JacksonException- Specified by:
getStringCharactersin classtools.jackson.core.JsonParser- Throws:
tools.jackson.core.JacksonException
-
getStringLength
public int getStringLength() throws tools.jackson.core.JacksonException- Specified by:
getStringLengthin classtools.jackson.core.JsonParser- Throws:
tools.jackson.core.JacksonException
-
getStringOffset
public int getStringOffset() throws tools.jackson.core.JacksonException- Specified by:
getStringOffsetin classtools.jackson.core.JsonParser- Throws:
tools.jackson.core.JacksonException
-
getString
- Overrides:
getStringin classtools.jackson.core.base.ParserMinimalBase- Throws:
tools.jackson.core.JacksonException
-
getEmbeddedObject
- Overrides:
getEmbeddedObjectin classtools.jackson.core.base.ParserMinimalBase- Throws:
tools.jackson.core.JacksonException
-
getBinaryValue
public byte[] getBinaryValue(tools.jackson.core.Base64Variant variant) throws tools.jackson.core.JacksonException - Specified by:
getBinaryValuein classtools.jackson.core.JsonParser- Throws:
tools.jackson.core.JacksonException
-
getNumberType
public tools.jackson.core.JsonParser.NumberType getNumberType() throws tools.jackson.core.JacksonException- Specified by:
getNumberTypein classtools.jackson.core.JsonParser- Throws:
tools.jackson.core.JacksonException
-
getNumberValue
- Specified by:
getNumberValuein classtools.jackson.core.JsonParser- Throws:
tools.jackson.core.JacksonException
-
getNumberValueExact
- Overrides:
getNumberValueExactin classtools.jackson.core.base.ParserMinimalBase- Throws:
tools.jackson.core.JacksonException
-
getIntValue
public int getIntValue() throws tools.jackson.core.JacksonException- Specified by:
getIntValuein classtools.jackson.core.JsonParser- Throws:
tools.jackson.core.JacksonException
-
getLongValue
public long getLongValue() throws tools.jackson.core.JacksonException- Specified by:
getLongValuein classtools.jackson.core.JsonParser- Throws:
tools.jackson.core.JacksonException
-
getBigIntegerValue
- Specified by:
getBigIntegerValuein classtools.jackson.core.JsonParser- Throws:
tools.jackson.core.JacksonException
-
getFloatValue
public float getFloatValue() throws tools.jackson.core.JacksonException- Specified by:
getFloatValuein classtools.jackson.core.JsonParser- Throws:
tools.jackson.core.JacksonException
-
getDoubleValue
public double getDoubleValue() throws tools.jackson.core.JacksonException- Specified by:
getDoubleValuein classtools.jackson.core.JsonParser- Throws:
tools.jackson.core.JacksonException
-
getDecimalValue
- Specified by:
getDecimalValuein classtools.jackson.core.JsonParser- Throws:
tools.jackson.core.JacksonException
-
isNaN
public boolean isNaN()- Specified by:
isNaNin classtools.jackson.core.JsonParser
-
_handleEOF
protected void _handleEOF() throws tools.jackson.core.exc.StreamReadException- Specified by:
_handleEOFin classtools.jackson.core.base.ParserMinimalBase- Throws:
tools.jackson.core.exc.StreamReadException
-
_reportCsvReadError
public <T> T _reportCsvReadError(String msg, Object... args) throws tools.jackson.core.JacksonException Method called when there is a problem related to mapping CSV columns to property names, i.e. is CSV-specific aspect- Throws:
tools.jackson.core.JacksonException
-
_reportUnexpectedCsvChar
public <T> T _reportUnexpectedCsvChar(int ch, String msg) throws tools.jackson.core.JacksonException - Throws:
tools.jackson.core.JacksonException
-
_reportError
- Overrides:
_reportErrorin classtools.jackson.core.base.ParserMinimalBase- Throws:
tools.jackson.core.exc.StreamReadException
-
_wrapIOFailure
- Overrides:
_wrapIOFailurein classtools.jackson.core.base.ParserMinimalBase
-
_verifyNumberToken
protected void _verifyNumberToken() throws tools.jackson.core.JacksonException- Throws:
tools.jackson.core.JacksonException
-
_reportNotNumericError
protected <T> T _reportNotNumericError() throws tools.jackson.core.JacksonException- Throws:
tools.jackson.core.JacksonException
-
_setSchema
-
_getByteArrayBuilder
public tools.jackson.core.util.ByteArrayBuilder _getByteArrayBuilder() -
_startArray
-
_isNullValue
Helper method called to check whether specified String value should be considered "null" value, if so configured.- Since:
- 2.17.1
-