Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/piotr-kalanski/csv2class
CSV reader/writer with conversion to Scala case class
https://github.com/piotr-kalanski/csv2class
converter csv io mapping reader scala writer
Last synced: 11 days ago
JSON representation
CSV reader/writer with conversion to Scala case class
- Host: GitHub
- URL: https://github.com/piotr-kalanski/csv2class
- Owner: piotr-kalanski
- License: apache-2.0
- Created: 2017-05-10T19:47:40.000Z (over 7 years ago)
- Default Branch: development
- Last Pushed: 2017-09-06T10:16:03.000Z (about 7 years ago)
- Last Synced: 2024-10-11T04:47:34.973Z (27 days ago)
- Topics: converter, csv, io, mapping, reader, scala, writer
- Language: Scala
- Homepage:
- Size: 38.1 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# csv2class
Generic CSV reader/writer with conversion to Scala case class without boilerplate[![Build Status](https://api.travis-ci.org/piotr-kalanski/csv2class.png?branch=development)](https://api.travis-ci.org/piotr-kalanski/csv2class.png?branch=development)
[![codecov.io](http://codecov.io/github/piotr-kalanski/csv2class/coverage.svg?branch=development)](http://codecov.io/github/piotr-kalanski/csv2class/coverage.svg?branch=development)
[](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22csv2class_2.11%22)
[![License](http://img.shields.io/:license-Apache%202-red.svg)](http://www.apache.org/licenses/LICENSE-2.0.txt)# Table of contents
- [Goals](#goals)
- [Getting started](#getting-started)
- [Examples](#examples)
- [Versioning support](#versioning-support)
- [Complex types](#complex-types)
- [Customizations](#customizations)# Goals
- Read and convert CSV files to Scala case classes
- Write Scala case classes to CSV# Getting started
Include dependency:
```scala
"com.github.piotr-kalanski" % "csv2class_2.11" % "0.3.3"
```or
```xml
com.github.piotr-kalanski
csv2class_2.11
0.3.3```
For reading from CSV import:
```scala
import com.datawizards.csv2class._
```For writing to CSV import:
```scala
import com.datawizards.class2csv._
```# Examples
## Reading from CSV
### Basic example:
CSV file:
```
s,i
first,10
second,11
```Parsing command:
```scala
case class Foo(s: String, i: Int)
parseCSV[Foo]("foo.csv")
```result:
```scala
Foo("first",10),
Foo("second",11)
```### Different order of columns
CSV file:
```csv
i,s
10,first
11,second
```Parsing command:
```scala
parseCSV[Foo]("foo.csv")
```result:
```scala
Foo("first",10),
Foo("second",11)
```### Returning not parsed rows
CSV file:
```
s,i
first,10
second,11
third,third
``````scala
parseCSV[Foo]("foo.csv")
```result:
```scala
Foo(first,10)
Foo(second,11)
java.lang.NumberFormatException: For input string: "third"
```## Writing to CSV
```scala
case class Foo(s: String, i: Int)val data = Seq(
Foo("first",10),
Foo("second",11)
)writeCSV(data, file)
```# Versioning support
First write below data to CSV:
```scala
case class PersonV2(name: String, age: Int, title: Option[String])
val peopleV2 = Seq(
PersonV2("p1", 10, Some("Developer")),
PersonV2("p2", 20, None),
PersonV2("p3", 30, None)
)
writeCSV(peopleV2, file)
```Read CSV file with previous version of compatible Person model:
```scala
case class Person(name: String, age: Int)
parseCSV[Person](file)
```result:
```scala
Seq(
Person("p1", 10),
Person("p2", 20),
Person("p3", 30)
)
```Read CSV file using newer compatible Person model version - new columns should be `Option` type.
```scala
case class PersonV3(name: String, age: Int, title: Option[String], salary: Option[Long])
parseCSV[PersonV3](file)
```result:
```scala
Seq(
PersonV3("p1", 10, Some("Developer"), None),
PersonV3("p2", 20, None, None),
PersonV3("p3", 30, None, None)
)
```# Complex types
Library supports writing fields with complex types (e.g. case class, Seq) by serializing them usin JSON format.
Example:
```scala
case class ClassWithArrayOfStruct(
id: String,
people: Seq[Person]
)
case class Person(name: String, age: Int)val data = Seq(
ClassWithArrayOfStruct("1",Seq(Person("p1", 10))),
ClassWithArrayOfStruct("2",Seq(Person("p1", 10),Person("p2", 20),Person("p3", 30)))
)writeCSV(data, file)
```
result:
```
id,people
1,"[{""name"":""p1"",""age"":10}]"
2,"[{""name"":""p1"",""age"":10},{""name"":""p2"",""age"":20},{""name"":""p3"",""age"":30}]"
```# Customizations
## Change delimiter
```scala
parseCSV[Foo]("file.csv", delimiter = ';')
writeCSV(data, file, delimiter = ';')
```## CSV without header
```scala
parseCSV[Foo]("file.csv", header = false, columns = Seq("s","i"))
writeCSV(data, "file.csv", header = false)
```