使用Jackson自定义反序列化:扩展默认反序列化器

时间:2018-02-05 09:36:51

标签: java spring spring-boot jackson json-deserialization

我想通过扩展默认值来设置我自己的反序列化器,然后设置更多的值:

simplified code:
public class Dto {
    public String originalJsonString;
}

public MyFooDto extends Dto {
    public String myField;
}



@Bean
public ObjectMapper deserializingObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addDeserializer(MyFooDto.class, new JsonDtoDeserializer<>());
    objectMapper.registerModule(javaTimeModule);
    return objectMapper;
}
// or maybe instead of the Beam just @JsonDeserialize(using = JsonDtoDeserializer.class) before MyFooDto?


public class JsonDtoDeserializer<T extends Dto> extends StdDeserializer<T> {
// or maybe extends JsonDeserializer? or UntypedObjectDeserializer? or UntypedObjectDeserializer.Vanilla?

    public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        // here I would like:
        T item = super.deserialize"AsUsual"(p, ctxt);
        // the difficulty is to avoid the loop of death, where the deserializer would call itself for the eternity...

        // And then set other Dto-fields depending on the original Json input, for example:
        item.originalJsonString = p.readValueAsTree().toString();
        return item;
    }
}

正如您所看到的,我还想将此Dto母语重用于其他DTO。

  • 我没有找到任何例子。我真的是世界上第一个?
  • 什么应该反序列化“AsUsual”(p,ctxt)?
  • 我应该使用什么样的母班? JsonDeserializer / StdDeserializer / UntypedObjectDeserializer?
  • 反序列化器是否知道它必须实例化哪个T类?

谢谢社区!

1 个答案:

答案 0 :(得分:3)

正如Sharon所说(基于How do I call the default deserializer from a custom deserializer in Jackson

@Bean
public ObjectMapper serializingObjectMapper() {

    ObjectMapper objectMapper = new ObjectMapper();
    SimpleModule simpleModule = new SimpleModule();

    simpleModule.setDeserializerModifier(new BeanDeserializerModifier() {
        @Override
        public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
            if (Dto.class.isAssignableFrom(beanDesc.getBeanClass())) {
                return new JsonDtoDeserializer<>(deserializer, beanDesc.getBeanClass());
            }
            return deserializer;
        }
    });

    objectMapper.registerModule(simpleModule);
    return objectMapper;
}



public class JsonDtoDeserializer<T extends Dto> extends StdDeserializer<T> implements ResolvableDeserializer /*StdDeserializer<Dto<T>>*/ /*UntypedObjectDeserializer.Vanilla*/ /*<T>*/ /*implements ResolvableDeserializer*/ {

    private final JsonDeserializer<?> defaultDeserializer;

    public JsonDtoDeserializer(JsonDeserializer<?> defaultDeserializer, Class<?> clazz) {
        super(clazz);
        this.defaultDeserializer = defaultDeserializer;
    }

    @Override
    public T deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException {

        @SuppressWarnings("unchecked")
        T itemObj = (T) defaultDeserializer.deserialize(p, ctxt);
        return itemObj;
    }

    // for some reason you have to implement ResolvableDeserializer when modifying BeanDeserializer
    // otherwise deserializing throws JsonMappingException??
    @Override public void resolve(DeserializationContext ctxt) throws JsonMappingException
    {
        ((ResolvableDeserializer) defaultDeserializer).resolve(ctxt);
    }
}