杰克逊没有忽略json数组中的空对象

时间:2016-02-01 14:31:48

标签: java android jackson retrofit realm

我使用Retrofit2中的JacksonConverterFactory将我的json从服务器转换为对象。但是因为im unsing领域,RealmList对象不应该为null。我试过.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
  ]

logcat:

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])

1 个答案:

答案 0 :(得分:2)

在您提供的代码段JsonInclude.Include.NON_EMPTY中使用。 你真的试过JsonInclude.Include.NON_NULL吗?