json測試類

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

//state:已閱
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("北京");
        user1.setDate(new Date());
        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);
        Date date3 =new Date();
        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);
        map.put("date", date3);
        word.setMap(map);
    }

    public static void main(String[] args) {
        init();
        //UseSingleQuotes:使用單引號而不是雙引號,默認爲false
//        useSingleQuotes();
        //WriteMapNullValue:是否輸出值爲null的字段,默認爲false,注意:是字段,本文發現list集合中的空會輸出,map集合中的空不會輸出,對象的屬性空不會輸出,當設置WriteMapNullValue後,前面所說的所有空都會輸出
//        writeMapNullValue();
        //設置時間格式,輸出來看下就可以知道,使用ISO8601格式輸出
//        useISO8601DateFormat();
        //如果List字段如果爲null,輸出爲[],而非null,需要配合WriteMapNullValue使用,現將null輸出
//        writeNullListAsEmpty();
        //WriteNullStringAsEmpty:字符類型字段如果爲null,輸出爲"",而非null,需要配合WriteMapNullValue使用,若list集合map集合等爲空會輸出null而不是""
//        writeNullStringAsEmpty();
        //按字段名稱排序輸出,具體介紹點開此方法下面有
//        sortField();
        //輸出的格式的轉變,輸出看一下就知道,由原來的字符串變爲了鍵值對
//        prettyFormat();
        //全局修改日期格式,修改爲自定義的格式,對象中的字段日期格式還是list集合中的日期格式或者map集合中的日期格式都會修改
        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("設置WriteDateUseDateFormat後:");
        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));
        System.out.println("設置WriteDateUseDateFormat後:");
        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("設置PrettyFormat後:");
        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("設置SortField後:");
        System.out.println(JSON.toJSONString(word, SerializerFeature.SortField));
    }

    /**
     *  5:
     *  WriteNullStringAsEmpty:字符類型字段如果爲null,輸出爲"",而非null
     *  需要配合WriteMapNullValue使用,現將null輸出
     */
    private static void writeNullStringAsEmpty() {
        word.setE(null);
        word.setMap(null);
        word.setList(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));
    }
}
 

兩個實體類爲:

import java.util.Date;
//state:已閱
public class User {

    private int id;
    private String name;
    private String add;
    private String old;
    private Date date;

}

import java.util.Date;
import java.util.List;
import java.util.Map;
//state:已閱
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;
 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章