All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lightningkite.lightningserver.serialization.CsvFormatHandler.kt Maven / Gradle / Ivy

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())
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy