如何将JSON null反序列化为NullNode而不是Java null?

如何将JSON null反序列化为NullNode而不是Java null?,java,jackson,Java,Jackson,注:Jackson 2.1.x 问题很简单,但到目前为止我还没有找到解决办法。我浏览了现有的文档等,没有找到答案 基类是这样的: @JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "op") @JsonSubTypes({ @Type(name = "add", value = AddOperation.class), @Type(name = "copy", value = CopyOperation

注:Jackson 2.1.x

问题很简单,但到目前为止我还没有找到解决办法。我浏览了现有的文档等,没有找到答案

基类是这样的:

@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "op")

@JsonSubTypes({
    @Type(name = "add", value = AddOperation.class),
    @Type(name = "copy", value = CopyOperation.class),
    @Type(name = "move", value = MoveOperation.class),
    @Type(name = "remove", value = RemoveOperation.class),
    @Type(name = "replace", value = ReplaceOperation.class),
    @Type(name = "test", value = TestOperation.class)
})

public abstract class JsonPatchOperation
{
    /*
     * Note: no need for a custom deserializer, Jackson will try and find a
     * constructor with a single string argument and use it
     */
    protected final JsonPointer path;

    protected JsonPatchOperation(final JsonPointer path)
    {
        this.path = path;
    }

    public abstract JsonNode apply(final JsonNode node)
        throws JsonPatchException;

    @Override
    public String toString()
    {
        return "path = \"" + path + '"';
    }
}
public abstract class PathValueOperation
    extends JsonPatchOperation
{
    protected final JsonNode value;

    protected PathValueOperation(final JsonPointer path, final JsonNode value)
    {
        super(path);
        this.value = value.deepCopy();
    }

    @Override
    public String toString()
    {
        return super.toString() + ", value = " + value;
    }
}
问题在于:

@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "op")

@JsonSubTypes({
    @Type(name = "add", value = AddOperation.class),
    @Type(name = "copy", value = CopyOperation.class),
    @Type(name = "move", value = MoveOperation.class),
    @Type(name = "remove", value = RemoveOperation.class),
    @Type(name = "replace", value = ReplaceOperation.class),
    @Type(name = "test", value = TestOperation.class)
})

public abstract class JsonPatchOperation
{
    /*
     * Note: no need for a custom deserializer, Jackson will try and find a
     * constructor with a single string argument and use it
     */
    protected final JsonPointer path;

    protected JsonPatchOperation(final JsonPointer path)
    {
        this.path = path;
    }

    public abstract JsonNode apply(final JsonNode node)
        throws JsonPatchException;

    @Override
    public String toString()
    {
        return "path = \"" + path + '"';
    }
}
public abstract class PathValueOperation
    extends JsonPatchOperation
{
    protected final JsonNode value;

    protected PathValueOperation(final JsonPointer path, final JsonNode value)
    {
        super(path);
        this.value = value.deepCopy();
    }

    @Override
    public String toString()
    {
        return super.toString() + ", value = " + value;
    }
}
当我尝试反序列化时:

{ "op": "add", "path": "/x", "value": null }
我希望将null值反序列化为
NullNode
,而不是Java null。到目前为止,我还没有找到一个方法

你是如何做到的


(注意:所有具体类的构造函数都是
@JsonCreator
s,带有适当的
@JsonProperty
注释——它们工作没有问题,我唯一的问题是JSON null处理)

好的,我没有充分阅读文档,事实上它非常简单

有一个
JsonDeserializer
的实现,您可以对其进行扩展。而
JsonDeserializer
有一个

因此,定制反序列化程序已准备就绪:

public final class JsonNullAwareDeserializer
    extends JsonNodeDeserializer
{
    @Override
    public JsonNode getNullValue()
    {
        return NullNode.getInstance();
    }
}
在有问题的课堂上:

@JsonDeserialize(using = JsonNullAwareDeserializer.class)
protected final JsonNode value;

不管它值多少钱,这可能是一个bug,现在在