com.lightningkite.lightningserver.serialization.FormDataHandler.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.properties.Properties
open class FormDataHandler(
val properties: () -> Properties
) : Serialization.HttpContentHandler {
override val contentType: ContentType = ContentType.Application.FormUrlEncoded
override suspend fun invoke(content: HttpContent, serializer: KSerializer): T {
return properties().decodeFromFormData(serializer, content.text())
}
override suspend fun invoke(contentType: ContentType, serializer: KSerializer, value: T): HttpContent {
return HttpContent.Text(
properties().encodeToFormData(serializer, value),
contentType
)
}
}