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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15293697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:07:28  来源:igfitidea点击:

How to deserialize JSON null to a NullNode instead of Java null?

javaHymanson

提问by fge

Note: Hymanson 2.1.x.

注意:Hyman逊 2.1.x。

The problem is quite simple but I could not find a solution so far. I have browsed through existing documentation etc and could not find an answer.

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

The base class is this:

基类是这样的:

@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, Hymanson 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 + '"';
    }
}

And the problematic class is this:

有问题的课程是这样的:

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

When I try to deserialize:

当我尝试反序列化时:

{ "op": "add", "path": "/x", "value": null }

I'd like the null value to be deserialized as a NullNode, not Java null. And I couldn't find a way to do it so far.

我希望将 null 值反序列化为 a NullNode,而不是 Java null。到目前为止我找不到办法做到这一点。

How do you achieve that?

你如何做到这一点?

(note: all constructors of concrete classes are @JsonCreators with the appropriate @JsonPropertyannotations -- they work without problem, my only problem is JSON null handling)

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

回答by fge

OK, well, I haven't read the documentation enough, and it is in fact very simple.

好吧,我还没有阅读足够的文档,实际上很简单。

There is a JsonNodeDeserializerimplementation of JsonDeserializer, which you can extend. And JsonDeserializerhas a.getNullValue() method.

有一个JsonNodeDeserializer实现JsonDeserializer,您可以对其进行扩展。并且JsonDeserializer有一个.getNullValue() 方法

So, a custom deserializer was in order:

所以,一个自定义的解串器是有序的:

public final class JsonNullAwareDeserializer
    extends JsonNodeDeserializer
{
    @Override
    public JsonNode getNullValue()
    {
        return NullNode.getInstance();
    }
}

And, in the problematic class:

而且,在有问题的课程中:

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