fastjson数据格式转换SerializerFeature属性详解SerializerFeature属性
名称含义备注
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含义
NotWriteDefaultValue含义
BrowserSecure含义
IgnoreNonFieldGetter含义
WriteEnumUsingName含义
⽰例
准备
User、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.serializer.SerializerFeature;
import lombok.AllArgsConstructor;
import lombok.Data;
public class FastjsonFormat {
private static Word word;
private static void init() {
word = new Word();
word.setA("a");
word.setB(2);
word.setC(true);
word.setD("d");
word.setE("");
word.setF(null);
word.setDate(new Date());
List<User> list = new ArrayList<User>();
User user1 = new User();
user1.setId(1);
user1.setOld("11");
user1.setName("⽤户1");
user1.setAdd("北京");
User user2 = new User();
user2.setId(2);
user2.setOld("22");
user2.setName("⽤户2");
user2.setAdd("上海");
User user3 = new User();
user3.setId(3);
user3.setOld("33");
user3.setName("⽤户3");
user3.setAdd("⼴州");
list.add(user3);
list.add(user2);
list.add(null);
list.add(user1);
word.setList(list);
Map<String, Object> map = new HashMap<>();
map.put("mapa", "mapa");
map.put("mapo", "mapo");
map.put("mapz", "mapz");
map.put("user1", user1);
map.put("user3", user3);
map.put("user4", null);
map.put("list", list);
word.setMap(map);
}
public static void main(String[] args) {
init();
useSingleQuotes();
writeMapNullValue();
useISO8601DateFormat();
writeNullListAsEmpty();
writeNullStringAsEmpty();
sortField();
prettyFormat();
writeDateUseDateFormat();
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.setMap(null);
word.setList(null);
System.out.JSONString(word));
System.out.JSONString(word, SerializerFeature.BeanToArray));
}
/**
* 7: WriteDateUseDateFormat:全局修改⽇期格式,默认为false。
*/
private static void writeDateUseDateFormat() {
word.setMap(null);
word.setList(null);
System.out.JSONString(word));
JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";
System.out.JSONString(word, SerializerFeature.WriteDateUseDateFormat));
}
/**
* 6: PrettyFormat
*/
private static void prettyFormat() {
word.setMap(null);
word.setList(null);
System.out.JSONString(word));
System.out.JSONString(word, SerializerFeature.PrettyFormat));
}
/**
* SortField:按字段名称排序后输出。默认为false 这⾥使⽤的是fastjson:为了更好使⽤sort field
* martch优化算法提升parser的性能,fastjson序列化的时候,
* 缺省把SerializerFeature.SortField特性打开了。反序列化的时候也缺省把SortFeidFastMatch的选项打开了。
* 这样,如果你⽤fastjson序列化的⽂本,输出的结果是按照fieldName排序输出的,parser时也能利⽤这个顺序进⾏优化读取。  * 这种情况下,parser能够获得⾮常好的性能。
*/
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.setE(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.setList(null);
System.out.JSONString(word));
System.out.println("设置WriteNullListAsEmpty后:");
System.out.JSONString(word, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullListAsEmpty));
}
/**
* 3: UseISO8601DateFormat:Date使⽤ISO8601格式输出,默认为false
*/
private static void useISO8601DateFormat() {
System.out.JSONString(word));
System.out.println("设置UseISO8601DateFormat后:");
System.out.JSONString(word, SerializerFeature.UseISO8601DateFormat));
}
/**
* 2: WriteMapNullValue:是否输出值为null的字段,默认为false
*/
private static void writeMapNullValue() {
System.out.JSONString(word));
System.out.println("设置WriteMapNullValue后:");
System.out.JSONString(word, SerializerFeature.WriteMapNullValue));
}
/
**
* 1: UseSingleQuotes:使⽤单引号⽽不是双引号,默认为false
*/
private static void useSingleQuotes() {
System.out.JSONString(word));
System.out.println("设置useSingleQuotes后:");
System.out.JSONString(word, SerializerFeature.UseSingleQuotes)); }
}
interface BodyInfo {
}
@Data
@AllArgsConstructor
class Height implements BodyInfo {
int height;
}
@Dataphpjson格式化输出
@AllArgsConstructor
class Person {
String name;
int age;
String[] nickNames;
BodyInfo[] bodyInfos;
}
@Data
@AllArgsConstructor
class Weight implements BodyInfo {
int weight;
}
@Data
class User {
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<User> list;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。