如何在自定义解串器中使用 jackson ObjectMapper?

2022-09-01 21:19:59

我尝试编写自定义 jackson 反序列化程序。我想“查看”一个字段并执行自动反序列化到类,请参阅下面的示例:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.mypackage.MyInterface;
import com.mypackage.MyFailure;
import com.mypackage.MySuccess;

import java.io.IOException;

public class MyDeserializer extends JsonDeserializer<MyInterface> {

    @Override
    public MyInterface deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        ObjectCodec codec = jp.getCodec();
        JsonNode node = codec.readTree(jp);
        if (node.has("custom_field")) {
            return codec.treeToValue(node, MyFailure.class);
        } else {
            return codec.treeToValue(node, MySuccess.class);
        }
    }
}

Pojos:

public class MyFailure implements MyInterface {}

public class MySuccess implements MyInterface {}

@JsonDeserialize(using = MyDeserializer.class)
public interface MyInterface {}

我得到了.在理解中调用相同的反序列化程序。有没有办法使用或内部定制解吸器?StackOverflowErrorcodec.treeToValuecodec.treeToValueObjectMapper.readValue(String,Class<T>)


答案 1

眼前的问题似乎是,正在为 MyInterface 的实现以及 MyInterface 本身选择 :因此是无限循环。@JsonDeserialize(using=...)

您可以修复此问题,我覆盖了每个实现中的设置:

@JsonDeserialize(using=JsonDeserializer.None.class)
public static class MySuccess implements MyInterface {
}

或者使用模块而不是注释来配置反序列化(并从 MyInterface 中删除注释):

mapper.registerModule(new SimpleModule() {{
    addDeserializer(MyInterface.class, new MyDeserializer());
}});

另外,您还可以考虑扩展以实现基于 JsonNode 的反序列化。例如:StdNodeBasedDeserializer

@Override
public MyInterface convert(JsonNode root, DeserializationContext ctxt) throws IOException {
    java.lang.reflect.Type targetType;
    if (root.has("custom_field")) {
        targetType = MyFailure.class;
    } else {
        targetType = MySuccess.class;
    }
    JavaType jacksonType = ctxt.getTypeFactory().constructType(targetType);
    JsonDeserializer<?> deserializer = ctxt.findRootValueDeserializer(jacksonType);
    JsonParser nodeParser = root.traverse(ctxt.getParser().getCodec());
    nodeParser.nextToken();
    return (MyInterface) deserializer.deserialize(nodeParser, ctxt);
}

对此自定义反序列化程序有一系列改进,特别是关于跟踪反序列化的上下文等,但这应该提供您所要求的功能。


答案 2

为了在自定义反序列化程序中使用您自己的反序列化程序,可以使用 Jackson Mix-in Annotations接口)从类中动态删除自定义反序列化程序,从而避免由于 .ObjectMapperDefaultJsonDeserializerPOJOStackOverflowErrorobjectMapper.readValue(JsonParser, Class<T>)

public class MyDeserializer extends JsonDeserializer<MyInterface> {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    static {
        objectMapper.addMixIn(MySuccess.class, DefaultJsonDeserializer.class);
        objectMapper.addMixIn(MyFailure.class, DefaultJsonDeserializer.class);
    }

    @Override
    public MyInterface deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        if (jp.getCodec().<JsonNode>readTree(jp).has("custom_field")) {
            return objectMapper.readValue(jp, MyFailure.class);
        } else {
            return objectMapper.readValue(jp, MySuccess.class);
        }           
    }

    @JsonDeserialize
    private interface DefaultJsonDeserializer {
        // Reset default json deserializer
    }

}