Default instances

Cells

Basic types

The following types have CellCodec instances available out of the box:

java.util.Date

There also is a default CellCodec instance available for Date, but this one is slightly more complicated. There are so many different ways of writing dates that there is no reasonable default behaviour - one might argue that defaulting to ISO 8601 might make sense, but there doesn’t appear to be a sane way of implementing that in Java’s crusty date / time API.

Instead of providing a default implementation that is likely going to be incorrect for most people, kantan.csv provides easy ways of creating CellEncoder, CellDecoder and CellCodec instances provided you can create a DateFormat for you dates:

Either

For any two types A and B that each have a CellEncoder, there exists a CellEncoder[Either[A, B]].

By the same token, for any two types A and B that each have a CellDecoder, there exists a CellDecoder[Either[A, B]].

This is useful for dodgy CSV data where the type of a column is not well defined - it might sometimes be an int, sometimes a boolean, for example.

Option

For any type A that as a CellEncoder, there exists a CellEncoder[Option[A]].

By the same token, for any type A that as a CellDecoder, there exists a CellDecoder[Option[A]].

This is useful for CSV data where some fields are optional and an empty value is legal.

Composition

It’s important to realise that these instances compose automatically. Int has a CellDecoder, which means that Option[Int] does to. Boolean has a CellDecoder, which means that Either[Option[Int], Boolean] does too.

Rows

Single columns

For any type A that has a CellEncoder, there exists a RowEncoder[A].

By the same token, for any type A that has a CellDecoder, there exists a RowDecoder[A].

This is useful for CSV data where each row is composed of a single column.

Tuples

Tuples of any arity, provided they’re composed of types that all have a CellEncoder, have a RowEncoder.

By the same token, tuples of any arity, provided they’re composed of types that all have a CellDecoder, have a RowDecoder.

For example, (Int, String, Either[Boolean, Option[Double]]) has both a RowEncoder and a RowDecoder.

Collections

For any first order type F that has a CanBuildFrom and type A that has a CellDecoder, there exists a RowDecoder[F[A]].

For any type F that extends TraversableOnce and A that has a CellEncoder, there exists a RowEncoder[F[A]].

This is useful for CSV data where each row is composed of homogeneous data.

Either

For any two types A and B that each have a RowEncoder, there exists a RowEncoder[Either[A, B]].

By the same token, for any two types A and B that each have a RowDecoder, there exists a RowDecoder[Either[A, B]].

This is useful for dodgy CSV data where the type of a row is not well defined - it might sometimes be a Cat, sometimes a Dog, for example.

Option

For any type A that as a RowEncoder, there exists a RowEncoder[Option[A]].

By the same token, for any type A that as a RowDecoder, there exists a RowDecoder[Option[A]].

This is useful for CSV data where some rows might be empty. Note that this is a bit of a dodgy case: the RFC states that empty rows should be ignored, so serializing a collection that contains None values and then deserializing the result will not yield the original list.

CsvSource

The following types have an instance of CsvSource out of the box:

CsvSink

The following types have an instance of CsvSink out of the box:


Other tutorials: