如何避免HashMap中的空值序列化? [英] How to avoid null values serialization in HashMap?

查看:164
本文介绍了如何避免HashMap中的空值序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Jackson JSON处理器将HashMap序列化为字符串。
例如:

I would like to serialize a HashMap as a string through the Jackson JSON processor. For example:

String strMap = getMapper().writeValueAsString(myHashMap);
result output -> {"r_id":6,"a_am":null,"smb":"Submit","a_li":null,"l_id":878,"pos":[1345,1346,1347]}

我不知道如何禁用Map的空值序列化。如果像这样配置Jackson,它只对POJO有效:

I don't know how to disable null values serialization for Map. It works fine only for POJO if configure the Jackson like this:

mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);


推荐答案

对于它的价值,Jackson 1.6将拥有:

For what it's worth, Jackson 1.6 will have this:

objectMapper.configure(SerializationConfig.WRITE_NULL_MAP_VALUES, false);

这可以做你想要的。现有方法仅适用于bean,并且不会被更改以确保最大向后兼容性。

which does do what you want. Existing method only works for beans, and is not being changed to ensure maximum backwards compatibility.

编辑:根据评论的注释,这是针对Jackson 1.x; Jackson 2.x已匹配 SerializationFeature

as per note on comments, this is for Jackson 1.x; Jackson 2.x has matching SerializationFeature

这篇关于如何避免HashMap中的空值序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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