About a year ago, I needed a CSV/TSV parser usable...
# feed
s
About a year ago, I needed a CSV/TSV parser usable with Kotlin Multiplatform, and wasn’t satisfied with any of the existing options at the time, so I wrote my own into my work-in-progress app. Today, I’ve carved that module out and released it as a library: https://github.com/sargunv/kotlin-dsv. The library supports encoding / decoding lists of flat objects to and from delimiter-separated-values with kotlinx-serialization, supports streaming I/O with kotlinx-io, supports all current KMP targets, and should handle quoting and escaping correctly as defined in RFC 4180 and tested with the csv-spectrum test suite.
K 10
❤️ 3
🎉 1
🙌 1
🆒 1
f
Needed CSV import/export for my KMP side-project. Perfect timing!
t
This is great! Thank you. I'm curious about how this library matches CSV values to properties in a Kotlin data class. Does it use the header row to map column names to class property names? And if there’s no header row, does it just map values to properties based on their position in the row and the class?
s
It uses the header row
t
What if there isn't a header row in the csv? Or does it need to have one
s
It currently requires a header row. Happy to accept a PR for a sequential decoding mode without a header row
There is however a low level parser for parsing CSV with or without a header row: https://sargunv.github.io/kotlin-dsv/api/kotlin-dsv/dev.sargunv.kotlindsv/-dsv-parser/index.html
t
Awesome. Thanks!
b
Impressively thorough work, @Sargun Vohra. Bravo for the multiplatform support, the integration with other kotlinx-* components, the robust testing, and the friendly examples. 👏
s
Update: just released a new version that fixes some bugs I found after adding a more challenging test suite. The fixes: • Utf-8 is now handled properly. Previously it would break when a multi-byte character crossed the buffer boundary • Newline handling is a bit more lenient to accept extra carriage returns (CRCRLF) instead of just CRLF and LF • Header to class mapping is a bit more lenient to ignore extraneous spaces around the column name when matching to a property name • An opt-in feature to skip empty lines found when parsing the CSV
K 2
s
Nice work!