SerializationConverter

class SerializationConverter : ContentConverter

ContentConverter with kotlinx.serialization.

Installation:

install(ContentNegotiation) {
   json()
   json(ContentType.Application.Json, Json.nonstrict)
   cbor()
   protoBuf()
}

Constructors

<init>

SerializationConverter(format: BinaryFormat)

Creates a converter serializing with the specified binary format.

SerializationConverter(
    format: StringFormat,
    defaultCharset: Charset = Charsets.UTF_8)

Creates a converter serializing with the specified string format and defaultCharset (optional, usually it is UTF-8).

SerializationConverter(json: Json = DefaultJson)
SerializationConverter()

This is no longer supported. Instead, specify format explicitly or use the corresponding DSL function.

Functions

convertForReceive

suspend fun convertForReceive(
    context: PipelineContext<ApplicationReceiveRequest, ApplicationCall>
): Any?

Convert a value (RAW or intermediate) from receive pipeline (deserialize). Pipeline PipelineContext.subject has ApplicationReceiveRequest.value of type ByteReadChannel

convertForSend

suspend fun convertForSend(
    context: PipelineContext<Any, ApplicationCall>,
    contentType: ContentType,
    value: Any
): Any?

Convert a value to the specified contentType to a value suitable for sending (serialize). Note that as far as ContentConverter could be registered multiple times with different content types hence contentType could be different depends on what the client accepts (inferred from Accept header). This function could ignore value if it is not suitable for conversion and return null so in this case other registered converters could be tried or this function could be invoked with other content types it the converted has been registered multiple times with different content types