Skip to content

Commit

Permalink
[api]: Move Path serializer and deserializer to nebulosa-json module
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagohm committed Jul 19, 2024
1 parent e969fc9 commit 421bf99
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -77,8 +77,8 @@ class BeanConfiguration {
): SimpleModule = kotlinModule()
.apply { serializers.forEach { addSerializer(it) } }
.apply { deserializers.forEach { addDeserializer(it.handledType() as Class<Any>, it) } }
.addSerializer(PathSerializer)
.addDeserializer(Path::class.java, PathDeserializer)
.addSerializer(PathSerializer())
.addDeserializer(Path::class.java, PathDeserializer())

@Bean
fun jackson2ObjectMapperBuilderCustomizer() = Jackson2ObjectMapperBuilderCustomizer {
Expand Down Expand Up @@ -217,7 +217,6 @@ class BeanConfiguration {

private const val MAX_CACHE_SIZE = 1024L * 1024L * 32L // 32MB

@JvmStatic private val LOG = loggerFor<BeanConfiguration>()
@JvmStatic private val OKHTTP_LOG = loggerFor<OkHttpClient>()
}
}
1 change: 0 additions & 1 deletion nebulosa-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ plugins {
}

dependencies {
compileOnly(libs.bundles.jackson)
implementation(project(":nebulosa-log"))
testImplementation(project(":nebulosa-test"))
}
Expand Down
17 changes: 17 additions & 0 deletions nebulosa-json/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
plugins {
kotlin("jvm")
id("maven-publish")
}

dependencies {
api(libs.bundles.jackson)
testImplementation(project(":nebulosa-test"))
}

publishing {
publications {
create<MavenPublication>("pluginMaven") {
from(components["java"])
}
}
}
Original file line number Diff line number Diff line change
@@ -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>(Path::class.java) {

private fun readResolve(): Any = PathDeserializer
class PathDeserializer : StdDeserializer<Path>(Path::class.java) {

override fun deserialize(p: JsonParser, ctxt: DeserializationContext): Path? {
return p.valueAsString?.let(Path::of)
Expand Down
24 changes: 24 additions & 0 deletions nebulosa-json/src/main/kotlin/nebulosa/json/PathModule.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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>(Path::class.java) {

private fun readResolve(): Any = PathSerializer
class PathSerializer : StdSerializer<Path>(Path::class.java) {

override fun serialize(value: Path?, gen: JsonGenerator, provider: SerializerProvider) {
value?.also { gen.writeString("$it") } ?: gen.writeNull()
Expand Down
2 changes: 1 addition & 1 deletion nebulosa-phd2-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,7 +71,6 @@ class PHD2Client : NettyClient() {
@JvmStatic private val LOG = loggerFor<PHD2Client>()

@JvmStatic private val MODULE = kotlinModule().also {
it.addDeserializer(Path::class.java, PathDeserializer)
it.addDeserializer(GuideState::class.java, GuideStateDeserializer)
it.addSerializer(GuideStateSerializer)
}
Expand All @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion nebulosa-pixinsight/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,12 +55,8 @@ abstract class AbstractPixInsightScript<T : PixInsightScript.Output> : PixInsigh

@JvmStatic private val LOG = loggerFor<AbstractPixInsightScript<*>>()

@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)
}

Expand Down
2 changes: 1 addition & 1 deletion nebulosa-retrofit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 421bf99

Please sign in to comment.