当前位置: 首页 > 知识库问答 >
问题:

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

钱和平
2023-03-14

我尝试将以下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)
}]
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 ('')

如果我为它添加一个字符串设置器

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

摘要:一些(设计错误的)API发送给我的是空字符串(“”)而不是null,我不得不告诉Jackson忽略这个错误的值。我怎么能这么做?

共有1个答案

鱼志学
2023-03-14

似乎没有现成的解决方案,所以我选择了自定义的反序列化程序:

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;
 类似资料:
  • 问题内容: 我有以下类,它是由Jackson映射的(简化版): 在某些情况下,服务器会返回,然后我想将name设置为空的Java String。 是否有任何Jackson注释,或者如果属性为,我应该只检查getter中的null并返回空字符串? 问题答案: 您可以在默认构造函数中或声明时进行设置: 要么

  • 我有以下由Jackson映射的类(简化版): 在某些情况下,服务器返回,然后我想将name设置为空Java字符串。 是否有任何Jackson注释,或者如果属性为,我应该只检查getter中的null并返回空字符串吗?

  • 有没有一种通用的方法告诉Gson不要写空字符串? 我非常不喜欢实现一个TypeAdapter,它可以像这里的答案所暗示的那样处理每个字段。

  • 我有getter方法 我尝试了几个链接,但这些都没有帮助, http://www.java2novice.com/java-json/jackson/ignore-json-null-elements/ 如何用JsonFormat反序列化Jackson Json空字符串到日期 如何告诉Jackson在序列化期间忽略一个字段,如果它的值为空? 错误:

  • 我的控制器接收到的JSON数据如下: 问题是对象“ContainedObject”被解释为是,并且它正在被实例化。因此,只要我的控制器读取这个JSON数据,它就会生成一个ContainedObject对象类型的实例,但我需要它为空。 最简单、最快的解决方案是,在接收的JSON数据中,该值为空,如下所示: 我的实体类如下: 和包含的对象类,如下所示:

  • 问题内容: 我有这个字符串: 我正在反序列化对象: 该对象看起来像: 并尝试创建字典: 但得到。 可能是什么问题? 问题答案: 请参阅mridula的答案,了解为什么您会得到null。但是,如果您想直接将json字符串转换为字典,则可以尝试以下代码段。