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

前端 未结 1 1245
萌比男神i
萌比男神i 2021-01-14 06:26

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 answ

1条回答
  •  春和景丽
    2021-01-14 06:53

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

    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;
    

    0 讨论(0)
提交回复
热议问题