Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

io.github.blackmo18:kotlin-grass-parser

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

io.github.blackmo18:kotlin-grass-parser

Csv File to Kotlin Data Class Parser

  • 0.8.0
  • Source
  • Maven
  • Socket score

Version published
Maintainers
1
Source
Kotlin-Grass

Kotlin Parser: 0.8.0 Core: 0.8.0 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.

  • Change Log - updates and bug fixes

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-core-jvm:1.0.0")
implementation("io.github.blackmo18:kotlin-grass-parser-jvm:0.8.0")
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-core-jvm</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>io.github.blackmo18</groupId>
    <artifactId>kotlin-grass-parser-jvm</artifactId>
    <version>0.8.0</version>
</dependency>

Examples

CSV file
shortintlongfloatdoublebooleanstring
0123.04.0truehello
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

Optiondefault valuedescription
dateFormatyyyy-MM-dddate format
timeFormatHH:mmtime format
dateTimeSeparator(space)date time separator
trimWhiteSpacetruetrims white spaces on csv entries
ignoreUnknownFieldsfalseignore unknown / unmapped fields in input
caseSensitivetruecase sensitive header matching
customKeyMapnullMap<String,String> custom key mapping, priority if not empty or null
customKeyMapDataPropertynullMap<String, KProperty<*>> custom key mapping

Java Date Time API Support

csv file
timedatetimedate
12:002020-12-31 12:002020-12-31
Date and Time Types
Import the following extension library
implementation("com.vhl.blackmo:kotlin-grass-date-time-jvm:0.8.0")
    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 = "/"
        customDataTypes = arrayListOf(Java8DateTime)
    }

Custom Mapping Support

CSV file
hourbirthdate
12:002020-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

FAQs

Package last updated on 08 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc