Skip to content

Commit

Permalink
Changed default serialization mechanism to kotlinx serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaro committed Jan 8, 2024
1 parent 4c77a37 commit 59507f8
Show file tree
Hide file tree
Showing 45 changed files with 140 additions and 9 deletions.
1 change: 1 addition & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ dependencies {
///// Kotlin
implementation group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: version_kotlin
implementation group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: version_kotlin
implementation group: 'org.jetbrains.kotlinx', name:'kotlinx-serialization-json', version: '1.6.2'
}

kapt {
Expand Down
42 changes: 42 additions & 0 deletions backend/src/main/kotlin/dev/dres/api/rest/KotlinxJsonMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dev.dres.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.json.Json
import kotlinx.serialization.serializer
import java.lang.reflect.Type

object KotlinxJsonMapper : JsonMapper {

private val fallbackMapper = jacksonObjectMapper()

override fun <T : Any> fromJsonString(json: String, targetType: Type): T {

return try {
@Suppress("UNCHECKED_CAST")
val serializer = serializer(targetType) as KSerializer<T>
Json.decodeFromString(serializer, json)
} catch (e: SerializationException) {
null
} catch (e: IllegalStateException) {
null
} ?: fallbackMapper.readValue(json, fallbackMapper.typeFactory.constructType(targetType))

}

override fun toJsonString(obj: Any, type: Type): String {

return try {
val serializer = serializer(type)
Json.encodeToString(serializer, obj)
} catch (e: SerializationException) {
null
} catch (e: IllegalStateException) {
null
} ?: fallbackMapper.writeValueAsString(obj)

}
}
7 changes: 5 additions & 2 deletions backend/src/main/kotlin/dev/dres/api/rest/RestApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ object RestApi {
)

javalin = Javalin.create {

it.jsonMapper(KotlinxJsonMapper)

it.plugins.enableCors { cors ->
cors.add { corsPluginConfig ->
corsPluginConfig.reflectClientOrigin =
Expand Down Expand Up @@ -381,8 +384,8 @@ object RestApi {


private val pool = QueuedThreadPool(
min(Runtime.getRuntime().availableProcessors() * 2, 8),
8,
32,
10,
60000,
-1,
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import io.javalin.http.BadRequestResponse
import io.javalin.http.Context
import io.javalin.openapi.*
import jetbrains.exodus.database.TransientEntityStore
import kotlinx.serialization.Serializable

/**
* A [PostRestHandler] that handles user-requests to login.
Expand All @@ -28,6 +29,7 @@ class LoginHandler() : RestHandler, PostRestHandler<ApiUser> {
override val route = "login"
override val apiVersion = RestApi.LATEST_API_VERSION

@Serializable
data class LoginRequest(var username: String, var password: String)

@OpenApi(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dev.dres.data.model.media.*
import kotlinx.dnq.query.filter
import kotlinx.dnq.query.first
import kotlinx.dnq.util.findById
import kotlinx.serialization.Serializable

/**
* The RESTful API equivalent for [DbMediaItem].
Expand All @@ -13,6 +14,7 @@ import kotlinx.dnq.util.findById
* @author Ralph Gasser
* @version 1.1.0
*/
@Serializable
data class ApiMediaItem(
override val mediaItemId: MediaItemId,
override val name: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package dev.dres.api.rest.types.collection

import kotlinx.serialization.Serializable

@Serializable
data class ApiMediaItemMetaDataEntry(val key: String, val value: String)
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package dev.dres.api.rest.types.collection.time

import dev.dres.data.model.media.time.TemporalPoint
import kotlinx.serialization.Serializable

/**
* RESTful API representation of a [TemporalPoint].
*
* @version 1.0.0
* @author Ralph Gasser
*/
@Serializable
data class ApiTemporalPoint(val value: String, val unit: ApiTemporalUnit) {

companion object{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package dev.dres.api.rest.types.collection.time

import dev.dres.data.model.media.time.TemporalRange
import kotlinx.serialization.Serializable

/**
* RESTful API representation of a [TemporalRange].
*
* @version 1.0.0
* @author Ralph Gasser
*/
@Serializable
data class ApiTemporalRange(val start: ApiTemporalPoint, val end: ApiTemporalPoint) {
constructor(range: TemporalRange) : this(ApiTemporalPoint.fromTemporalPoint(range.start), ApiTemporalPoint.fromTemporalPoint(range.end))
fun toTemporalRange(fps: Float): TemporalRange = TemporalRange(start.toTemporalPoint(fps), end.toTemporalPoint(fps))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import dev.dres.run.InteractiveAsynchronousRunManager
import dev.dres.run.InteractiveSynchronousRunManager
import dev.dres.run.NonInteractiveRunManager
import dev.dres.run.RunManager
import kotlinx.serialization.Serializable

/**
* Contains the basic and most importantly static information about a [RunManager] that is relevant to a participant.
*
*/
@Serializable
data class ApiClientEvaluationInfo(
val id: String,
val name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package dev.dres.api.rest.types.evaluation

import dev.dres.api.rest.types.template.tasks.ApiTaskTemplate
import dev.dres.data.model.template.task.DbTaskTemplate
import kotlinx.serialization.Serializable

/**
* Basic and most importantly static information about a [DbTaskTemplate] relevant to the participant.
*/
@Serializable
data class ApiClientTaskTemplateInfo(
val name: String,
val taskGroup: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dev.dres.api.rest.types.template.ApiEvaluationTemplate
import dev.dres.data.model.run.DbEvaluation
import dev.dres.data.model.run.interfaces.Evaluation
import dev.dres.data.model.run.interfaces.EvaluationId
import kotlinx.serialization.Serializable

/**
* The RESTful API equivalent of a [DbEvaluation].
Expand All @@ -12,6 +13,7 @@ import dev.dres.data.model.run.interfaces.EvaluationId
* @author Ralph Gasser
* @version 2.0.0
*/
@Serializable
data class ApiEvaluation(
override val evaluationId: EvaluationId,
val name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dev.dres.run.InteractiveAsynchronousRunManager
import dev.dres.run.InteractiveSynchronousRunManager
import dev.dres.run.NonInteractiveRunManager
import dev.dres.run.RunManager
import kotlinx.serialization.Serializable

/**
* Contains the basic and most importantly static information about a [RunManager].
Expand All @@ -14,6 +15,7 @@ import dev.dres.run.RunManager
* @author Ralph Gasser
* @version 1.1.0
*/
@Serializable
data class ApiEvaluationInfo(
val id: String,
val name: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dev.dres.api.rest.types.evaluation

import dev.dres.run.*
import kotlinx.serialization.Serializable

@Serializable
data class ApiEvaluationOverview(val state: RunManagerStatus, val teamOverviews: List<ApiTeamTaskOverview>) {
companion object {
fun of(manager: InteractiveRunManager): ApiEvaluationOverview {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.dres.api.rest.types.evaluation

import dev.dres.data.model.run.RunActionContext
import dev.dres.run.*
import kotlinx.serialization.Serializable

/**
* Contains the information about the state of an [InteractiveRunManager].
Expand All @@ -10,6 +11,7 @@ import dev.dres.run.*
*
* @version 1.3.0
*/
@Serializable
data class ApiEvaluationState(
val evaluationId: String,
val evaluationStatus: ApiEvaluationStatus,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.dres.api.rest.types.evaluation

import dev.dres.api.rest.types.evaluation.submission.ApiVerdictStatus
import kotlinx.serialization.Serializable

/**
* Data transfer object for overriding a [ApiAnswerSet]'s verdict.
Expand All @@ -9,4 +10,5 @@ import dev.dres.api.rest.types.evaluation.submission.ApiVerdictStatus
* @author Loris Sauter
* @version 1.0
*/
@Serializable
data class ApiOverrideAnswerSetVerdictDto(val verdict: ApiVerdictStatus)
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package dev.dres.api.rest.types.evaluation
import dev.dres.api.rest.types.evaluation.submission.ApiSubmission
import dev.dres.data.model.run.interfaces.EvaluationId
import dev.dres.data.model.template.TemplateId
import kotlinx.serialization.Serializable

/**
* Encodes [ApiSubmission] data for a specific [EvaluationId] and (optionally) [EvaluationId].
*
* @author Loris Sauter
* @version 1.1.0
*/
@Serializable
data class ApiSubmissionInfo(val evaluationId: EvaluationId, val taskId: TemplateId, val submissions: List<ApiSubmission>)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dev.dres.api.rest.types.evaluation.submission.ApiAnswerSet
import dev.dres.data.model.run.DbTask
import dev.dres.data.model.run.TaskId
import dev.dres.data.model.template.TemplateId
import kotlinx.serialization.Serializable

/**
* The RESTful API equivalent of a [DbTask].
Expand All @@ -12,6 +13,7 @@ import dev.dres.data.model.template.TemplateId
* @author Ralph Gasser
* @version 2.0.0
*/
@Serializable
data class ApiTask(
val taskId: TaskId,
val templateId: TemplateId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dev.dres.api.rest.types.evaluation

import dev.dres.data.model.run.interfaces.TaskRun
import kotlinx.serialization.Serializable

@Serializable
data class ApiTaskOverview(
val id: String,
val name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.dres.api.rest.types.evaluation

import dev.dres.api.rest.types.template.tasks.ApiTaskTemplate
import dev.dres.data.model.template.task.DbTaskTemplate
import kotlinx.serialization.Serializable

/**
* Basic and most importantly static information about a [DbTaskTemplate].
Expand All @@ -11,6 +12,7 @@ import dev.dres.data.model.template.task.DbTaskTemplate
* @author Ralph Gasser & Loris Sauter
* @version 1.2.0
*/
@Serializable
data class ApiTaskTemplateInfo(
val templateId: String,
val name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dev.dres.api.rest.types.template.team.ApiTeam
import dev.dres.data.model.template.team.DbTeam
import dev.dres.data.model.run.InteractiveSynchronousEvaluation
import dev.dres.data.model.template.team.TeamId
import kotlinx.serialization.Serializable

/**
* Basic and most importantly static information about the [DbTeam] partaking in a [InteractiveSynchronousEvaluation].
Expand All @@ -13,6 +14,8 @@ import dev.dres.data.model.template.team.TeamId
* @author Ralph Gasser
* @version 1.1.0
*/

@Serializable
data class ApiTeamInfo(val id: TeamId, val name: String, val color: String) {
constructor(team: ApiTeam) : this(team.id!!, team.name!!, team.color!!)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package dev.dres.api.rest.types.evaluation

import kotlinx.serialization.Serializable

@Serializable
data class ApiTeamTaskOverview(val teamId: String, val tasks: List<ApiTaskOverview>)
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package dev.dres.api.rest.types.evaluation

import kotlinx.serialization.Serializable

/**
* Basic information regarding a viewer instance
*
* @author Ralph Gasser
* @version 1.1.0
*/
@Serializable
data class ApiViewerInfo(val viewersId: String, val username: String, val host: String, val ready: Boolean)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.dres.api.rest.types.evaluation.submission
import dev.dres.api.rest.types.collection.ApiMediaItem
import dev.dres.data.model.submissions.Answer
import dev.dres.data.model.submissions.AnswerType
import kotlinx.serialization.Serializable

/**
* The RESTful API equivalent for the type of answer as submitted by the DRES endpoint.
Expand All @@ -13,6 +14,7 @@ import dev.dres.data.model.submissions.AnswerType
* @author Ralph Gasser
* @version 1.0.0
*/
@Serializable
data class ApiAnswer(
/** The [ApiAnswerType] of this [ApiAnswer]. */
val type: ApiAnswerType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore
import dev.dres.data.model.run.TaskId
import dev.dres.data.model.submissions.*
import io.javalin.openapi.OpenApiIgnore
import kotlinx.serialization.Serializable

/**
* The RESTful API equivalent for the type of answer set as submitted by the DRES endpoint.
Expand All @@ -14,6 +15,7 @@ import io.javalin.openapi.OpenApiIgnore
* @author Ralph Gasser
* @version 1.0.0
*/
@Serializable
data class ApiAnswerSet(
override val id: AnswerSetId,
val status: ApiVerdictStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dev.dres.data.model.submissions.DbAnswerType
import io.javalin.openapi.OpenApiIgnore
import kotlinx.dnq.query.filter
import kotlinx.dnq.query.singleOrNull
import kotlinx.serialization.Serializable

/**
* The RESTful API equivalent of an answer as provided by a client of the submission API endpoints.
Expand All @@ -17,7 +18,8 @@ import kotlinx.dnq.query.singleOrNull
* @author Luca Rossetto
* @version 1.0.0
*/
data class ApiClientAnswer(
@Serializable
data class ApiClientAnswer( //TODO add optional relevance score field
/** The text that is part of this [ApiClientAnswer]. */
val text: String? = null,

Expand Down
Loading

0 comments on commit 59507f8

Please sign in to comment.