如何将JSON null反序列化为NullNode而不是Java null? [英] How to deserialize JSON null to a NullNode instead of Java null?

查看:114
本文介绍了如何将JSON null反序列化为NullNode而不是Java null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:Jackson 2.1.x。

Note: Jackson 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.

基类如下:

@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。到目前为止我找不到办法。

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.

你是如何实现这一目标的?

How do you achieve that?

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

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

推荐答案

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

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

有一个 JsonNodeDeserializer JsonDeserializer 的实现,你可以扩展它。并且 JsonDeserializer 有一个。getNullValue()方法

There is a JsonNodeDeserializer implementation of JsonDeserializer, which you can extend. And JsonDeserializer has a.getNullValue() method.

因此,自定义反序列化器按顺序排列:

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;

这篇关于如何将JSON null反序列化为NullNode而不是Java null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆