From ef2b77c3770bebcfe0a35e9f4845478ce17386ca Mon Sep 17 00:00:00 2001 From: faberf Date: Wed, 17 Jul 2024 18:45:56 +0200 Subject: [PATCH] naiv jsonmapper --- .../kotlin/org/vitrivr/engine/server/Main.kt | 2 +- .../server/api/rest/KotlinxJsonMapper.kt | 68 ++++++++++--------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/vitrivr-engine-server/src/main/kotlin/org/vitrivr/engine/server/Main.kt b/vitrivr-engine-server/src/main/kotlin/org/vitrivr/engine/server/Main.kt index 1a5b1cce..9ecc38e0 100644 --- a/vitrivr-engine-server/src/main/kotlin/org/vitrivr/engine/server/Main.kt +++ b/vitrivr-engine-server/src/main/kotlin/org/vitrivr/engine/server/Main.kt @@ -1,5 +1,6 @@ package org.vitrivr.engine.server +import KotlinxJsonMapper import io.github.oshai.kotlinlogging.KLogger import io.github.oshai.kotlinlogging.KotlinLogging import io.javalin.Javalin @@ -12,7 +13,6 @@ import org.vitrivr.engine.core.config.pipeline.execution.ExecutionServer import org.vitrivr.engine.core.model.metamodel.SchemaManager import org.vitrivr.engine.server.api.cli.Cli import org.vitrivr.engine.server.api.cli.commands.SchemaCommand -import org.vitrivr.engine.server.api.rest.KotlinxJsonMapper import org.vitrivr.engine.server.api.rest.configureApiRoutes import org.vitrivr.engine.server.api.rest.model.ErrorStatus import org.vitrivr.engine.server.api.rest.model.ErrorStatusException diff --git a/vitrivr-engine-server/src/main/kotlin/org/vitrivr/engine/server/api/rest/KotlinxJsonMapper.kt b/vitrivr-engine-server/src/main/kotlin/org/vitrivr/engine/server/api/rest/KotlinxJsonMapper.kt index 29c1c167..387718a7 100644 --- a/vitrivr-engine-server/src/main/kotlin/org/vitrivr/engine/server/api/rest/KotlinxJsonMapper.kt +++ b/vitrivr-engine-server/src/main/kotlin/org/vitrivr/engine/server/api/rest/KotlinxJsonMapper.kt @@ -1,51 +1,55 @@ -package org.vitrivr.engine.server.api.rest - -import com.fasterxml.jackson.core.type.TypeReference -import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import io.javalin.json.JsonMapper -import kotlinx.serialization.KSerializer -import kotlinx.serialization.SerializationException +import kotlinx.serialization.* import kotlinx.serialization.json.Json +import kotlinx.serialization.modules.SerializersModule import kotlinx.serialization.serializer -import org.vitrivr.engine.core.features.metadata.source.exif.logger +import kotlin.reflect.KType +import java.lang.reflect.ParameterizedType import java.lang.reflect.Type +import kotlin.reflect.KTypeProjection +import kotlin.reflect.full.createType object KotlinxJsonMapper : JsonMapper { - private val fallbackMapper = jacksonObjectMapper() - - override fun fromJsonString(json: String, targetType: Type): T { + private val json = Json { + serializersModule = SerializersModule { } + isLenient = true + ignoreUnknownKeys = true + } + override fun fromJsonString(jsonString: String, targetType: Type): T { return try { - @Suppress("UNCHECKED_CAST") - val serializer = serializer(targetType) as KSerializer - Json.decodeFromString(serializer, json) + val kType : KType = targetType.toKType() + val serializer = json.serializersModule.serializer(kType) + json.decodeFromString(serializer, jsonString) as T } catch (e: SerializationException) { - "Error while deserializing JSON: ${e.message}".let { - logger.error { it } - throw Exception(it) - } - null + throw Exception("Error while deserializing JSON: ${e.message}") } catch (e: IllegalStateException) { - "Error state: ${e.message}".let { - logger.error { it } - throw Exception(it) - } - null - } ?: fallbackMapper.readValue(json, fallbackMapper.typeFactory.constructType(targetType)) - + throw Exception("Error state: ${e.message}") + } } override fun toJsonString(obj: Any, type: Type): String { - return try { - val serializer = serializer(type) - Json.encodeToString(serializer, obj) + val kType = type.toKType() + val serializer = json.serializersModule.serializer(kType) + json.encodeToString(serializer, obj) } catch (e: SerializationException) { - null + throw Exception("Error while serializing JSON: ${e.message}") } catch (e: IllegalStateException) { - null - } ?: fallbackMapper.writeValueAsString(obj) + throw Exception("Error state: ${e.message}") + } + } + private fun Type.toKType(): KType { + return when (this) { + is ParameterizedType -> { + val rawType = (this.rawType as Class<*>).kotlin + val args = this.actualTypeArguments.map { it.toKType() } + rawType.createType(args.map { KTypeProjection.invariant(it) }) + } + is Class<*> -> this.kotlin.createType() + else -> throw IllegalArgumentException("Unsupported Type: $this") + } } -} \ No newline at end of file +}