-
-
Notifications
You must be signed in to change notification settings - Fork 149
CsvSchema
Most aspects of CSV parsing and generation are controlled by CsvSchema
associated with the reader/parser and writer/generator. Schema defines two main kinds of things:
- Columns that the CSV document has, to allow for binding logical property name to/from actual physical column in CSV document (by index)
- Details of how contents within columns (cell values) are encoded
There are 3 ways to construct a CsvSchema
instance with column mapping definition (and one way for "unmapped" reading):
- Create schema based on a Java class
- Build schema programmatically (explicit code)
- Use the first line of CSV document to get the names (no types) for Schema
(TO BE WRITTEN)
(TO BE WRITTEN)
Note: this considered a "simple" encoding feature.
(TO BE WRITTEN)
The way to construct an "unmapped" schema is to:
CsvSchema unmapped = CsvSchema.emptySchema();
but this "schema" is also the default one configured to ObjectReader
s and ObjectWriter
s if you do not explicitly specify a schema. So it is possible to simply omit the definition and read content like:
CsvMapper mapper = new CsvMapper();
mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);
// when wrapped as an array, we'll get array/List of arrays/Lists, for example:
String[][] rows = mapper.readValue(csvContent, String[][].class);
You can also use streaming read like so:
// NOTE: type given for 'readerFor' is type of individual row
MappingIterator<String[]> it = mapper.readerFor(String[].class)
.readValues(csvSource);
while (it.hasNextValue()) {
String[] row = it.nextValue();
Note that when using "unmapped" schema, content must be read as arrays/Lists of String or java.lang.Object
s (String[]
, List<String>
etc); no other types are accepted
Following encoding/decoding properties are currently (2.6) defined:
- On/off encoding features:
- Use header? (default: disabled)
- Comments allowed? (default: disabled)
- Skip data row? (default: disabled)
- Column separator (default: comma, ",")
- Must be defined, can not be disablde
- Array element separator (default: semicolon, ";")
- Optional, may be disabled
- Quote char (default: double quote, '"')
- Optional, may be disabled
- Escape char (default: not enabled)
- Optional, may be disabled
- Null value (default: empty String, "")
TO BE COMPLETED