【问题标题】:Jackson not ignoring null objects in json array杰克逊没有忽略 json 数组中的空对象
【发布时间】:2016-05-10 01:19:42
【问题描述】:

我正在使用 Retrofit2 中的 JacksonConverterFactory 将我的 json 从服务器转换为对象。但是因为我取消了领域,所以 RealmList 对象不应该为空。我试图 .getSerializationConfig().withSerializationInclusion(JsonInclude.Include.NON_NULL);但看起来他仍在尝试向 RealmList 添加空值。

ObjectMapper mapper = new ObjectMapper();     mapper.getSerializationConfig().withSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
...
.addConverterFactory(JacksonConverterFactory.create(mapper)

json 看起来像这样

"items": [
    {
      "id": 8941,
      "size": 4
      "childs": [
        null,
        {
          "id": 32134,
          "count": 1
        },
        null,
        null
      ]
    },
    null,
    {
      "id": 8932,
      "size": 20
      "childs": [
        null,
        null,
        ...
      ]
    },
    {
      "id": 48715,
      "size": 20
    },
    null
  ]

日志猫:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: RealmList does not accept null values (through reference chain: java.util.ArrayList[11]->my.package.myobject["items"]->io.realm.RealmList[4])

【问题讨论】:

    标签: java android jackson retrofit realm


    【解决方案1】:

    在您提供的 sn-p 中使用了JsonInclude.Include.NON_EMPTY。 你真的试过JsonInclude.Include.NON_NULL 吗?

    【讨论】:

    • 我也尝试过gson,但对于gson他也在做同样的事情
    • 我正在使用 RealmList
    • 我也可以覆盖 RealmList 并删除异常
    猜你喜欢
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 2019-05-21
    • 1970-01-01
    • 2015-12-05
    • 2016-06-06
    相关资源
    最近更新 更多