From 4dc8296cc3041d5c5d24626ecfba49c17d295e4d Mon Sep 17 00:00:00 2001 From: Chaoran Chen Date: Mon, 25 Nov 2024 22:23:19 +0100 Subject: [PATCH] fix(backend): add LocalDate serialization module --- .../backend/utils/LocalDateJacksonModule.kt | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 backend/src/main/kotlin/org/loculus/backend/utils/LocalDateJacksonModule.kt diff --git a/backend/src/main/kotlin/org/loculus/backend/utils/LocalDateJacksonModule.kt b/backend/src/main/kotlin/org/loculus/backend/utils/LocalDateJacksonModule.kt new file mode 100644 index 000000000..be687bfc7 --- /dev/null +++ b/backend/src/main/kotlin/org/loculus/backend/utils/LocalDateJacksonModule.kt @@ -0,0 +1,29 @@ +package org.loculus.backend.utils + +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.JsonDeserializer +import com.fasterxml.jackson.databind.JsonSerializer +import com.fasterxml.jackson.databind.SerializerProvider +import com.fasterxml.jackson.databind.module.SimpleModule +import kotlinx.datetime.LocalDate +import org.springframework.context.annotation.Configuration + +@Configuration +class LocalDateJacksonModule : SimpleModule() { + init { + addSerializer(LocalDate::class.java, LocalDateSerializer()) + addDeserializer(LocalDate::class.java, LocalDateDeserializer()) + } +} + +class LocalDateSerializer : JsonSerializer() { + override fun serialize(value: LocalDate, gen: JsonGenerator, serializers: SerializerProvider) { + gen.writeString(value.toString()) + } +} + +class LocalDateDeserializer : JsonDeserializer() { + override fun deserialize(p: JsonParser, ctxt: DeserializationContext): LocalDate = LocalDate.parse(p.text) +}