kotlin-grass

Csv File to Kotlin Data Class Parser

License

License

Categories

Categories

Kotlin Languages
GroupId

GroupId

io.github.blackmo18
ArtifactId

ArtifactId

kotlin-grass-jvm
Last Version

Last Version

0.7.1
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

kotlin-grass
Csv File to Kotlin Data Class Parser
Project URL

Project URL

https://github.com/blackmo18/kotlin-grass
Project Organization

Project Organization

io.github.blackmo18
Source Code Management

Source Code Management

https://github.com/blackmo18/kotlin-grass

Download kotlin-grass-jvm

Dependencies

runtime (3)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib-common jar 1.4.32
org.jetbrains.kotlin : kotlin-reflect jar 1.4.32
org.jetbrains.kotlinx : kotlinx-coroutines-core-jvm jar 1.4.3

Project Modules

There are no modules declared in this project.

Kotlin-Grass

Kotlin Latest: 0.7.1 License: Apache License 2.0 CodeFactor CodeFactor

Csv File to Kotlin Data Class Parser
Currently, it requires to have @ExperimentalStdlibApi on the class/method using this Library.
Requires kotlin-csv by doyaaaaaken for reading csv file.

Links

Features

1. Simple And Direct

  • No hard configuration
  • No invasive annotations to data class
  • Custom mapping
  • Nullable Data Types

2. Primitive Types

  • Short
  • Int
  • Long
  • Float
  • Double
  • Boolean
  • String

3. Support for Java 8 Date Time Apis

  • LocalTime
  • LocalDateTime
  • LocalDate
  • Custom Formatting

Usage

Gradle DSL:

//doyaaaaaken's kotlin-csv
implementation("com.github.doyaaaaaken:kotlin-csv-jvm:0.15.2")
//kotlin-grass
implementation("io.github.blackmo18:kotlin-grass-jvm:0.7.1")

Maven:

<dependency>
    <groupId>com.github.doyaaaaaken</groupId>
    <artifactId>kotlin-csv-jvm</artifactId>
    <version>0.15.2</version>
</dependency>
<dependency>
    <groupId>io.github.blackmo18</groupId>
    <artifactId>kotlin-grass-jvm</artifactId>
    <version>0.7.1</version>
</dependency>

Examples

CSV file

short int long float double boolean string
0 1 2 3.0 4.0 true hello

Declaring data class

    data class PrimitiveTypes(
        val short: Short,
        val int: Int,
        val long: Long,
        val float: Float,
        val double: Double,
        val boolean: Boolean,
        val string: String
    )

Nullable Data Types

If a variable in your data class is a nullable, all you have to do is mark it with ?

    data class NullableData(
        val nullableString: String?,
        val nullableInt: Int? = null,
        ...
    )

Parsing to data class

    val csvContents = csvReader().readAllWithHeader(file)
    val dataClasses = grass<PrimitiveTypes>().harvest(csvContents)

Parsing to data class using Kotlin Flow

    val contents = File("file/path").inputStream()
    val dataClasses: Flow<PrimitiveTypes> = csvReader().openAsync(contents) {
        val data = readAllWithHeaderAsSequence().asFlow()
        grass<PrimitiveTypes>().harvest(data)
    }

Custom Configuration

Option default value description
dateFormat yyyy-MM-dd date format
timeFormat HH:mm time format
dateTimeSeparator (space) date time separator
trimWhiteSpace true trims white spaces on csv entries
ignoreUnknownFields false ignore unknown / unmapped fields in input
caseSensitive true case sensitive header matching
customKeyMap null Map<String,String> custom key mapping, priority if not empty or null
customKeyMapDataProperty null Map<String, KProperty<*>> custom key mapping

Java Date Time API Support

csv file

time datetime date
12:00 2020-12-31 12:00 2020-12-31

Date and Time Types

    data class DateTimeTypes(
        val time: LocalTime,
        val datetime: LocalDateTime,
        val date: LocalDate,
    )

Customize Formatting

    val grass = grass<DateTimeTypes> {
        dateFormat = "MM-dd-yyyy"
        timeFormat = "HH:mm:ss"
        dateTimeSeparator = "/"
    }

Custom Mapping Support

CSV file

hour birthdate
12:00 2020-12-31

Code

    data class DateTime(
        val time: LocalTime,
        val date: LocalDate,
    )

    val grass = grass<DateTimeTypes> {
        customKeyMap = mapOf("hour" to "time", "birthdate" to "date")
    }

    // or

    val grass = grass<DateTimeTypes> {
        customKeyMapDataProperty = mapOf("hour" to DateTime::time, "birthdate" to DateTime::date)
    }

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2020 blackmo18.
This project is Apache License 2.0 licensed.


This project inspired ❤️ by kotlin-csv

Versions

Version
0.7.1
0.7.0
0.6.0