public class CsvSchema extends Object implements com.fasterxml.jackson.core.FormatSchema, Iterable<CsvSchema.Column>
FormatSchema
sub-type that defines properties of
a CSV document to read or write.
Properties supported currently are:
Note that schemas without any columns are legal, but if no columns are added, behavior of parser/generator is usually different and content will be exposed as logical Arrays instead of Objects.
There are 4 ways to create CsvSchema
instances:
CsvSchema.Builder
withXxx
methods
or rebuild()
for creating CsvSchema.Builder
)
CsvMapper
methods like CsvMapper.schemaFor(java.lang.Class)
.
CsvParser
reads schema from the first line:
enable "useHeader" property for the initial schema, and let parser
read column names from the document itself.
Modifier and Type | Class and Description |
---|---|
static class |
CsvSchema.Builder
Class used for building
CsvSchema instances. |
static class |
CsvSchema.Column
Representation of info for a single column
|
static class |
CsvSchema.ColumnType
Enumeration that defines optional type indicators that can be passed
with schema.
|
Modifier and Type | Field and Description |
---|---|
protected CsvSchema.Column[] |
_columns
Column definitions, needed for optional header and/or mapping
of field names to column positions.
|
protected Map<String,CsvSchema.Column> |
_columnsByName |
protected char |
_columnSeparator |
protected int |
_escapeChar |
protected char[] |
_lineSeparator |
protected int |
_quoteChar |
protected boolean |
_skipFirstDataRow |
protected boolean |
_useHeader |
static char |
DEFAULT_COLUMN_SEPARATOR |
static int |
DEFAULT_ESCAPE_CHAR
By default, no escape character is used -- this is denoted by
int value that does not map to a valid character
|
static char[] |
DEFAULT_LINEFEED |
static char |
DEFAULT_QUOTE_CHAR |
static boolean |
DEFAULT_SKIP_FIRST_DATA_ROW |
static boolean |
DEFAULT_USE_HEADER
By default we do NOT expect the first line to be header.
|
protected static CsvSchema.Column[] |
NO_COLUMNS |
Modifier | Constructor and Description |
---|---|
|
CsvSchema(CsvSchema.Column[] columns,
boolean useHeader,
boolean skipFirstDataRow,
char columnSeparator,
int quoteChar,
int escapeChar,
char[] lineSeparator) |
protected |
CsvSchema(CsvSchema.Column[] columns,
boolean useHeader,
boolean skipFirstDataRow,
char columnSeparator,
int quoteChar,
int escapeChar,
char[] lineSeparator,
Map<String,CsvSchema.Column> columnsByName)
Copy constructor used for creating variants using
withXxx() methods. |
protected |
CsvSchema(CsvSchema base,
CsvSchema.Column[] columns)
Copy constructor used for creating variants using
sortedBy() methods. |
Modifier and Type | Method and Description |
---|---|
static CsvSchema.Builder |
builder() |
CsvSchema.Column |
column(int index) |
CsvSchema.Column |
column(String name) |
static CsvSchema |
emptySchema()
Accessor for creating a "default" CSV schema instance, with following
settings:
Does NOT use header line
Uses double quotes ('"') for quoting of field values (if necessary)
Uses comma (',') as the field separator
Uses Unix linefeed ('\n') as row separator
Does NOT use any escape characters
Does NOT have any columns defined
|
String |
getColumnDesc()
Method for getting description of column definitions in
developer-readable form
|
char |
getColumnSeparator() |
int |
getEscapeChar() |
char[] |
getLineSeparator() |
int |
getQuoteChar() |
String |
getSchemaType() |
Iterator<CsvSchema.Column> |
iterator() |
CsvSchema.Builder |
rebuild()
Helper method for constructing Builder that can be used to create modified
schema.
|
int |
size() |
boolean |
skipFirstDataRow() |
CsvSchema |
sortedBy(Comparator<String> cmp)
Mutant factory method that will construct a new instance in which columns
are sorted using given
Comparator over column names. |
CsvSchema |
sortedBy(String... columnNames)
Mutant factory method that will construct a new instance in which columns
are sorted based on names given as argument.
|
String |
toString() |
boolean |
useHeader() |
boolean |
usesEscapeChar() |
boolean |
usesQuoteChar() |
CsvSchema |
withColumnSeparator(char sep) |
CsvSchema |
withEscapeChar(char c) |
CsvSchema |
withHeader()
Helper method for construcing and returning schema instance that
is similar to this one, except that it will be using header line.
|
CsvSchema |
withLineSeparator(String sep) |
CsvSchema |
withoutColumns() |
CsvSchema |
withoutEscapeChar() |
CsvSchema |
withoutHeader()
Helper method for construcing and returning schema instance that
is similar to this one, except that it will not be using header line.
|
CsvSchema |
withoutQuoteChar() |
CsvSchema |
withQuoteChar(char c) |
CsvSchema |
withSkipFirstDataRow(boolean state) |
CsvSchema |
withUseHeader(boolean state) |
protected static final CsvSchema.Column[] NO_COLUMNS
public static final char DEFAULT_COLUMN_SEPARATOR
public static final char DEFAULT_QUOTE_CHAR
public static final int DEFAULT_ESCAPE_CHAR
public static final char[] DEFAULT_LINEFEED
public static final boolean DEFAULT_USE_HEADER
public static final boolean DEFAULT_SKIP_FIRST_DATA_ROW
protected final CsvSchema.Column[] _columns
protected final Map<String,CsvSchema.Column> _columnsByName
protected final boolean _useHeader
protected final boolean _skipFirstDataRow
protected final char _columnSeparator
protected final int _quoteChar
protected final int _escapeChar
protected final char[] _lineSeparator
public CsvSchema(CsvSchema.Column[] columns, boolean useHeader, boolean skipFirstDataRow, char columnSeparator, int quoteChar, int escapeChar, char[] lineSeparator)
protected CsvSchema(CsvSchema.Column[] columns, boolean useHeader, boolean skipFirstDataRow, char columnSeparator, int quoteChar, int escapeChar, char[] lineSeparator, Map<String,CsvSchema.Column> columnsByName)
withXxx()
methods.protected CsvSchema(CsvSchema base, CsvSchema.Column[] columns)
sortedBy()
methods.public static CsvSchema.Builder builder()
public static CsvSchema emptySchema()
public CsvSchema.Builder rebuild()
public CsvSchema withUseHeader(boolean state)
public CsvSchema withHeader()
public CsvSchema withoutHeader()
public CsvSchema withSkipFirstDataRow(boolean state)
public CsvSchema withColumnSeparator(char sep)
public CsvSchema withQuoteChar(char c)
public CsvSchema withoutQuoteChar()
public CsvSchema withEscapeChar(char c)
public CsvSchema withoutEscapeChar()
public CsvSchema withoutColumns()
public CsvSchema sortedBy(String... columnNames)
For example, schema that has columns:
"a", "d", "c", "b"ordered with
schema.sortedBy("a", "b");
would result instance that columns in order:
"a", "b", "d", "c"
public CsvSchema sortedBy(Comparator<String> cmp)
Comparator
over column names.public String getSchemaType()
getSchemaType
in interface com.fasterxml.jackson.core.FormatSchema
public boolean useHeader()
public boolean skipFirstDataRow()
public char getColumnSeparator()
public int getQuoteChar()
public int getEscapeChar()
public char[] getLineSeparator()
public boolean usesQuoteChar()
public boolean usesEscapeChar()
public Iterator<CsvSchema.Column> iterator()
iterator
in interface Iterable<CsvSchema.Column>
public int size()
public CsvSchema.Column column(int index)
public CsvSchema.Column column(String name)
public String getColumnDesc()
Copyright © 2014 FasterXML. All Rights Reserved.