-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(backend): add LocalDate serialization module
- Loading branch information
1 parent
54d0347
commit 4dc8296
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
backend/src/main/kotlin/org/loculus/backend/utils/LocalDateJacksonModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |