forked from openrs2/openrs2
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>
parent
2c4261e751
commit
9441527c61
@ -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…
Reference in new issue