Skip to main content

CSV Parsers

The CsvParser class is used to parse CSV into POJOs.

The class hierarchy for the builder of this parser is:

Context.BuilderMarshallingContextable.BuilderParser.BuilderReaderParser.BuilderCsvParser.Builder

Refer to the builder javadocs for configurable settings.

The following pre-configured parsers are provided for convenience:

CsvParserDEFAULT

Usage​

// Parse a CSV string into a list of beans
String csv = "name,age\nAlice,30\nBob,25\n";
List<Person> people = CsvParser.DEFAULT.read(csv, List.class, Person.class);

// Parse into a list of maps
List<Map<String,String>> rows = CsvParser.DEFAULT.read(csv, List.class, Map.class);

// Parse into a single bean
String singleRow = "name,age\nAlice,30\n";
Person p = CsvParser.DEFAULT.read(singleRow, Person.class);

// Use the Csv marshaller for convenience
List<Person> people = Csv.to(csv, List.class, Person.class);

Supported Target Types​

Target TypeParsing Strategy
Collection<Bean> / Bean[]Header row → property names; each data row → one bean.
Collection<Map> / Map[]Header row → map keys; each data row → one map.
Collection<SimpleType> / SimpleType[]value column; each row → one value coerced to element type.
Single BeanHeader row + one data row → one bean.
Single MapHeader row + one data row → one map.
ObjectReturns a JsonList of JsonMap entries (all values as strings).

Type Coercion​

Cell values are strings in CSV. The parser automatically coerces them to the target property/element type using the same type conversion machinery used by all Juneau parsers:

  • Primitive types and their wrappers (int, long, double, boolean, etc.)
  • String
  • Enum types (by name)
  • BigDecimal, BigInteger
  • Date/time types (Date, Calendar, Temporal subtypes) — as ISO 8601 strings
  • Duration — as ISO 8601 duration strings (e.g. PT1H30M)
  • Any type whose ClassMeta supports convertToType(String)

Null Handling​

The unquoted literal string null in a CSV cell is parsed as Java null. A quoted "null" cell is parsed as the Java String "null".

Quoted Fields​

The parser handles all RFC 4180 quoted field constructs:

  • "hello, world" → hello, world (comma inside quoted field)
  • "line1\nline2" → line1\nline2 (newline inside quoted field)
  • "say ""hi""" → say "hi" (doubled quotes → single quote)

Unknown Properties​

By default, CSV columns that do not match any bean property are silently ignored (the ignoreUnknownBeanProperties parser setting applies). To throw on unknown properties, configure the parser:

CsvParser p = CsvParser.create()
.disableIgnoreMissingSetters()
.build();

Swaps​

Object swaps registered on the parser are applied during cell value conversion:

CsvParser p = CsvParser.create().swaps(DateSwap.class).build();
List<Event> events = p.read(csv, List.class, Event.class);

Media Type​

CsvParser consumes input of media type text/csv.