@see https://blog.csdn.net/u010246789/article/details/52539576
rely
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
SerializerFeature property
name | meaning | Remark |
---|---|---|
QuoteFieldNames | Whether to use double quotes when outputting the key, the default is true | |
UseSingleQuotes | use single quotes instead of double quotes, defaults to false | |
WriteMapNullValue | Whether to output a field whose value is null, the default is false | |
WriteEnumUsingToString | Enum outputs name() or original, default is false | |
UseISO8601DateFormat | Date is output in ISO8601 format, the default is false | |
WriteNullListAsEmpty | If the List field is null, the output is [] instead of null | |
WriteNullStringAsEmpty | If the character type field is null, the output is "" instead of null | |
WriteNullNumberAsZero | If the value field is null, the output is 0, not null | |
WriteNullBooleanAsFalse | If the Boolean field is null, the output is false, not null | |
SkipTransientField | If it is true, the Field corresponding to the Get method in the class is transient, which will be ignored during serialization. Defaults to true | |
SortField | Output sorted by field name. Default is false | |
WriteTabAsSpecial | Make \t escape output, the default is false | 不推荐 |
PrettyFormat | Whether the result is formatted, the default is false | |
WriteClassName | Write type information when serializing, default is false. deserialization is required | |
DisableCircularReferenceDetect | Eliminates problems with circular references to the same object, defaults to false | |
WriteSlashAsSpecial | Escape the slash '/' | |
BrowserCompatible | The Chinese will be serialized into \uXXXX format, the number of bytes will be more, but it is compatible with IE 6, the default is false | |
WriteDateUseDateFormat | Globally modify the date format, the default is false. JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat); | |
DisableCheckSpecialChar | If there are special characters such as double quotes in the string property of an object, it will be converted to json with a backslash transfer character. If escaping is not required, this property can be used. Default is false | |
NotWriteRootClassName | meaning | |
BeanToArray | Convert object to array output | |
WriteNonStringKeyAsString | meaning | |
NotWriteDefaultValue | meaning | |
BrowserSecure | meaning | |
IgnoreNonFieldGetter | meaning | |
WriteEnumUsingName | meaning |
Example
Prepare
- User, Word to simulate various data types.
- SerializerFeatureTest: Example method for JSON partial example.
User type: default get, set method
public class User {
private int id;
private String name;
private String add;
private String old;
}
Word type: default get, set method
public 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;
}
SerializerFeatureTest: Test class
public class SerializerFeatureTest {
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 HashedMap();
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();
}
/**
* 9:自定义
* 格式化输出
* 显示值为null的字段
* 将为null的字段值显示为""
* DisableCircularReferenceDetect:消除循环引用
*/
private static void showJsonBySelf() {
System.out.println(JSON.toJSONString(word));
System.out.println(JSON.toJSONString(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.println(JSON.toJSONString(word));
System.out.println(JSON.toJSONString(word, SerializerFeature.BeanToArray));
}
/**
* 7:
* WriteDateUseDateFormat:全局修改日期格式,默认为false。
*/
private static void writeDateUseDateFormat() {
word.setMap(null);
word.setList(null);
System.out.println(JSON.toJSONString(word));
JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";
System.out.println(JSON.toJSONString(word, SerializerFeature.WriteDateUseDateFormat));
}
/**
* 6:
* PrettyFormat
*/
private static void prettyFormat() {
word.setMap(null);
word.setList(null);
System.out.println(JSON.toJSONString(word));
System.out.println(JSON.toJSONString(word, SerializerFeature.PrettyFormat));
}
/**
* SortField:按字段名称排序后输出。默认为false
* 这里使用的是fastjson:为了更好使用sort field martch优化算法提升parser的性能,fastjson序列化的时候,
* 缺省把SerializerFeature.SortField特性打开了。
* 反序列化的时候也缺省把SortFeidFastMatch的选项打开了。
* 这样,如果你用fastjson序列化的文本,输出的结果是按照fieldName排序输出的,parser时也能利用这个顺序进行优化读取。
* 这种情况下,parser能够获得非常好的性能。
*/
private static void sortField() {
System.out.println(JSON.toJSONString(word));
System.out.println(JSON.toJSONString(word, SerializerFeature.SortField));
}
/**
* 5:
* WriteNullStringAsEmpty:字符类型字段如果为null,输出为"",而非null
* 需要配合WriteMapNullValue使用,现将null输出
*/
private static void writeNullStringAsEmpty() {
word.setE(null);
System.out.println(JSONObject.toJSONString(word));
System.out.println("设置WriteMapNullValue后:");
System.out.println(JSONObject.toJSONString(word, SerializerFeature.WriteMapNullValue));
System.out.println("设置WriteMapNullValue、WriteNullStringAsEmpty后:");
System.out.println(JSONObject.toJSONString(word, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty));
}
/**
* 4:
* WriteNullListAsEmpty:List字段如果为null,输出为[],而非null
* 需要配合WriteMapNullValue使用,现将null输出
*/
private static void writeNullListAsEmpty() {
word.setList(null);
System.out.println(JSONObject.toJSONString(word));
System.out.println("设置WriteNullListAsEmpty后:");
System.out.println(JSONObject.toJSONString(word, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty));
}
/**
* 3:
* UseISO8601DateFormat:Date使用ISO8601格式输出,默认为false
*/
private static void useISO8601DateFormat() {
System.out.println(JSONObject.toJSONString(word));
System.out.println("设置UseISO8601DateFormat后:");
System.out.println(JSONObject.toJSONString(word, SerializerFeature.UseISO8601DateFormat));
}
/**
* 2:
* WriteMapNullValue:是否输出值为null的字段,默认为false
*/
private static void writeMapNullValue() {
System.out.println(JSONObject.toJSONString(word));
System.out.println("设置WriteMapNullValue后:");
System.out.println(JSONObject.toJSONString(word, SerializerFeature.WriteMapNullValue));
}
/**
* 1:
* UseSingleQuotes:使用单引号而不是双引号,默认为false
*/
private static void useSingleQuotes() {
System.out.println(JSONObject.toJSONString(word));
System.out.println("设置useSingleQuotes后:");
System.out.println(JSONObject.toJSONString(word, SerializerFeature.UseSingleQuotes));
}
}
The corresponding output results are as follows:
1、useSingleQuotes:
2 、 writeMapNullValue :
3、useISO8601DateFormat:
4、writeNullListAsEmpty:
5、writeNullStringAsEmpty:
6、prettyFormat:
7、writeDateUseDateFormat:
8、beanToArray:
9. Custom combination: showJsonBySelf:
The complete output at this time is as follows:
{"a":"a","b":2,"c":true,"d":"d","date":1473839656840,"e":"","list":[{"add":"广州","id":3,"name":"用户3","old":"33"},{"add":"上海","id":2,"name":"用户2","old":"22"},null,{"add":"北京","id":1,"name":"用户1","old":"11"}],"map":{"list":[{"$ref":"$.list[0]"},{"$ref":"$.list[1]"},null,{"$ref":"$.list[3]"}],"user3":{"$ref":"$.list[0]"},"mapz":"mapz","mapo":"mapo","mapa":"mapa","user1":{"$ref":"$.list[3]"}}}
{
"a":"a",
"b":2,
"c":true,
"d":"d",
"date":1473839656840,
"e":"",
"f":"",
"list":[
{
"add":"广州",
"id":3,
"name":"用户3",
"old":"33"
},
{
"add":"上海",
"id":2,
"name":"用户2",
"old":"22"
},
null,
{
"add":"北京",
"id":1,
"name":"用户1",
"old":"11"
}
],
"map":{
"list":[
{
"add":"广州",
"id":3,
"name":"用户3",
"old":"33"
},
{
"add":"上海",
"id":2,
"name":"用户2",
"old":"22"
},
null,
{
"add":"北京",
"id":1,
"name":"用户1",
"old":"11"
}
],
"user4":null,
"user3":{
"add":"广州",
"id":3,
"name":"用户3",
"old":"33"
},
"mapz":"mapz",
"mapo":"mapo",
"mapa":"mapa",
"user1":{
"add":"北京",
"id":1,
"name":"用户1",
"old":"11"
}
}
}
Note:
- fastjson converts objects to json to avoid $ref
学习地址:
- http://blog.csdn.net/glarystar/article/details/6654494
- http://blog.csdn.net/u013163567/article/details/50736096
项目github地址:
- https://github.com/gubaijin/buildmavenweb