在使用alibaba的fastjson做json序列化時,由於上游系統對於json的要求過於嚴格,
才發現了JSON.toJSONString使用時值為NULL的屬性被忽略的問題。
如果某個屬性的值為null,再被序列化為字符串是默認會被忽略,原因和解決方式詳述如下,
1,原因:
public static String toJSONString(Object object, SerializerFeature… features):
該方法將實體對象轉換成Json字符串時,如果不傳遞參數SerializerFeature.WriteMapNullValue,則忽略值為null的屬性。
轉json串,默認設置了許多序列化配置:
名稱 | 含義 |
QuoteFieldNames | 輸出key時是否使用雙引號,默認為true |
UseSingleQuotes | 使用單引號而不是雙引號,默認為false |
WriteMapNullValue | 是否輸出值為null的字段,默認為false |
WriteEnumUsingToString | Enum輸出name()或者original,默認為false |
UseISO8601DateFormat | Date使用ISO8601格式輸出,默認為false |
WriteNullListAsEmpty | List字段如果為null,輸出為[],而非null |
WriteNullStringAsEmpty | 字符類型字段如果為null,輸出為”“,而非null |
WriteNullNumberAsZero | 數值字段如果為null,輸出為0,而非null |
WriteNullBooleanAsFalse | Boolean字段如果為null,輸出為false,而非null |
SkipTransientField | 如果是true,類中的Get方法對應的Field是transient,序列化時將會被忽略。默認為true |
SortField | 按字段名稱排序后輸出。默認為false |
WriteTabAsSpecial | 把\t做轉義輸出,默認為false |
PrettyFormat | 結果是否格式化,默認為false |
WriteClassName | 序列化時寫入類型信息,默認為false。反序列化是需用到 |
DisableCircularReferenceDetect | 消除對同一對象循環引用的問題,默認為false |
WriteSlashAsSpecial | 對斜杠’/’進行轉義 |
BrowserCompatible | 將中文都會序列化為\uXXXX格式,字節數會多一些,但是能兼容IE 6,默認為false |
WriteDateUseDateFormat | 全局修改日期格式,默認為false。JSON.DEFFAULT_DATE_FORMAT = “yyyy-MM-dd”;JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat); |
DisableCheckSpecialChar | 一個對象的字符串屬性中如果有特殊字符如雙引號,將會在轉成json時帶有反斜杠轉移符。如果不需要轉義,可以使用這個屬性。默認為false |
NotWriteRootClassName | 不知道 |
BeanToArray | 將對象轉為array輸出 |
WriteNonStringKeyAsString | 不是String的字段寫為String |
NotWriteDefaultValue | 不設默認值 |
BrowserSecure | 不知道 |
IgnoreNonFieldGetter | 忽略沒有getter方法的屬性 |
WriteEnumUsingName | 目前版本的fastjson默認對enum對象使用WriteEnumUsingName屬性,因此會將enum值序列化為其Name。 |
2.解決方式
加SerializerFeature.WriteMapNullValue即可,具體如下:
String ret = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
引申閱讀,大佬詳細的程序展示,轉載自:https://blog.csdn.net/weixin_44516305/article/details/88821815
1 背景
在Java開發中,通常需要將一個實體對象轉換成Json字符串,使用FastJson來實現這種轉換十分方便,只要使用FastJson中JSONObject靜態類提供的toJSONString()靜態方法即可。但是在轉換時,我們可能需要指定使用實體對象的某些屬性來進行轉換,或者指定轉換時要排除實體對象的某些屬性。
FastJson提供的SerializeFilter類就可以實現這種需求,可以指定轉換時要包含的屬性,或者指定轉換時要排除的屬性。
JSONObject.toJSONString()默認忽略值為null的屬性這篇文章分析了JSONObject.toJSONString()將實體對象轉換成JSON字符串時默認是忽略值為null的屬性,以及如何設置包含值為null的屬性。本文則主要是演示使用SerializeFilter來指定包含或者排除的屬性,使得生成的JSON字符串中包含或者不包含某些屬性。
2 演示程序
2.1 此程序演示了使用JSONObject提供的以下方法將實體對象轉換成Json字符串:
public static String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features);
2.2 Java程序完整代碼如下:
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.spring.PropertyPreFilters; /** * 使用FastJson將實體對象轉換成Json字符串測試類 */ public class FastJsonApplication { public static void main(String[] args) { User user = new User(); user.setId(1L); user.setUsername("張三"); user.setPassword(""); user.setMobile(null); user.setCountry("中國"); user.setCity("武漢"); String jsonUser = null; /** * 指定排除屬性過濾器和包含屬性過濾器 * 指定排除屬性過濾器:轉換成JSON字符串時,排除哪些屬性 * 指定包含屬性過濾器:轉換成JSON字符串時,包含哪些屬性 */ String[] excludeProperties = {"country", "city"}; String[] includeProperties = {"id", "username", "mobile"}; PropertyPreFilters filters = new PropertyPreFilters(); PropertyPreFilters.MySimplePropertyPreFilter excludefilter = filters.addFilter(); excludefilter.addExcludes(excludeProperties); PropertyPreFilters.MySimplePropertyPreFilter includefilter = filters.addFilter(); includefilter.addIncludes(includeProperties); /** * 情況一:默認忽略值為null的屬性 */ jsonUser = JSONObject.toJSONString(user, SerializerFeature.PrettyFormat); System.out.println("情況一:\n" + jsonUser); /** * 情況二:包含值為null的屬性 */ jsonUser = JSONObject.toJSONString(user, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue); System.out.println("情況二:\n" + jsonUser); /** * 情況三:默認忽略值為null的屬性,但是排除country和city這兩個屬性 */ jsonUser = JSONObject.toJSONString(user, excludefilter, SerializerFeature.PrettyFormat); System.out.println("情況三:\n" + jsonUser); /** * 情況四:包含值為null的屬性,但是排除country和city這兩個屬性 */ jsonUser = JSONObject.toJSONString(user, excludefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue); System.out.println("情況四:\n" + jsonUser); /** * 情況五:默認忽略值為null的屬性,但是包含id、username和mobile這三個屬性 */ jsonUser = JSONObject.toJSONString(user, includefilter, SerializerFeature.PrettyFormat); System.out.println("情況五:\n" + jsonUser); /** * 情況六:包含值為null的屬性,但是包含id、username和mobile這三個屬性 */ jsonUser = JSONObject.toJSONString(user, includefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue); System.out.println("情況六:\n" + jsonUser); } /** * 用戶實體類 */ public static class User { private Long id;