kotlinx-serialization-csv
kotlinx-serialization-csv copied to clipboard
Please add at least some examples of usage
I am using multiple different formats but this CSV library usage looks to be different from common way of implementation for such libs in kotlinx.serialization, so it would be great to have examples of usage in readme
Can you please elaborate what you mean with looks different from common way?
I agree the readme does not contain many examples, but on the other hand the lib is very simple.
Can you please elaborate what you mean with
looks different from common way? I agree the readme does not contain many examples, but on the other hand the lib is very simple.
This README only shows how to serialize and deserialize a single element. Examples for multi-line elements are missing.
Such as:
firstName,lastName
John,Doe
Jack,Ma
Deserializing according to the sample code, only the first row of data can be obtained
Intuitively, it might be thought of like this:
CSVFormat.decodeFromString<List<Names>>(csv)
// or
CSVFormat.decodeFromString(List<Names>.serializer(), csv)
But actually it doesn't work
But actually it doesn't work
https://github.com/hfhbd/kotlinx-serialization-csv/blob/531003dfaa2842cead31de2e9fab2f26203fa2b5/kotlinx-serialization-csv/src/commonTest/kotlin/app/softwork/serialization/csv/CsvDecoderTest.kt#L131
Can you please add some configs or our environment? The format uses \n as line delimiter by default. You have to change it to \r\n if you use Windows (or files created on Windows).
https://github.com/hfhbd/kotlinx-serialization-csv/blob/531003dfaa2842cead31de2e9fab2f26203fa2b5/kotlinx-serialization-csv/src/commonTest/kotlin/app/softwork/serialization/csv/CsvDecoderTest.kt#L131
After using the following code, deserialization succeeded
CSVFormat.Default.decodeFromString(ListSerializer(Names.serializer()), csv)
Maybe this should be added to the README example. In kotlinx-serialization-json, people more often use generics: decodeFromString<List<Names>>. ListSerializer is rarely used
Both usages are supported and not part of csv, but part of the core library. For generics support, you need to import the method from the core library.