jackson: serialize kotlin instant

This commit is contained in:
Moritz Zwerger 2025-05-25 21:44:19 +02:00
parent f18ffb8bee
commit 2a5849fc9d
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 53 additions and 2 deletions

View File

@ -0,0 +1,50 @@
/*
* Minosoft
* Copyright (C) 2020-2025 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
@file:OptIn(ExperimentalTime::class)
package de.bixilon.minosoft.util.json
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.JsonToken
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import kotlin.time.ExperimentalTime
import kotlin.time.Instant
object InstantSerializer : SimpleModule() {
init {
addDeserializer(Instant::class.java, Deserializer)
addSerializer(Instant::class.java, Serializer)
}
object Deserializer : StdDeserializer<Instant>(Instant::class.java) {
override fun deserialize(parser: JsonParser, context: DeserializationContext?) = when (parser.currentToken) {
JsonToken.VALUE_NUMBER_INT -> Instant.fromEpochSeconds(parser.valueAsLong, 0L)
else -> TODO("Can not parse instant!")
}
}
object Serializer : StdSerializer<Instant>(Instant::class.java) {
override fun serialize(value: Instant?, generator: JsonGenerator, provider: SerializerProvider?) {
generator.writeNumber(value?.epochSeconds ?: -1)
}
}
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2025 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -42,7 +42,7 @@ object Jackson {
.configure(KotlinFeature.NullToEmptyMap, false)
.configure(KotlinFeature.NullIsSameAsDefault, false)
.configure(KotlinFeature.SingletonSupport, false)
.configure(KotlinFeature.StrictNullChecks, false)
.configure(KotlinFeature.NewStrictNullChecks, false)
.build()
)
.registerModule(JavaTimeModule())
@ -55,6 +55,7 @@ object Jackson {
.registerModule(Vec2iSerializer)
.registerModule(Vec3Serializer)
.registerModule(Vec4Serializer)
.registerModule(InstantSerializer)
.setDefaultMergeable(true)
.apply {
propertyNamingStrategy = PropertyNamingStrategies.SNAKE_CASE