66[ ![ Codecov] ( https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy?container=focus&url=https%3A%2F%2Fimg.shields.io%2Fcodecov%2Fc%2Fgithub%2Fbrudaswen%2Fkotlinx-serialization-csv%3Fstyle%3Dflat-square )] ( https://codecov.io/gh/brudaswen/kotlinx-serialization-csv )
77[ ![ License] ( https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy?container=focus&url=https%3A%2F%2Fimg.shields.io%2Fgithub%2Flicense%2Fbrudaswen%2Fkotlinx-serialization-csv%3Fstyle%3Dflat-square )] ( https://www.apache.org/licenses/LICENSE-2.0 )
88
9- Library to easily use Kotlin Serialization to serialize to/from CSV.
9+ Library to easily use * Kotlin Serialization* to serialize/parse CSV.
1010
1111## Gradle Dependencies
1212``` kotlin
@@ -18,15 +18,53 @@ implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.14.0")
1818```
1919
2020## Usage
21+ First configure your project according to the
22+ [ documentation] ( https://github.com/Kotlin/kotlinx.serialization#setup )
23+ of the * Kotlin Serialization* library.
24+
25+ ### CSV Example
2126``` kotlin
27+ @Serializable
28+ data class Person (val nickname : String , val name : String? , val appearance : Appearance )
29+
30+ @Serializable
31+ data class Appearance (val gender : Gender ? , val age : Int? , val height : Double? )
32+
33+ @Serializable
34+ enum class Gender { MALE , FEMALE }
35+
36+ fun main () {
37+ val csv = Csv (CsvConfiguration (hasHeaderRecord = true ))
38+
39+ val records = listOf (
40+ Person (" Neo" , " Thomas A. Anderson" , Appearance (Gender .MALE , 37 , 1.86 )),
41+ Person (" Trinity" , null , Appearance (Gender .FEMALE , null , 1.74 ))
42+ )
43+ val serialized = csv.stringify(Person .serializer().list, records)
44+ println (serialized)
45+ // nickname,name,appearance.gender,appearance.age,appearance.height
46+ // Neo,Thomas A. Anderson,MALE,37,1.86
47+ // Trinity,,FEMALE,,1.74
2248
49+ val input = """
50+ nickname,appearance.gender,appearance.height,appearance.age,name
51+ Neo,MALE,1.86,37,Thomas A. Anderson
52+ Trinity,FEMALE,1.74,,
53+ """ .trimIndent().replace(" \n " , " \r\n " )
54+ val parsed = csv.parse(Person .serializer().list, input)
55+ println (parsed)
56+ // [
57+ // Person(nickname=Neo, name=Thomas A. Anderson, appearance=Appearance(gender=MALE, age=37, height=1.86)),
58+ // Person(nickname=Trinity, name=null, appearance=Appearance(gender=FEMALE, age=null, height=1.74))
59+ // ]
60+ }
2361```
2462
2563## Requirements
2664
27- | Dependency | Versions |
28- | --- | --- |
29- | * Kotlin Serialization* | XXX |
65+ | Dependency | Versions |
66+ | --- | --- |
67+ | * Kotlin Serialization* | 0.14.0 |
3068
3169## License
3270
0 commit comments