我正在使用Fasterxml Jackson 2.2.2
我有一个带有boolean
(原始)属性的简单pojo。当默认BeanSerializer
和BeanPropertyWritter
尝试对其进行序列化时,如果其值为false
,则会跳过此属性。
我想:
{"id":1, "enabled":false}
我得到的是:
{"id":1}
BeanPropertyWritter
中的代码是:
// and then see if we must suppress certain values (default, empty)
if (_suppressableValue != null) {
if (MARKER_FOR_EMPTY == _suppressableValue) {
if (ser.isEmpty(value)) {
return;
}
} else if (_suppressableValue.equals(value)) {
return;
}
}
我调试了它,发现BeanPropertyWritter._suppressableValue
等于Boolean(false)
,所以当假布尔到达此块时,它只返回并且没有返回输出。
我有什么选择?我可以配置属性的写入器来取消设置其_suppressableValue
吗?什么是最简单,最简单的解决方案?
答案 0 :(得分:1)
根据建议,您的ObjectMapper
设置可能是非默认设置,并指定NON_DEFAULT
的包含策略。
但是你可以添加@JsonInclude
来覆盖你的POJO类,甚至是布尔属性本身:确保使用Inclusion.ALWAYS
。