kotlinx-serialization-csv
3.2.2indexedFacilitates serializing and parsing CSV data, supporting various record types. Offers configurable options and pre-defined formats, optimizing performance for fixed-column CSV files.
Facilitates serializing and parsing CSV data, supporting various record types. Offers configurable options and pre-defined formats, optimizing performance for fixed-column CSV files.
Library to easily use Kotlin Serialization to serialize/parse CSV.
All types of record classes are supported (primitives, classes, enums, nested classes, ...). However, CSV serialization works best if the column number is fixed. So, collections (lists, sets, maps) and open classes should be avoided.
// Kotlin Serialization CSV
implementation("de.brudaswen.kotlinx.serialization:kotlinx-serialization-csv:3.2.2")
// Kotlin Serialization is added automatically, but can be added to force a specific version
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.9.0")
First configure your project according to the documentation of the Kotlin Serialization library.
The library comes with multiple pre-defined Csv formats that can be used out of the box.
| Config | Description |
|---|---|
CSV serialization and parsing options can be changed by configuring the Csv instance during
initialization via the Csv { } builder function.
| Dependency | Versions |
|---|---|
| Kotlin Serialization | 1.9.0 |
Copyright 2020 Sven Obser
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http:
Unless applicable law agreed to writing, software
distributed under the License distributed an BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
See the License the specific language governing permissions
limitations under the License.
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.csv.Csv
data class Person(val nickname: String, val name: String?, val appearance: Appearance)
data class Appearance(val gender: Gender?, val age: Int?, val height: Double?)
enum class Gender { MALE, FEMALE }
fun main() {
val csv = Csv { hasHeaderRecord = true }
val records = listOf(
Person("Neo", "Thomas A. Anderson", Appearance(Gender.MALE, 37, 1.86)),
Person("Trinity", null, Appearance(Gender.FEMALE, null, 1.74))
)
val serialized = csv.encodeToString(ListSerializer(Person.serializer()), records)
println(serialized)
// nickname,name,appearance.gender,appearance.age,appearance.height
// Neo,Thomas A. Anderson,MALE,37,1.86
// Trinity,,FEMALE,,1.74
val input = """
nickname,appearance.gender,appearance.height,appearance.age,name
Neo,MALE,1.86,37,Thomas A. Anderson
Trinity,FEMALE,1.74,,
""".trimIndent()
val parsed = csv.decodeFromString(ListSerializer(Person.serializer()), input)
println(parsed)
// [
// Person(nickname=Neo, name=Thomas A. Anderson, appearance=Appearance(gender=MALE, age=37, height=1.86)),
// Person(nickname=Trinity, name=null, appearance=Appearance(gender=FEMALE, age=null, height=1.74))
// ]
}
Csv.DefaultStandard Comma Separated Value format, as for Rfc4180 but using Unix newline (\n) as record separator and ignoring empty lines. Format is unstable and may change in upcoming versions. |
Csv.Rfc4180 | Comma separated format as defined by RFC 4180. |
| Option | Default Value | Description |
|---|
delimiter | , | The delimiter character between columns. |
recordSeparator | \n | The record separator. |
quoteChar | " | The quote character used to quote column values. |
quoteMode | MINIMAL | The quote mode used to decide if a column value should get quoted.
|
escapeChar | null | The escape character used to escape reserved characters in a column value. |
nullString | empty string | The value to identify null values. |
ignoreEmptyLines | true | Ignore empty lines during parsing. |
hasHeaderRecord | false | First line is header record. |
headerSeparator | . | Character that is used to separate hierarchical header names. |
ignoreUnknownColumns | false | Ignore unknown columns (only has effect when hasHeaderRecord is enabled). |
hasTrailingDelimiter | false | If records end with a trailing delimiter. |
shortValueClassHeaderName | false | Use short value class header name. |
Surfaced from shared tags and platforms — no rankings paid for.