1.問題
在使用fastjson中的JSON.toJSONString方法將對象轉換成json字符串的時候,發現有些字段沒有了。如:
public static void main(String[] args) {
Map<String,Object>map=new HashMap<>();
map.put("id","1");
map.put("name",null);
System.out.println(JSON.toJSONString(map));
}
輸出結果: {"id":"1"}
輸出結果中沒有了 "name"這個屬性了;
2.解決方式
加SerializerFeature.WriteMapNullValue)
public static void main(String[] args) { Map<String,Object>map=new HashMap<>(); map.put("id","1"); map.put("name",null); System.out.println(JSON.toJSONString(map,SerializerFeature.WriteMapNullValue)); } 輸出結果: {"name":null,"id":"1"}
3.原因
轉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。 使用WriteEnumUsingToString方法可以序列化時將Enum轉換為toString()的返回值;同時override toString函數能夠將enum值輸出需要的形式。但是這樣做會帶來一個問題,對應的反序列化使用的Enum的靜態方法valueof可能無法識別自行生成的toString(),導致反序列化出錯。 如果將節省enum序列化后的大小,可以將enum序列化其ordinal值,保存為int類型。fastJson在反序列化時,如果值為int,則能夠使用ordinal值匹配,找到合適的對象。 fastjson要將enum序列化為ordinal只需要禁止WriteEnumUsingName feature。 首先根據默認的features排除WriteEnumUsingName,然后使用新的features序列化即可。 |
4.源碼
/*
* Copyright 1999-2101 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.fastjson.serializer;
/**
* @author wenshao[szujobs@hotmail.com]
*/
public enum SerializerFeature {
QuoteFieldNames,
/**
*
*/
UseSingleQuotes,
/**
*
*/
WriteMapNullValue,
/**
* 用枚舉toString()值輸出
*/
WriteEnumUsingToString,
/**
* 用枚舉name()輸出
*/
WriteEnumUsingName,
/**
*
*/
UseISO8601DateFormat,
/**
* @since 1.1
*/
WriteNullListAsEmpty,
/**
* @since 1.1
*/
WriteNullStringAsEmpty,
/**
* @since 1.1
*/
WriteNullNumberAsZero,
/**
* @since 1.1
*/
WriteNullBooleanAsFalse,
/**
* @since 1.1
*/
SkipTransientField,
/**
* @since 1.1
*/
SortField,
/**
* @since 1.1.1
*/
@Deprecated
WriteTabAsSpecial,
/**
* @since 1.1.2
*/
PrettyFormat,
/**
* @since 1.1.2
*/
WriteClassName,
/**
* @since 1.1.6
*/
DisableCircularReferenceDetect, // 32768
/**
* @since 1.1.9
*/
WriteSlashAsSpecial,
/**
* @since 1.1.10
*/
BrowserCompatible,
/**
* @since 1.1.14
*/
WriteDateUseDateFormat,
/**
* @since 1.1.15
*/
NotWriteRootClassName,
/**
* @since 1.1.19
* @deprecated
*/
DisableCheckSpecialChar,
/**
* @since 1.1.35
*/
BeanToArray,
/**
* @since 1.1.37
*/
WriteNonStringKeyAsString,
/**
* @since 1.1.42
*/
NotWriteDefaultValue,
/**
* @since 1.2.6
*/
BrowserSecure,
/**
* @since 1.2.7
*/
IgnoreNonFieldGetter,
/**
* @since 1.2.9
*/
WriteNonStringValueAsString,
/**
* @since 1.2.11
*/
IgnoreErrorGetter
;
SerializerFeature(){
mask = (1 << ordinal());
}
public final int mask;
public final int getMask() {
return mask;
}
public static boolean isEnabled(int features, SerializerFeature feature) {
return (features & feature.mask) != 0;
}
public static boolean isEnabled(int features, int fieaturesB, SerializerFeature feature) {
int mask = feature.mask;
return (features & mask) != 0 || (fieaturesB & mask) != 0;
}
public static int config(int features, SerializerFeature feature, boolean state) {
if (state) {
features |= feature.mask;
} else {
features &= ~feature.mask;
}
return features;
}
public static int of(SerializerFeature[] features) {
if (features == null) {
return 0;
}
int value = 0;
for (SerializerFeature feature: features) {
value |= feature.mask;
}
return value;
}
public final static SerializerFeature[] EMPTY = new SerializerFeature[0];
}
