From 421bf99896ce5c2b42c1331c55abf427837cf9ad Mon Sep 17 00:00:00 2001 From: tiagohm Date: Thu, 18 Jul 2024 23:47:09 -0300 Subject: [PATCH] [api]: Move Path serializer and deserializer to nebulosa-json module --- .../beans/configurations/BeanConfiguration.kt | 9 ++++--- nebulosa-common/build.gradle.kts | 1 - nebulosa-json/build.gradle.kts | 17 +++++++++++++ .../kotlin/nebulosa}/json/PathDeserializer.kt | 6 ++--- .../main/kotlin/nebulosa/json/PathModule.kt | 24 +++++++++++++++++++ .../kotlin/nebulosa}/json/PathSerializer.kt | 6 ++--- nebulosa-phd2-client/build.gradle.kts | 2 +- .../kotlin/nebulosa/phd2/client/PHD2Client.kt | 5 ++-- nebulosa-pixinsight/build.gradle.kts | 2 +- .../script/AbstractPixInsightScript.kt | 10 ++------ nebulosa-retrofit/build.gradle.kts | 2 +- .../nebulosa/retrofit/RetrofitService.kt | 2 ++ settings.gradle.kts | 1 + 13 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 nebulosa-json/build.gradle.kts rename {nebulosa-common/src/main/kotlin/nebulosa/common => nebulosa-json/src/main/kotlin/nebulosa}/json/PathDeserializer.kt (68%) create mode 100644 nebulosa-json/src/main/kotlin/nebulosa/json/PathModule.kt rename {nebulosa-common/src/main/kotlin/nebulosa/common => nebulosa-json/src/main/kotlin/nebulosa}/json/PathSerializer.kt (70%) diff --git a/api/src/main/kotlin/nebulosa/api/beans/configurations/BeanConfiguration.kt b/api/src/main/kotlin/nebulosa/api/beans/configurations/BeanConfiguration.kt index 06571b436..698ad108a 100644 --- a/api/src/main/kotlin/nebulosa/api/beans/configurations/BeanConfiguration.kt +++ b/api/src/main/kotlin/nebulosa/api/beans/configurations/BeanConfiguration.kt @@ -13,12 +13,12 @@ import nebulosa.api.calibration.CalibrationFrameEntity import nebulosa.api.database.MyObjectBox import nebulosa.api.preference.PreferenceEntity import nebulosa.common.concurrency.DaemonThreadFactory -import nebulosa.common.json.PathDeserializer -import nebulosa.common.json.PathSerializer import nebulosa.guiding.Guider import nebulosa.guiding.phd2.PHD2Guider import nebulosa.hips2fits.Hips2FitsService import nebulosa.horizons.HorizonsService +import nebulosa.json.PathDeserializer +import nebulosa.json.PathSerializer import nebulosa.log.loggerFor import nebulosa.phd2.client.PHD2Client import nebulosa.sbd.SmallBodyDatabaseService @@ -77,8 +77,8 @@ class BeanConfiguration { ): SimpleModule = kotlinModule() .apply { serializers.forEach { addSerializer(it) } } .apply { deserializers.forEach { addDeserializer(it.handledType() as Class, it) } } - .addSerializer(PathSerializer) - .addDeserializer(Path::class.java, PathDeserializer) + .addSerializer(PathSerializer()) + .addDeserializer(Path::class.java, PathDeserializer()) @Bean fun jackson2ObjectMapperBuilderCustomizer() = Jackson2ObjectMapperBuilderCustomizer { @@ -217,7 +217,6 @@ class BeanConfiguration { private const val MAX_CACHE_SIZE = 1024L * 1024L * 32L // 32MB - @JvmStatic private val LOG = loggerFor() @JvmStatic private val OKHTTP_LOG = loggerFor() } } diff --git a/nebulosa-common/build.gradle.kts b/nebulosa-common/build.gradle.kts index 0c9e3b2a9..24ca9524d 100644 --- a/nebulosa-common/build.gradle.kts +++ b/nebulosa-common/build.gradle.kts @@ -4,7 +4,6 @@ plugins { } dependencies { - compileOnly(libs.bundles.jackson) implementation(project(":nebulosa-log")) testImplementation(project(":nebulosa-test")) } diff --git a/nebulosa-json/build.gradle.kts b/nebulosa-json/build.gradle.kts new file mode 100644 index 000000000..7ab28945d --- /dev/null +++ b/nebulosa-json/build.gradle.kts @@ -0,0 +1,17 @@ +plugins { + kotlin("jvm") + id("maven-publish") +} + +dependencies { + api(libs.bundles.jackson) + testImplementation(project(":nebulosa-test")) +} + +publishing { + publications { + create("pluginMaven") { + from(components["java"]) + } + } +} diff --git a/nebulosa-common/src/main/kotlin/nebulosa/common/json/PathDeserializer.kt b/nebulosa-json/src/main/kotlin/nebulosa/json/PathDeserializer.kt similarity index 68% rename from nebulosa-common/src/main/kotlin/nebulosa/common/json/PathDeserializer.kt rename to nebulosa-json/src/main/kotlin/nebulosa/json/PathDeserializer.kt index 2d0fb4fda..77640facf 100644 --- a/nebulosa-common/src/main/kotlin/nebulosa/common/json/PathDeserializer.kt +++ b/nebulosa-json/src/main/kotlin/nebulosa/json/PathDeserializer.kt @@ -1,13 +1,11 @@ -package nebulosa.common.json +package nebulosa.json import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.databind.DeserializationContext import com.fasterxml.jackson.databind.deser.std.StdDeserializer import java.nio.file.Path -data object PathDeserializer : StdDeserializer(Path::class.java) { - - private fun readResolve(): Any = PathDeserializer +class PathDeserializer : StdDeserializer(Path::class.java) { override fun deserialize(p: JsonParser, ctxt: DeserializationContext): Path? { return p.valueAsString?.let(Path::of) diff --git a/nebulosa-json/src/main/kotlin/nebulosa/json/PathModule.kt b/nebulosa-json/src/main/kotlin/nebulosa/json/PathModule.kt new file mode 100644 index 000000000..23e4a2d13 --- /dev/null +++ b/nebulosa-json/src/main/kotlin/nebulosa/json/PathModule.kt @@ -0,0 +1,24 @@ +package nebulosa.json + +import com.fasterxml.jackson.databind.module.SimpleDeserializers +import com.fasterxml.jackson.databind.module.SimpleModule +import com.fasterxml.jackson.databind.module.SimpleSerializers +import com.fasterxml.jackson.datatype.jsr310.PackageVersion +import java.nio.file.Path + +class PathModule : SimpleModule(PackageVersion.VERSION) { + + override fun setupModule(context: SetupContext) { + super.setupModule(context) + + with(SimpleDeserializers()) { + addDeserializer(Path::class.java, PathDeserializer()) + context.addDeserializers(this) + } + + with(SimpleSerializers()) { + addSerializer(PathSerializer()) + context.addSerializers(this) + } + } +} diff --git a/nebulosa-common/src/main/kotlin/nebulosa/common/json/PathSerializer.kt b/nebulosa-json/src/main/kotlin/nebulosa/json/PathSerializer.kt similarity index 70% rename from nebulosa-common/src/main/kotlin/nebulosa/common/json/PathSerializer.kt rename to nebulosa-json/src/main/kotlin/nebulosa/json/PathSerializer.kt index 199eb96b2..36c08fbbb 100644 --- a/nebulosa-common/src/main/kotlin/nebulosa/common/json/PathSerializer.kt +++ b/nebulosa-json/src/main/kotlin/nebulosa/json/PathSerializer.kt @@ -1,13 +1,11 @@ -package nebulosa.common.json +package nebulosa.json import com.fasterxml.jackson.core.JsonGenerator import com.fasterxml.jackson.databind.SerializerProvider import com.fasterxml.jackson.databind.ser.std.StdSerializer import java.nio.file.Path -data object PathSerializer : StdSerializer(Path::class.java) { - - private fun readResolve(): Any = PathSerializer +class PathSerializer : StdSerializer(Path::class.java) { override fun serialize(value: Path?, gen: JsonGenerator, provider: SerializerProvider) { value?.also { gen.writeString("$it") } ?: gen.writeNull() diff --git a/nebulosa-phd2-client/build.gradle.kts b/nebulosa-phd2-client/build.gradle.kts index 6e7c20b3e..41aee57dc 100644 --- a/nebulosa-phd2-client/build.gradle.kts +++ b/nebulosa-phd2-client/build.gradle.kts @@ -7,7 +7,7 @@ dependencies { api(project(":nebulosa-common")) api(project(":nebulosa-netty")) api(project(":nebulosa-guiding")) - api(libs.bundles.jackson) + api(project(":nebulosa-json")) implementation(project(":nebulosa-log")) testImplementation(project(":nebulosa-test")) } diff --git a/nebulosa-phd2-client/src/main/kotlin/nebulosa/phd2/client/PHD2Client.kt b/nebulosa-phd2-client/src/main/kotlin/nebulosa/phd2/client/PHD2Client.kt index 9d3789923..e185fe988 100644 --- a/nebulosa-phd2-client/src/main/kotlin/nebulosa/phd2/client/PHD2Client.kt +++ b/nebulosa-phd2-client/src/main/kotlin/nebulosa/phd2/client/PHD2Client.kt @@ -7,15 +7,14 @@ import com.fasterxml.jackson.module.kotlin.jsonMapper import com.fasterxml.jackson.module.kotlin.kotlinModule import io.netty.channel.ChannelInitializer import io.netty.channel.socket.SocketChannel -import nebulosa.common.json.PathDeserializer import nebulosa.guiding.GuideState +import nebulosa.json.PathModule import nebulosa.log.loggerFor import nebulosa.netty.NettyClient import nebulosa.phd2.client.commands.CompletableCommand import nebulosa.phd2.client.commands.PHD2Command import nebulosa.phd2.client.events.GuideStateDeserializer import nebulosa.phd2.client.events.GuideStateSerializer -import java.nio.file.Path import java.util.* import java.util.concurrent.CompletableFuture import java.util.concurrent.TimeUnit @@ -72,7 +71,6 @@ class PHD2Client : NettyClient() { @JvmStatic private val LOG = loggerFor() @JvmStatic private val MODULE = kotlinModule().also { - it.addDeserializer(Path::class.java, PathDeserializer) it.addDeserializer(GuideState::class.java, GuideStateDeserializer) it.addSerializer(GuideStateSerializer) } @@ -81,6 +79,7 @@ class PHD2Client : NettyClient() { disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS) serializationInclusion(JsonInclude.Include.NON_NULL) + addModule(PathModule()) addModule(MODULE) } } diff --git a/nebulosa-pixinsight/build.gradle.kts b/nebulosa-pixinsight/build.gradle.kts index f22e2d728..60cae2725 100644 --- a/nebulosa-pixinsight/build.gradle.kts +++ b/nebulosa-pixinsight/build.gradle.kts @@ -10,7 +10,7 @@ dependencies { api(project(":nebulosa-stardetector")) api(project(":nebulosa-stacker")) api(project(":nebulosa-livestacker")) - api(libs.bundles.jackson) + api(project(":nebulosa-json")) api(libs.apache.codec) implementation(project(":nebulosa-log")) testImplementation(project(":nebulosa-image")) diff --git a/nebulosa-pixinsight/src/main/kotlin/nebulosa/pixinsight/script/AbstractPixInsightScript.kt b/nebulosa-pixinsight/src/main/kotlin/nebulosa/pixinsight/script/AbstractPixInsightScript.kt index a0de3c09d..b49b79349 100644 --- a/nebulosa-pixinsight/src/main/kotlin/nebulosa/pixinsight/script/AbstractPixInsightScript.kt +++ b/nebulosa-pixinsight/src/main/kotlin/nebulosa/pixinsight/script/AbstractPixInsightScript.kt @@ -2,11 +2,9 @@ package nebulosa.pixinsight.script import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.module.kotlin.jsonMapper -import com.fasterxml.jackson.module.kotlin.kotlinModule import nebulosa.common.exec.CommandLine import nebulosa.common.exec.CommandLineListener -import nebulosa.common.json.PathDeserializer -import nebulosa.common.json.PathSerializer +import nebulosa.json.PathModule import nebulosa.log.loggerFor import org.apache.commons.codec.binary.Hex import java.nio.file.Path @@ -57,12 +55,8 @@ abstract class AbstractPixInsightScript : PixInsigh @JvmStatic private val LOG = loggerFor>() - @JvmStatic private val KOTLIN_MODULE = kotlinModule() - .addDeserializer(Path::class.java, PathDeserializer) - .addSerializer(PathSerializer) - @JvmStatic internal val OBJECT_MAPPER = jsonMapper { - addModule(KOTLIN_MODULE) + addModule(PathModule()) disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) } diff --git a/nebulosa-retrofit/build.gradle.kts b/nebulosa-retrofit/build.gradle.kts index b17fb04ba..d68a28ef0 100644 --- a/nebulosa-retrofit/build.gradle.kts +++ b/nebulosa-retrofit/build.gradle.kts @@ -4,11 +4,11 @@ plugins { } dependencies { + api(project(":nebulosa-json")) api(libs.retrofit) api(libs.retrofit.jackson) api(libs.okhttp) api(libs.okhttp.logging) - api(libs.bundles.jackson) compileOnly(libs.csv) implementation(project(":nebulosa-log")) testImplementation(project(":nebulosa-test")) diff --git a/nebulosa-retrofit/src/main/kotlin/nebulosa/retrofit/RetrofitService.kt b/nebulosa-retrofit/src/main/kotlin/nebulosa/retrofit/RetrofitService.kt index c84f606c1..4d0565dc3 100644 --- a/nebulosa-retrofit/src/main/kotlin/nebulosa/retrofit/RetrofitService.kt +++ b/nebulosa-retrofit/src/main/kotlin/nebulosa/retrofit/RetrofitService.kt @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.MapperFeature import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule import com.fasterxml.jackson.module.kotlin.jsonMapper +import nebulosa.json.PathModule import okhttp3.ConnectionPool import okhttp3.OkHttpClient import retrofit2.CallAdapter @@ -64,6 +65,7 @@ abstract class RetrofitService( .build() @JvmStatic private val DEFAULT_MAPPER = jsonMapper { + addModule(PathModule()) addModule(JavaTimeModule()) enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS) disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) diff --git a/settings.gradle.kts b/settings.gradle.kts index 6ff001453..93e0f196c 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -69,6 +69,7 @@ include(":nebulosa-indi-client") include(":nebulosa-indi-device") include(":nebulosa-indi-protocol") include(":nebulosa-io") +include(":nebulosa-json") include(":nebulosa-livestacker") include(":nebulosa-log") include(":nebulosa-lx200-protocol")