diff --git a/src/main/java/de/bixilon/minosoft/util/json/InstantSerializer.kt b/src/main/java/de/bixilon/minosoft/util/json/InstantSerializer.kt new file mode 100644 index 000000000..72348ddf4 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/util/json/InstantSerializer.kt @@ -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 . + * + * 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::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::class.java) { + + override fun serialize(value: Instant?, generator: JsonGenerator, provider: SerializerProvider?) { + generator.writeNumber(value?.epochSeconds ?: -1) + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/util/json/Jackson.kt b/src/main/java/de/bixilon/minosoft/util/json/Jackson.kt index e5792c6f7..d596a1f83 100644 --- a/src/main/java/de/bixilon/minosoft/util/json/Jackson.kt +++ b/src/main/java/de/bixilon/minosoft/util/json/Jackson.kt @@ -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