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 <gpe@openrs2.dev>
Graham 5 years ago
parent 2c4261e751
commit 9441527c61
  1. 3
      settings.gradle.kts
  2. 28
      yaml/build.gradle.kts
  3. 18
      yaml/src/main/java/dev/openrs2/yaml/ObjectMapperProvider.kt
  4. 17
      yaml/src/main/java/dev/openrs2/yaml/YamlModule.kt

@ -18,7 +18,8 @@ include(
"deob-map",
"deob-processor",
"game",
"util"
"util",
"yaml"
)
if (Files.exists(rootProject.projectDir.toPath().resolve("nonfree/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<MavenPublication>("maven") {
from(components["java"])
pom {
packaging = "jar"
name.set("OpenRS2 YAML")
description.set(
"""
Guava module for creating a YAML ObjectMapper.
""".trimIndent()
)
}
}
}

@ -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<ObjectMapper> {
override fun get(): ObjectMapper {
return ObjectMapper(YAMLFactory())
.registerKotlinModule()
.registerModules(modules)
}
}

@ -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)
}
}
Loading…
Cancel
Save