fastjson1和fastjson2利用过滤器ValueFilter实现null转自定义字符串
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json

·
fastjson默认将对象转为json 不保留值为null的属性,如果需要保留使用以下方式
fastjson1 保留空值属性
属性为null原样输出使用WriteMapNullValue
JSON.toJSONString(data, SerializerFeature.WriteMapNullValue)
属性为null转为空字符输出使用WriteNullStringAsEmpty
JSON.toJSONString(data, SerializerFeature.WriteNullStringAsEmpty)
fastjson2 保留空值属性
属性为null原样输出使用WriteMapNullValue
JSON.toJSONString(data, SerializerFeature.WriteMapNullValue)
转为空字符使用NullAsDefaultValue
JSON.toJSONString(data, SerializerFeature.NullAsDefaultValue)
保留空值属性,转化为自定义字符串
有时候我们在将对象转为json字符串的时候,需要将属性值的null转换为自定义字符串,或者实现字段脱敏等操作,可以利用valueFilter实现替换。
fastJson1 中可以直接使用valueFilter
import com.alibaba.fastjson.serializer.ValueFilter;
String s = JSON.toJSONString(data, (ValueFilter)(object, ame, value)-> {
if(value == null){
return " ";
}
return value;
});
fastJson2 中可以需要配合 JSONWriter.Feature.WriteMapNullValue
import com.alibaba.fastjson2.filter.ValueFilter;
String s = JSON.toJSONString(data, (ValueFilter)(object, ame, value)-> {
if(value == null){
return " ";
}
return value;
}, JSONWriter.Feature.WriteMapNullValue);




适用于现代 C++ 的 JSON。
最近提交(Master分支:30 天前 )
68c25aec
* :memo: update customers
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :memo: add badge to Cloudback
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me> 16 小时前
ac0133ea
Bumps [mkdocs-git-revision-date-localized-plugin](https://github.com/timvink/mkdocs-git-revision-date-localized-plugin) from 1.4.5 to 1.4.6.
- [Release notes](https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/releases)
- [Commits](https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/compare/v1.4.5...v1.4.6)
---
updated-dependencies:
- dependency-name: mkdocs-git-revision-date-localized-plugin
dependency-version: 1.4.6
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 1 天前
更多推荐
所有评论(0)