From 9441527c6115fe6eacc3339c5213d11fa30fcd8f Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 26 Apr 2020 20:38:45 +0100 Subject: [PATCH] Add yaml module It uses multibindings to allow additional Jackson modules to be registered across different Guice modules while using a single ObjectMapper for the whole application. Signed-off-by: Graham --- settings.gradle.kts | 3 +- yaml/build.gradle.kts | 28 +++++++++++++++++++ .../dev/openrs2/yaml/ObjectMapperProvider.kt | 18 ++++++++++++ .../main/java/dev/openrs2/yaml/YamlModule.kt | 17 +++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 yaml/build.gradle.kts create mode 100644 yaml/src/main/java/dev/openrs2/yaml/ObjectMapperProvider.kt create mode 100644 yaml/src/main/java/dev/openrs2/yaml/YamlModule.kt diff --git a/settings.gradle.kts b/settings.gradle.kts index 9fbb441134..1d70b24d48 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -18,7 +18,8 @@ include( "deob-map", "deob-processor", "game", - "util" + "util", + "yaml" ) if (Files.exists(rootProject.projectDir.toPath().resolve("nonfree/build.gradle.kts"))) { diff --git a/yaml/build.gradle.kts b/yaml/build.gradle.kts new file mode 100644 index 0000000000..2e5b4b1a00 --- /dev/null +++ b/yaml/build.gradle.kts @@ -0,0 +1,28 @@ +plugins { + `maven-publish` + kotlin("jvm") +} + +dependencies { + api("com.fasterxml.jackson.core:jackson-databind:${Versions.jackson}") + api("com.google.inject:guice:${Versions.guice}") + + implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${Versions.jackson}") + implementation("com.fasterxml.jackson.module:jackson-module-kotlin:${Versions.jackson}") +} + +publishing { + publications.create("maven") { + from(components["java"]) + + pom { + packaging = "jar" + name.set("OpenRS2 YAML") + description.set( + """ + Guava module for creating a YAML ObjectMapper. + """.trimIndent() + ) + } + } +} diff --git a/yaml/src/main/java/dev/openrs2/yaml/ObjectMapperProvider.kt b/yaml/src/main/java/dev/openrs2/yaml/ObjectMapperProvider.kt new file mode 100644 index 0000000000..519459f7f6 --- /dev/null +++ b/yaml/src/main/java/dev/openrs2/yaml/ObjectMapperProvider.kt @@ -0,0 +1,18 @@ +package dev.openrs2.yaml + +import com.fasterxml.jackson.databind.Module +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory +import com.fasterxml.jackson.module.kotlin.registerKotlinModule +import javax.inject.Inject +import javax.inject.Provider + +class ObjectMapperProvider @Inject constructor( + private val modules: Set<@JvmSuppressWildcards Module> +) : Provider { + override fun get(): ObjectMapper { + return ObjectMapper(YAMLFactory()) + .registerKotlinModule() + .registerModules(modules) + } +} diff --git a/yaml/src/main/java/dev/openrs2/yaml/YamlModule.kt b/yaml/src/main/java/dev/openrs2/yaml/YamlModule.kt new file mode 100644 index 0000000000..2623ad3c7f --- /dev/null +++ b/yaml/src/main/java/dev/openrs2/yaml/YamlModule.kt @@ -0,0 +1,17 @@ +package dev.openrs2.yaml + +import com.fasterxml.jackson.databind.Module +import com.fasterxml.jackson.databind.ObjectMapper +import com.google.inject.AbstractModule +import com.google.inject.Scopes +import com.google.inject.multibindings.Multibinder + +class YamlModule : AbstractModule() { + override fun configure() { + Multibinder.newSetBinder(binder(), Module::class.java) + + bind(ObjectMapper::class.java) + .toProvider(ObjectMapperProvider::class.java) + .`in`(Scopes.SINGLETON) + } +}