Skip to content

Commit

Permalink
fix(backend): add LocalDate serialization module
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoran-chen committed Nov 25, 2024
1 parent 54d0347 commit 4dc8296
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<LocalDate>() {
override fun serialize(value: LocalDate, gen: JsonGenerator, serializers: SerializerProvider) {
gen.writeString(value.toString())
}
}

class LocalDateDeserializer : JsonDeserializer<LocalDate>() {
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): LocalDate = LocalDate.parse(p.text)
}

0 comments on commit 4dc8296

Please sign in to comment.