FastJson 版本:1.2.75
eg:{ “name”: “zhangsan”, “age”: 18 }
一、序列化特性
引号
特性 | 默认值 | 作用 | 示例 |
---|---|---|---|
QuoteFieldNames | true | 使用双引号 | |
UseSingleQuotes | false | 使用单引号 | { ‘name’: ‘zhangsan’, ‘age’: 18 } |
null
特性 | 默认值 | 作用 | 示例 |
---|---|---|---|
WriteMapNullValue | false | 值为 null 字段也返回 | |
WriteNullStringAsEmpty | false | null -> “” | |
WriteNullNumberAsZero | false | null -> 0 | |
WriteNullBooleanAsFalse | false | null -> false | |
WriteNullListAsEmpty | false | null -> [] |
值特殊处理
特性 | 默认值 | 作用 | 示例 |
---|---|---|---|
NotWriteDefaultValue | false | 不使用默认属性值 | |
WriteNonStringKeyAsString | false | 将 JSON 中 非 String 类型 key 值 转为 String | |
WriteNonStringValueAsString | false | 将 JSON 中 非 String 类型 value 值 转为 String 类型 | |
WriteBigDecimalAsPlain | false | 禁用科学计数,显示原内容 | |
BeanToArray | false | 返回对象属性值 array | [“zhangsan”, 22] |
PrettyFormat | false | 美化 JSON | postman 响应 -> body -> Row 看效果 |
序列化时忽略
特性 | 默认值 | 作用 | 示例 |
---|---|---|---|
SkipTransientField | true | 序列化时忽略 transient 字段 | private transient String userId; |
IgnoreNonFieldGetter | true | 忽略没有 Getter 方法的属性 | |
IgnoreErrorGetter | false | 忽略 Getter 方法报错,正常执行下边的序列化 |
时间
特性 | 默认值 | 作用 | 示例 |
---|---|---|---|
UseISO8601DateFormat | false | Date 使用 ISO8601 格式输出 | |
WriteDateUseDateFormat | 使用指定的 DateFormat 格式化 Date |
排序
特性 | 默认值 | 作用 | 示例 |
---|---|---|---|
SortField | false | 按字段名称排序后输出 | {“aa”: “zhangsan”, “ab”: “lisi” } |
MapSortField | false | 使用 Map key 值排序后输出 |
枚举
特性 | 默认值 | 作用 | 示例 |
---|---|---|---|
WriteEnumUsingToString | 用枚举 toString() 值输出 | ||
WriteEnumUsingName | 用枚举 name() 输出 |
类信息
特性 | 默认值 | 作用 | 示例 |
---|---|---|---|
WriteClassName | false | 序列化时写入类型信息 | {"@type": “com.zxguan.mvc.po.Stu”, “age”: 22, “name”: “zhangsan” } |
NotWriteRootClassName | |||
DisableCircularReferenceDetect | 禁止同一对象循环引用序列化 |
安全、转义
特性 | 默认值 | 作用 | 示例 |
---|---|---|---|
BrowserCompatible | 将中文序列化为 \uXXXX 格式 | ||
BrowserSecure | XSS 攻击处理,安全转义 | ||
WriteSlashAsSpecial | 斜杠 / 进行转义 | ||
@Deprecated WriteTabAsSpecial | 把 \t 做转义 |
二、enum SerializerFeature
package com.alibaba.fastjson.serializer;
/**
* @author wenshao[szujobs@hotmail.com]
*/
public enum SerializerFeature {
/**
* 输出 key 时是否使用双引号,默认为 true
* */
QuoteFieldNames,
/**
* 使用单引号而不是双引号,默认为 false
* <p>
* {
* 'name': 'zhangsan',
* 'age': 18
* }
* </p>
* */
UseSingleQuotes,
/**
* 是否输出值为 null 的字段,默认为 false
* */
WriteMapNullValue,
/**
* 用枚举 toString() 值输出
* */
WriteEnumUsingToString,
/**
* 用枚举 name() 输出
* */
WriteEnumUsingName,
/**
* Date 使用 ISO8601 格式输出,默认为 false
* */
UseISO8601DateFormat,
/**
* List null -> []
* @since 1.1
* */
WriteNullListAsEmpty,
/**
* String null -> ""
* @since 1.1
* */
WriteNullStringAsEmpty,
/**
* Number null -> 0
* @since 1.1
* */
WriteNullNumberAsZero,
/**
* Boolean null -> false
* @since 1.1
* */
WriteNullBooleanAsFalse,
/**
* 序列化时忽略 transient 字段,默认为 true
* <p>
* private transient String userId;
* </p>
* @since 1.1
* */
SkipTransientField,
/**
* 按字段名称排序后输出。默认为 false
* <p>
* {
* "aa": "zhangsan",
* "ab": "lisi"
* }
* </p>
* @since 1.1
* */
SortField,
/**
* 把 '\t' 做转义输出,默认为 false
* @since 1.1
* @Deprecated
* */
WriteTabAsSpecial,
/**
* 美化 JSON,默认为 false
* <p>
* postman 响应 -> body -> Row
* {
* "age": 22,
* "name": "zhangsan"
* }
* </p>
* @since 1.1.2
* */
PrettyFormat,
/**
* 序列化时写入类型信息,默认为 false。反序列化是需用到
* <p>
* {
* "@type": "com.zxguan.mvc.po.Stu",
* "age": 22,
* "name": "zhangsan"
* }
* </p>
* @since 1.1.2
* */
WriteClassName,
/**
* 消除对同一对象循环引用的问题,默认为 false
* @since 1.1.6
* */
DisableCircularReferenceDetect, // 32768
/**
* 对斜杠 ’/’ 进行转义
* @since 1.1.9
* */
WriteSlashAsSpecial,
/**
* 将中文都会序列化为 \uXXXX 格式,字节数会多一些,但是能兼容 IE6,默认为 false
* <p>
* {
* "age": 22,
* "name":"\u5566\u5566\u5566"
* }
* </p>
* @since 1.1.10
* */
BrowserCompatible,
/**
* 使用指定的 DateFormat 格式化 Date ,默认为 false
* <p>
* long 类型时间戳按着给定的格式格式化
* </p>
* @since 1.1.14
* */
WriteDateUseDateFormat,
/**
* @since 1.1.15
* */
NotWriteRootClassName,
/**
* 一个对象的字符串属性中如果有特殊字符如双引号,将会在转成 json 时带有反斜杠转移符。如果不需要转义,可以使用这个属性。默认为 false
* @since 1.1.19
* @deprecated
* */
DisableCheckSpecialChar,
/**
* 将对象属性值转为 array 输出
* @since 1.1.35
* */
BeanToArray,
/**
* 将 JSON key 类型转为 String
* @since 1.1.37
* */
WriteNonStringKeyAsString,
/**
* 不写默认属性值
* @since 1.1.42
* */
NotWriteDefaultValue,
/**
* XSS攻击处理 安全转义
* @since 1.2.6
* */
BrowserSecure,
/**
* 忽略没有 Getter 方法的属性
* @since 1.2.7
* */
IgnoreNonFieldGetter,
/**
* 将 非 String 类型属性值转为 String 类型
* @since 1.2.9
* */
WriteNonStringValueAsString,
/**
* 正常来说,服务端序列化时,get方法抛异常,导致序列化失败
* 此属性用于忽略那些抛错的 getter 方法,正常执行下边的序列化
* @since 1.2.11
* */
IgnoreErrorGetter,
/**
* 禁用科学计数,显示原内容
* @since 1.2.16
* */
WriteBigDecimalAsPlain,
/**
* 使用 Map key 值排序后输出
* @since 1.2.27
* */
MapSortField;
SerializerFeature(){
// 掩码 = ordinal() 左移一位
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 featuresB, SerializerFeature feature) {
int mask = feature.mask;
return (features & mask) != 0 || (featuresB & 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];
public static final int WRITE_MAP_NULL_FEATURES
= WriteMapNullValue.getMask()
| WriteNullBooleanAsFalse.getMask()
| WriteNullListAsEmpty.getMask()
| WriteNullNumberAsZero.getMask()
| WriteNullStringAsEmpty.getMask()
;
}