如何不在杰克逊发送空集合 [英] How to not send an empty collection in jackson

查看:128
本文介绍了如何不在杰克逊发送空集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个被强制序列化的对象:

I have an object that is curently being serialized to:

{
  "label" : "label",
  "proxyIds" : [ ],
  "childIds" : [ 161, 204, 206, 303, 311 ],
  "actionIds" : [ 157, 202 ],
}

该proxyIds是java对象中的空(非空)集合。

That proxyIds is an empty (not null) collection in the java object.

如何配置Jackson根本不在json中包含该对象?

How do I configure Jackson to not include that object in the json at all?

我希望行为类似于xml / soap中的unwrapped集合,如果集合为空,则不包括在内。我不需要区分null和empty集合,并希望减小json有效负载的大小。

I want behaviour similar to "unwrapped" collections in xml/soap where if the collection is empty it is not included. I do not need to distinguish between null and empty collection and want to reduce the size of the json payload.

推荐答案

这可能是很长一段时间但是如何使用Inclusions和定义NON_DEFAULT作为包含属性。文档说:

This may be a long shot but how about using Inclusions and defining NON_DEFAULT as the inclusion property. The docs say:

值,表示只有值与默认设置不同的属性(表示使用无参数构造函数构造Bean时的值)将被包括在内。

"Value that indicates that only properties that have values that differ from default settings (meaning values they have when Bean is constructed with its no-arguments constructor) are to be included."

因此,如果默认值为空数组,则应跳过它。

So if the default value is an empty array it should skip it.

类似于:

ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_DEFAULT);


public class Test {
     String[] array = { };
     ....
}

http://jackson.codehaus.org/1.1.2/javadoc/org/codehaus/ jackson / map / annotate / JsonSerialize.Inclusion.html

这篇关于如何不在杰克逊发送空集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆