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

法案

我想通过扩展默认值使其在其后设置更多值来制作自己的解串器:

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。

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

谢谢社区!

法案

正如沙龙所说(基于我如何从杰克逊的自定义解串器中调用默认解串器

@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);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章