How do I call the default deserializer from a custom deserializer in Jackson

前端 未结 11 1026
野趣味
野趣味 2020-11-22 14:26

I have a problem in my custom deserializer in Jackson. I want to access the default serializer to populate the object I am deserializing into. After the population I will do

11条回答
  •  囚心锁ツ
    2020-11-22 14:55

    There are couple of ways to do this, but to do it right involves bit more work. Basically you can not use sub-classing, since information default deserializers need is built from class definitions.

    So what you can most likely use is to construct a BeanDeserializerModifier, register that via Module interface (use SimpleModule). You need to define/override modifyDeserializer, and for the specific case where you want to add your own logic (where type matches), construct your own deserializer, pass the default deserializer you are given. And then in deserialize() method you can just delegate call, take the result Object.

    Alternatively, if you must actually create and populate the object, you can do so and call overloaded version of deserialize() that takes third argument; object to deserialize into.

    Another way that might work (but not 100% sure) would be to specify Converter object (@JsonDeserialize(converter=MyConverter.class)). This is a new Jackson 2.2 feature. In your case, Converter would not actually convert type, but simplify modify the object: but I don't know if that would let you do exactly what you want, since the default deserializer would be called first, and only then your Converter.

提交回复
热议问题