名称含义备注
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”;JSONString(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(),导致反序列化出错。
1.问题
在使⽤fastjson中的JSONString⽅法将对象转换成json字符串的时候,发现有些字段没有了。如:public static void main(String[] args) {
Map<String,Object>map=new HashMap<>();
map.put("id","1");
map.put("name",null);
System.out.JSONString(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.JSONString(map,SerializerFeature.WriteMapNullValue));
}
输出结果: {"name":null,"id":"1"}
3.原因
转json串,默认设置了许多序列化配置:
WriteEnumUsingName 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
*
* /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]
*/
public enum SerializerFeature {
QuoteFieldNames,
/**
*
*/
UseSingleQuotes,
/**
*
*/
WriteMapNullValue,
/
**
write的返回值* ⽤枚举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]; }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论