Custom Jackson Deserializer Getting Access to Current Field Class

前端 未结 4 1011
遥遥无期
遥遥无期 2021-01-05 07:13

I\'m trying to write a custom deserializer for Jackson and I want to make it generic (generic in the sense of working on any type, not as in \"generics\").

However I

4条回答
  •  甜味超标
    2021-01-05 07:43

    I have solved my particular problem by adding an implementation of Deserializers to the ObjectMapper. Eg

       Deserializers d = new Deserializers.Base() {
    
       @Override
       public JsonDeserializer findEnumDeserializer(Class type, DeserializationConfig config, BeanDescription beanDesc, BeanProperty property)
                      throws JsonMappingException {
                    if (property.getType().getContentType() != null)
                        return new EnumDeserializer(property.getType().getContentType().getRawClass());
                    return new EnumDeserializer(property.getType().getRawClass());
                }
    
            };
            mapper.setDeserializerProvider(mapper.getDeserializerProvider().withAdditionalDeserializers(d));
    

    This will return my custom EnumDeserializer instantiated for each separate Enum type.

提交回复
热议问题