在通用属性解串器中包装所有标准和自定义Jackson解串器

我正在使用杰克逊2.8并期待包装所有标准/自定义反序列化器在一个标准的包装反序列化,以便对每个属性的每个反序列化的值实例做一些后处理。 为了做到这一点,我分类了BeanDeserializerModifier ,以便将基础JsonDeserializer和给定属性类型的相应JavaType传递给JsonDeserializer的子类, StdJsonDeserializer然后调用该基本反序列化器并对结果值实例执行各种操作。

在反序列化过程中,我得到以下错误:

 com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.lindisfarne.MundaneDataClass: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?) 

如果没有BeanDeserializerModifier ,那么这个操作当然会工作得很好 – 这显然我得到了一个不正确或不完整的反序列化器。 我怎样才能得到正确的解串器给定的类型?

完整的Kotlin代码重现以下问题:

 package com.lindisfarne import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.databind.* import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier import com.fasterxml.jackson.databind.deser.std.StdDeserializer import com.fasterxml.jackson.module.kotlin.KotlinModule import com.fasterxml.jackson.module.kotlin.readValue class ReferencingBeanModifier() : BeanDeserializerModifier() { override fun modifyDeserializer(config: DeserializationConfig?, beanDesc: BeanDescription?, deserializer: JsonDeserializer<*>?): JsonDeserializer<*> { val javaType = beanDesc?.type return WrappingDeserializer(deserializer = deserializer, javaType = javaType) } } class WrappingDeserializer(val deserializer :JsonDeserializer<*>?, val javaType: JavaType?) : StdDeserializer<Any>(javaType) { override fun deserialize(jsonParser: JsonParser?, deserializationContext: DeserializationContext?): Any? { val instance = deserializer?.deserialize(jsonParser,deserializationContext) println(instance) //do more stuff return instance } } data class MundaneDataClass( val trivialBoolean: Boolean = true, val borningInt:Int = 1, val meaninglessString:String = "yes" ) fun massiveFailure() : Unit { val mapper = ObjectMapper() with (mapper) { registerModules( KotlinModule().setDeserializerModifier(ReferencingBeanModifier()) ) disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) } val mdc = MundaneDataClass() val dataJson = mapper.writeValueAsString(mdc) val mdc2 = mapper.readValue<MundaneDataClass>(dataJson) } 

    Kotlin language will be the best programming language for Android.