From 396bc895557dd7f5b50163e535c19898f93edf0d Mon Sep 17 00:00:00 2001 From: Stefan Oltmann Date: Wed, 30 Oct 2024 00:07:27 +0100 Subject: [PATCH] Enabled support for Cbor --- build.gradle.kts | 1 + src/main/kotlin/Routings.kt | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index d055cf3..4f5e559 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -56,6 +56,7 @@ dependencies { implementation("io.ktor:ktor-server-content-negotiation:$ktorVersion") implementation("io.ktor:ktor-server-compression:$ktorVersion") implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion") + implementation("io.ktor:ktor-serialization-kotlinx-cbor:$ktorVersion") implementation("io.ktor:ktor-server-cors:$ktorVersion") diff --git a/src/main/kotlin/Routings.kt b/src/main/kotlin/Routings.kt index 42c5663..67fce42 100644 --- a/src/main/kotlin/Routings.kt +++ b/src/main/kotlin/Routings.kt @@ -29,6 +29,7 @@ import io.ktor.http.ContentType import io.ktor.http.HttpHeaders import io.ktor.http.HttpMethod import io.ktor.http.HttpStatusCode +import io.ktor.serialization.kotlinx.cbor.cbor import io.ktor.serialization.kotlinx.json.json import io.ktor.server.application.Application import io.ktor.server.application.ApplicationCall @@ -53,6 +54,8 @@ import io.ktor.server.routing.routing import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.toList +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.cbor.Cbor import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import model.Cluster @@ -83,11 +86,18 @@ private val strictAllFieldsJson = Json { encodeDefaults = true } +@OptIn(ExperimentalSerializationApi::class) +private val strictAllFieldsCbor = Cbor { + ignoreUnknownKeys = false + encodeDefaults = true +} + private val logger = LoggerFactory.getLogger("Routings") /* Limit the results to avoid memory issues */ const val RESULT_LIMIT = 100 +@OptIn(ExperimentalSerializationApi::class) fun Application.configureRouting() { val startTime = System.currentTimeMillis() @@ -96,6 +106,7 @@ fun Application.configureRouting() { install(ContentNegotiation) { json(strictAllFieldsJson) + cbor(strictAllFieldsCbor) } install(Compression) { @@ -107,7 +118,11 @@ fun Application.configureRouting() { /* Only compress responses larger than 1 KB (for efficiency) */ minimumSize(1024) - matchContentType(ContentType.Application.Json, ContentType.Application.Zip) + matchContentType( + ContentType.Application.Json, + ContentType.Application.Cbor, + ContentType.Application.Zip + ) } }