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

com.lightningkite.lightningserver.serialization.JsonFormatHandler.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.json.Json
import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.json.encodeToStream
import java.io.InputStream

class JsonFormatHandler(val json: () -> Json, val jsonWithoutDefaults: () -> Json) :
    StringFormatHandler(json, ContentType.Application.Json) {
    override suspend fun  invoke(contentType: ContentType, serializer: KSerializer, value: T): HttpContent {
        return if (contentType.parameters["defaults"] == "false") HttpContent.Text(
            jsonWithoutDefaults().encodeToString(serializer, value),
            contentType
        ) else super.invoke(contentType, serializer, value)
    }

    override suspend fun  streaming(contentType: ContentType, serializer: KSerializer, value: T): HttpContent {
        return HttpContent.OutStream(
            write = {
                (if (contentType.parameters["defaults"] == "false") jsonWithoutDefaults() else json()).encodeToStream(
                    serializer,
                    value,
                    it
                )
            },
            length = null,
            type = contentType
        )
    }

    override suspend fun  fromStream(contentType: ContentType, stream: InputStream, serializer: KSerializer): T {
        return json().decodeFromStream(serializer, stream)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy