com.fasterxml.jackson.module.kotlin.KotlinObjectSingletonDeserializer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jackson-module-kotlin Show documentation
Show all versions of jackson-module-kotlin Show documentation
Add-on module for Jackson (https://github.com/FasterXML/jackson/) to support
Kotlin language, specifically introspection of method/constructor parameter names,
without having to add explicit property name annotation.
The newest version!
package com.fasterxml.jackson.module.kotlin
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.BeanProperty
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.deser.ContextualDeserializer
import com.fasterxml.jackson.databind.deser.ResolvableDeserializer
internal fun JsonDeserializer<*>.asSingletonDeserializer(singleton: Any) =
KotlinObjectSingletonDeserializer(singleton, this)
/** deserialize as normal, but return the canonical singleton instance. */
internal class KotlinObjectSingletonDeserializer(
private val singletonInstance: Any,
private val defaultDeserializer: JsonDeserializer<*>
) : JsonDeserializer(),
// Additional interfaces of a specific 'JsonDeserializer' must be supported
// Kotlin objectInstances are currently handled by a BeanSerializer which
// implements 'ContextualDeserializer' and 'ResolvableDeserializer'.
ContextualDeserializer,
ResolvableDeserializer {
override fun resolve(ctxt: DeserializationContext?) {
if (defaultDeserializer is ResolvableDeserializer) {
defaultDeserializer.resolve(ctxt)
}
}
override fun createContextual(ctxt: DeserializationContext?, property: BeanProperty?): JsonDeserializer<*> =
if (defaultDeserializer is ContextualDeserializer) {
defaultDeserializer.createContextual(ctxt, property)
.asSingletonDeserializer(singletonInstance)
} else {
this
}
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): Any {
defaultDeserializer.deserialize(p, ctxt)
return singletonInstance
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy