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

com.cedarsoft.commons.ktor.ProtoBufSerializationConverter.kt Maven / Gradle / Ivy

The newest version!
package com.cedarsoft.commons.ktor

import io.ktor.application.ApplicationCall
import io.ktor.content.ByteArrayContent
import io.ktor.http.ContentType
import io.ktor.request.ApplicationReceiveRequest
import io.ktor.util.cio.toByteArray
import io.ktor.util.pipeline.PipelineContext
import kotlinx.coroutines.io.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.protobuf.ProtoBuf

val ContentType.Application.ProtoBuf: ContentType
  get() = ContentType("application", "protobuf")

/**
 * Serializer that converts to protocol buffers
 */
class ProtoBufSerializationConverter(private val protoBuf: ProtoBuf = ProtoBuf.plain) : KotlinxSerializationConverter() {
  override suspend fun deserialize(
    context: PipelineContext,
    contentType: ContentType,
    input: ByteReadChannel,
    serializer: KSerializer
  ): Any? {
    val bytes = input.toByteArray()
    return protoBuf.load(serializer, bytes)
  }

  override suspend fun serialize(
    context: PipelineContext,
    contentType: ContentType,
    value: Any,
    serializer: KSerializer
  ): Any? {
    return ByteArrayContent(
      bytes = protoBuf.dump(serializer, value),
      contentType = ContentType.Application.ProtoBuf
    )
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy