Description
Search before asking
- I searched in the issues and found nothing similar.
Describe the bug
The ObjectMapper
functions handle the case where the root node is the null node by specifically calling getNullValue
of the root deserializer. The equivalent DeserializationContext
functions do not. I would expect the two functions to behave similarly, or the differences should be documented.
See below for a reproducer and use case.
Version Information
2.18.1
Reproduction
My use case is that I have a custom deserializer that reads the defaultValue
metadata field that is populated by the JsonProperty
annotation. I implement a custom getAbsentValue
function that will handle the default values. However, I also want to allow "null"
as a default value.
override fun getAbsentValue(ctxt: DeserializationContext): Property<*> {
val defaultString = property?.metadata?.defaultValue // This is the string "null"
if(defaultString != null) {
val tree = ctxt.parser.codec.factory.createParser(defaultString).readValueAsTree<JsonNode>()
val thisWorks = (ctxt.parser.codec as ObjectMapper).treeToValue<Any>(tree, valueType)
val doesNotWork = ctxt.readTreeAsValue<Any>(tree, valueType)
// Further handling
}
The workaround is to cast the codec to an ObjectMapper
, but that does not seem very nice and I would prefer not to do it, since there are no guarantees of the cast being allowed.