fastjson的SerializerFeature

0X00

先來一下SerializerFeature的源碼吧。

/*
 * Copyright 1999-2101 Alibaba Group.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.fastjson.serializer;


/**
 * @author wenshao<[email protected]>
 */
public enum SerializerFeature {
    QuoteFieldNames,//輸出key時是否使用雙引號,默認爲true 
    /**
     * 
     */
    UseSingleQuotes,//使用單引號而不是雙引號,默認爲false
    /**
     * 
     */
    WriteMapNullValue,//是否輸出值爲null的字段,默認爲false 
    /**
     * 
     */
    WriteEnumUsingToString,//Enum輸出name()或者original,默認爲false
    /**
     * 
     */
    UseISO8601DateFormat,//Date使用ISO8601格式輸出,默認爲false
    /**
     * @since 1.1
     */
    WriteNullListAsEmpty,//List字段如果爲null,輸出爲[],而非null 
    /**
     * @since 1.1
     */
    WriteNullStringAsEmpty,//字符類型字段如果爲null,輸出爲"",而非null 
    /**
     * @since 1.1
     */
    WriteNullNumberAsZero,//數值字段如果爲null,輸出爲0,而非null 
    /**
     * @since 1.1
     */
    WriteNullBooleanAsFalse,//Boolean字段如果爲null,輸出爲false,而非null
    /**
     * @since 1.1
     */
    SkipTransientField,//如果是true,類中的Get方法對應的Field是transient,序列化時將會被忽略。默認爲true
    /**
     * @since 1.1
     */
    SortField,//按字段名稱排序後輸出。默認爲false
    /**
     * @since 1.1.1
     */
    @Deprecated
    WriteTabAsSpecial,//把\t做轉義輸出,默認爲false
    /**
     * @since 1.1.2
     */
    PrettyFormat,//結果是否格式化,默認爲false
    /**
     * @since 1.1.2
     */
    WriteClassName,//序列化時寫入類型信息,默認爲false。反序列化是需用到

    /**
     * @since 1.1.6
     */
    DisableCircularReferenceDetect,//消除對同一對象循環引用的問題,默認爲false

    /**
     * @since 1.1.9
     */
    WriteSlashAsSpecial,//對斜槓'/'進行轉義

    /**
     * @since 1.1.10
     */
    BrowserCompatible,//將中文都會序列化爲\uXXXX格式,字節數會多一些,但是能兼容IE 6,默認爲false

    /**
     * @since 1.1.14
     */
    WriteDateUseDateFormat,//全局修改日期格式,默認爲false。JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);

    /**
     * @since 1.1.15
     */
    NotWriteRootClassName,//暫不知,求告知

    /**
     * @since 1.1.19
     */
    DisableCheckSpecialChar,//一個對象的字符串屬性中如果有特殊字符如雙引號,將會在轉成json時帶有反斜槓轉移符。如果不需要轉義,可以使用這個屬性。默認爲false 

    /**
     * @since 1.1.35
     */
    BeanToArray //暫不知,求告知
    ;

    private SerializerFeature(){
        mask = (1 << ordinal());
    }

    private final int mask;

    public final int getMask() {
        return mask;
    }

    public static boolean isEnabled(int features, SerializerFeature feature) {
        return (features & feature.getMask()) != 0;
    }

    public static int config(int features, SerializerFeature feature, boolean state) {
        if (state) {
            features |= feature.getMask();
        } else {
            features &= ~feature.getMask();
        }

        return features;
    }
}

0X01

大致用法

先來張圖(圖是盜的,鏈接在參考3):

圖

簡單的代碼示例

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

private static SerializerFeature[] features = {
            SerializerFeature.WriteNullNumberAsZero,
            SerializerFeature.WriteNullStringAsEmpty,
            SerializerFeature.DisableCircularReferenceDetect };

JSONObject result = new JSONObject(); 
result.put("errorCode", 1);
result.put("errorMessage", cause);//Map<String, String> cause
JSON.toJSONString(result, features);

參考

  1. fastjson的SerializerFeature用法

  2. SerialFeature

  3. FastJSON 簡單使用

  4. fastjson常見問題

  5. fastjson中SerializerFeature解析

  6. fastjson wiki

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