kotlinx-serialization-csv icon indicating copy to clipboard operation
kotlinx-serialization-csv copied to clipboard

Please add at least some examples of usage

Open orchestr7 opened this issue 1 year ago • 1 comments

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

orchestr7 avatar May 07 '24 20:05 orchestr7

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.

hfhbd avatar May 07 '24 22:05 hfhbd

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

zhangzih4n avatar Nov 04 '24 07:11 zhangzih4n

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).

hfhbd avatar Nov 04 '24 07:11 hfhbd

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

zhangzih4n avatar Nov 04 '24 08:11 zhangzih4n

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.

hfhbd avatar Nov 04 '24 08:11 hfhbd