gpt4 book ai didi

java - Jackson Json序列化: Remove Blank strings

转载 作者:行者123 更新时间:2023-12-02 09:05:49 28 4
gpt4 key购买 nike

我尝试使用 Jackson 从生成的 Json 中排除所有空白字符串。

我知道我可以使用下面的注释来过滤它,但这似乎不能处理空白字符串。[只有空格的字符串]

@JsonInclude(JsonInclude.Include.NON_EMPTY) 

有办法做到这一点吗?

最佳答案

您可以使用自定义值过滤器,请尝试此操作并告诉我这是否适合您 -

@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = CustomFilter.class)

这是自定义过滤器 -

class CustomFilter {
public CustomFilter() {
}
@Override
public boolean equals(Object obj) {
if(obj == null)
return true;
if(obj instanceof String){
return ((String)obj).trim().isEmpty();
}
return false;
}
}
<小时/>

根据自定义过滤器的 javadoc -

public static final JsonInclude.Include CUSTOM

Value that indicates that separate filter Object (specified by JsonInclude.valueFilter() for value itself, and/or JsonInclude.contentFilter() for contents of structured types) is to be used for determining inclusion criteria. Filter object's equals() method is called with value to serialize; if it returns true value is excluded (that is, filtered out); if false value is included.

关于java - Jackson Json序列化: Remove Blank strings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59821176/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com