在反序列化过程中将空字符串忽略为 null

Ignore empty string as null during deserialization

我尝试将以下 json 反序列化为 java pojo。

[{
    "image" : {
        "url" : "http://foo.bar"
    }
}, {
    "image" : ""      <-- This is some funky null replacement
}, {
    "image" : null    <-- This is the expected null value (Never happens in that API for images though)
}]

我的 Java 类 看起来像这样:

public class Server {

    public Image image;
    // lots of other attributes

}

public class Image {

    public String url;
    // few other attributes

}

我使用 jackson 2.8.6

ObjectMapper.read(json, LIST_OF_SERVER_TYPE_REFERENCE);

但我不断收到以下异常:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of Image: no String-argument constructor/factory method to deserialize from String value ('')

如果我为它添加一个字符串 setter

public void setImage(Image image) {
    this.image = image;
}

public void setImage(String value) {
    // Ignore
}

我得到以下异常

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token

无论我是否(也)添加图片setter,异常都不会改变。

我也试过@JsonInclude(NOT_EMPTY)但这似乎只影响序列化。

总结:一些(设计糟糕的)API给我发送了一个空字符串("")而不是null,我必须告诉杰克逊忽略那个糟糕的价值。我该怎么做?

似乎没有开箱即用的解决方案,所以我选择了自定义解串器:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;

public class ImageDeserializer extends JsonDeserializer<Image> {

    @Override
    public Image deserialize(final JsonParser parser, final DeserializationContext context)
            throws IOException, JsonProcessingException {
        final JsonToken type = parser.currentToken();
        switch (type) {
            case VALUE_NULL:
                return null;
            case VALUE_STRING:
                return null; // TODO: Should check whether it is empty
            case START_OBJECT:
                return context.readValue(parser, Image.class);
            default:
                throw new IllegalArgumentException("Unsupported JsonToken type: " + type);
        }
    }

}

并使用下面的代码

@JsonDeserialize(using = ImageDeserializer.class)
@JsonProperty("image")
public Image image;