fastjson数据格式转换SerializerFeature属性详解SerializerFeature属性
名称含义备注表达爱意的话
QuoteFieldNames输出key时是否使⽤双引号,默认为true
USingleQuotes使⽤单引号⽽不是双引号,默认为fal
WriteMapNullValue是否输出值为null的字段,默认为fal
WriteEnumUsingToString Enum输出name()或者original,默认为fal
UISO8601DateFormat Date使⽤ISO8601格式输出,默认为fal
WriteNullListAsEmpty List字段如果为null,输出为[],⽽⾮null
WriteNullStringAsEmpty字符类型字段如果为null,输出为”“,⽽⾮null
WriteNullNumberAsZero数值字段如果为null,输出为0,⽽⾮null
WriteNullBooleanAsFal Boolean字段如果为null,输出为fal,⽽⾮null
SkipTransientField如果是true,类中的Get⽅法对应的Field是transient,序列化时将会被忽略。默认为true SortField按字段名称排序后输出。默认为fal
WriteTabAsSpecial把\t做转义输出,默认为fal 不推荐
PrettyFormat结果是否格式化,默认为fal
WriteClassName序列化时写⼊类型信息,默认为fal。反序列化是需⽤到DisableCircularReferenceDetect消除对同⼀对象循环引⽤的问题,默认为fal
WriteSlashAsSpecial对斜杠’/’进⾏转义
BrowrCompatible将中⽂都会序列化为\uXXXX格式,字节数会多⼀些,但是能兼容IE 6,默认为fal
WriteDateUDateFormat全局修改⽇期格式,默认为fal。JSON.DEFFAULT_DATE_FORMAT = “yyyy-MM-
dd”;JSONString(obj, SerializerFeature.WriteDateUDateFormat);
DisableCheckSpecialChar⼀个对象的字符串属性中如果有特殊字符如双引号,将会在转成json时带有反斜杠转移符。如果不需
要转义,可以使⽤这个属性。默认为fal
NotWriteRootClassName含义
BeanToArray将对象转为array输出
WriteNonStringKeyAsString含义
NotWriteDefaultValue含义
BrowrSecure含义
IgnoreNonFieldGetter含义
WriteEnumUsingName含义
⽰例
准备
Ur、Word来模拟各种数据类型。
SerializerFeatureTest:JSON部分⽰例的⽰例⽅法。
package com.fastjson;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.rializer.SerializerFeature;
import lombok.AllArgsConstructor;
import lombok.Data;
public class FastjsonFormat {
private static Word word;
private static void init() {
word = new Word();
word.tA("a");
word.tB(2);
word.tC(true);
word.tD("d");
word.tE("");
word.tF(null);
word.tDate(new Date());
List<Ur> list = new ArrayList<Ur>();
Ur ur1 = new Ur();
ur1.tId(1);
ur1.tOld("11");
ur1.tName("⽤户1");
ur1.tAdd("北京");
Ur ur2 = new Ur();
ur2.tId(2);
ur2.tOld("22");
ur2.tName("⽤户2");
ur2.tAdd("上海");
Ur ur3 = new Ur();
ur3.tId(3);
ur3.tOld("33");
ur3.tName("⽤户3");
ur3.tAdd("⼴州");
list.add(ur3);
list.add(ur2);
list.add(null);
list.add(ur1);
小升初数学题
word.tList(list);
Map<String, Object> map = new HashMap<>();
map.put("mapa", "mapa");
map.put("mapo", "mapo");
map.put("mapz", "mapz");
map.put("ur1", ur1);
map.put("ur3", ur3);
map.put("ur4", null);
map.put("list", list);
word.tMap(map);
}
public static void main(String[] args) {
init();
uSingleQuotes();
writeMapNullValue();男生真爱一个人的表现
uISO8601DateFormat();
writeNullListAsEmpty();
writeNullStringAsEmpty();
sortField();
prettyFormat();
writeDateUDateFormat();
beanToArray();
showJsonBySelf();
Person person = new Person(null, 28, new String[] { "", "明明" },
new BodyInfo[] { new Height(188), new Weight(150) });
System.out.println(person);
// 去null值
System.out.JSONString(person));
// 美化,format湮远
System.out.JSONString(person, SerializerFeature.PrettyFormat));
}
/**
* 9:⾃定义格式化输出显⽰值为null的字段将为null的字段值显⽰为""
* DisableCircularReferenceDetect:消除循环引⽤
两个一百年是什么*/
private static void showJsonBySelf() {
System.out.JSONString(word));
System.out.JSONString(word, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteNullListAsEmpty));
}
/**
* 8: 将对象转为array输出
*/
private static void beanToArray() {
word.tMap(null);
word.tList(null);
双一流建设高校
System.out.JSONString(word));
System.out.JSONString(word, SerializerFeature.BeanToArray));
}
/**
* 7: WriteDateUDateFormat:全局修改⽇期格式,默认为fal。
*/
private static void writeDateUDateFormat() {
word.tMap(null);
word.tList(null);
System.out.JSONString(word));
JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";
System.out.JSONString(word, SerializerFeature.WriteDateUDateFormat));
}
/**
* 6: PrettyFormat
*/
private static void prettyFormat() {
word.tMap(null);
word.tList(null);
System.out.JSONString(word));
System.out.JSONString(word, SerializerFeature.PrettyFormat));
}
/**
* SortField:按字段名称排序后输出。默认为fal 这⾥使⽤的是fastjson:为了更好使⽤sort field
* martch优化算法提升parr的性能,fastjson序列化的时候,
* 缺省把SerializerFeature.SortField特性打开了。反序列化的时候也缺省把SortFeidFastMatch的选项打开了。
* 这样,如果你⽤fastjson序列化的⽂本,输出的结果是按照fieldName排序输出的,parr时也能利⽤这个顺序进⾏优化读取。 * 这种情况下,parr能够获得⾮常好的性能。
*/
private static void sortField() {
System.out.JSONString(word));
System.out.JSONString(word, SerializerFeature.SortField));
}
/**
* 5: WriteNullStringAsEmpty:字符类型字段如果为null,输出为"",⽽⾮null
* 需要配合WriteMapNullValue使⽤,现将null输出
*/
private static void writeNullStringAsEmpty() {
word.tE(null);
System.out.JSONString(word));
System.out.println("设置WriteMapNullValue后:");
System.out.JSONString(word, SerializerFeature.WriteMapNullValue));
System.out.println("设置WriteMapNullValue、WriteNullStringAsEmpty后:");
System.out.JSONString(word, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty));
}
/**
* 4: WriteNullListAsEmpty:List字段如果为null,输出为[],⽽⾮null
* 需要配合WriteMapNullValue使⽤,现将null输出
*/
private static void writeNullListAsEmpty() {
word.tList(null);
System.out.JSONString(word));
System.out.println("设置WriteNullListAsEmpty后:");
System.out.JSONString(word, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullListAsEmpty));
}
/**
* 3: UISO8601DateFormat:Date使⽤ISO8601格式输出,默认为fal
*/
private static void uISO8601DateFormat() {
System.out.JSONString(word));
System.out.println("设置UISO8601DateFormat后:");
System.out.JSONString(word, SerializerFeature.UISO8601DateFormat));
}
/**
* 2: WriteMapNullValue:是否输出值为null的字段,默认为fal
*/
private static void writeMapNullValue() {
System.out.JSONString(word));
System.out.println("设置WriteMapNullValue后:");
System.out.JSONString(word, SerializerFeature.WriteMapNullValue));
}
/
**
情人节礼物送男朋友
* 1: USingleQuotes:使⽤单引号⽽不是双引号,默认为fal
*/
private static void uSingleQuotes() {
System.out.JSONString(word));
System.out.println("设置uSingleQuotes后:");
System.out.JSONString(word, SerializerFeature.USingleQuotes)); }
}
interface BodyInfo {
}
@Data
@AllArgsConstructor
class Height implements BodyInfo {
int height;
}
@Data
@AllArgsConstructor
class Person {
String name;
int age;
String[] nickNames;
BodyInfo[] bodyInfos;
}
@Data
@AllArgsConstructor
class Weight implements BodyInfo {
int weight;
}
@Data
class Ur {
private int id;
private String name;
private String add;
private String old;
}
@Data
class Word {
private String d;
private String e;抗战历史
private String f;
private String a;
private int b;
private boolean c;
private Date date;
private Map<String, Object> map;
private List<Ur> list;
}