1. 程式人生 > >com.alibaba.fastjson常用的SerializerFeature

com.alibaba.fastjson常用的SerializerFeature

個人筆記

SerializerFeature.WriteNullListAsEmpty , // List欄位如果為null,輸出為[],而非null
SerializerFeature.WriteNullStringAsEmpty, //字元型別欄位如果為null,輸出為”“,而非null
SerializerFeature.WriteDateUseDateFormat, //如果時間是data、時間戳型別,按照這種格式初始化時間 "yyyy-MM-dd HH:mm"
SerializerFeature.WriteNullNumberAsZero, //數值欄位如果為null,輸出為0,而非null
SerializerFeature.WriteNullBooleanAsFalse ,//Boolean欄位如果為null,輸出為false,而非null
SerializerFeature.SkipTransientField , //類中的Get方法對應的Field是transient,序列化時將會被忽略。預設為true
SerializerFeature.DisableCircularReferenceDetect //消除對同一物件迴圈引用的問題,預設為false

案例

package com.cn.dl;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Timestamp;
import java.util.Date;

/**
 * Created by yanshao on 2018/11/26.
 */
@Data
@NoArgsConstructor
@Builder
@AllArgsConstructor
public class Stu {
    private String name;
    private Integer age;
    private Boolean isMan;
    private String address = "beijing";
    private transient String interest = "ball";
    private Date createDate = new Date();
}
package com.cn.dl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.junit.Test;

public class SpringbootAopTestApplicationTests {

	@Test
	public void contextLoads() {

		Stu stu = new Stu();
		System.out.println(toJSON(stu));
		System.out.println(toJSON2(stu));
		System.out.println(toJSON3(stu));
	}

	/**
	 * 過濾欄位值等於null的欄位
	 * */
	private PropertyFilter filter = new PropertyFilter() {
		/**
		 * @param obj 當前物件
		 * @param key 當前物件屬性
		 * @param value 當前物件屬性值
		 * @return
		 * */
		@Override
		public boolean apply(Object obj, String key, Object value) {
			if(value != null){
				return true;
			}
			return false;
		}
	};

	/**
	 * 過濾欄位值等於null的欄位
	 * */
	private PropertyFilter nullFilter = (obj, key,value) -> {
		if (value != null){
			return true;
		}
		return false;
	};

	private String toJSON(Object obj){
		return  JSONObject.toJSONString(
				obj,
				SerializerFeature.WriteNullListAsEmpty,
				SerializerFeature.WriteNullStringAsEmpty,
				SerializerFeature.WriteDateUseDateFormat,
				SerializerFeature.WriteNullNumberAsZero,
				SerializerFeature.WriteNullBooleanAsFalse,
				SerializerFeature.DisableCircularReferenceDetect
		);
	}

	private String toJSON2(Object obj){
		return  JSONObject.toJSONString(
				obj,
				filter,
				SerializerFeature.WriteNullListAsEmpty,
				SerializerFeature.WriteNullStringAsEmpty,
				SerializerFeature.WriteDateUseDateFormat,
				SerializerFeature.WriteNullNumberAsZero,
				SerializerFeature.WriteNullBooleanAsFalse,
				SerializerFeature.DisableCircularReferenceDetect
		);
	}

	private String toJSON3(Object obj){
		return  JSONObject.toJSONString(
				obj,
				nullFilter,
				SerializerFeature.WriteNullListAsEmpty , // List欄位如果為null,輸出為[],而非null
				SerializerFeature.WriteNullStringAsEmpty, //字元型別欄位如果為null,輸出為”“,而非null
				SerializerFeature.WriteDateUseDateFormat, //如果時間是data、時間戳型別,按照這種格式初始化時間 "yyyy-MM-dd HH:mm"
				SerializerFeature.WriteNullNumberAsZero, //數值欄位如果為null,輸出為0,而非null
				SerializerFeature.WriteNullBooleanAsFalse ,//Boolean欄位如果為null,輸出為false,而非null
				SerializerFeature.SkipTransientField , //類中的Get方法對應的Field是transient,序列化時將會被忽略。預設為true
				SerializerFeature.DisableCircularReferenceDetect //消除對同一物件迴圈引用的問題,預設為false
		);
	}

}
/**
	 * 過濾欄位值等於null的欄位
	 * */
	private PropertyFilter nullFilter = (obj, key,value) -> {
		if (value != null){
			return true;
		}
		return false;
	};

注意點:

 1、這個方法用來過濾物件obj中為null的欄位,其中key是欄位名,value是欄位值;

 2、如果javaBean上的欄位用transient修飾,就會忽略掉這個欄位;

測試結果:

{"address":"beijing","age":0,"createDate":"2018-11-27 10:15:06","isMan":false,"name":""}
{"address":"beijing","createDate":"2018-11-27 10:15:06"}
{"address":"beijing","createDate":"2018-11-27 10:15:06"}