com.lightningkite.lightningserver.serialization.CsvFormatHandler.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of server-core Show documentation
Show all versions of server-core Show documentation
A set of tools to fill in/replace what Ktor is lacking in.
The newest version!
package com.lightningkite.lightningserver.serialization
import com.lightningkite.lightningserver.core.ContentType
import com.lightningkite.lightningserver.http.HttpContent
import kotlinx.serialization.KSerializer
import kotlinx.serialization.csv.Csv
import java.io.InputStream
class CsvFormatHandler(val csv: () -> Csv) : StringFormatHandler(csv, ContentType.Text.CSV) {
override suspend fun streaming(contentType: ContentType, serializer: KSerializer, value: T): HttpContent {
return HttpContent.OutStream(
write = { csv().encodeToAppendable(serializer, value, it.writer()) },
length = null,
type = contentType
)
}
override suspend fun fromStream(contentType: ContentType, stream: InputStream, serializer: KSerializer): T {
return csv().decodeFromReader(serializer, stream.bufferedReader())
}
}