likes
comments
collection
share

FastJson Feature

作者站长头像
站长
· 阅读数 68

FastJson 在序列化和反序列化的过程中提供了很多特性,比如Feature.DisableFieldSmartMatch。如果没有选择该Feature,那么在反序列的过程中,FastJson 会自动把下划线命名的Json字符串转化到驼峰式命名的Java对象字段中。

public enum Feature {
    /**
     * 
     */
    AutoCloseSource,
    /**
     * 
     */
    AllowComment,
    /**
     * 
     */
    AllowUnQuotedFieldNames,
    /**
     * 
     */
    AllowSingleQuotes,
    /**
     * 
     */
    InternFieldNames,
    /**
     * 
     */
    AllowISO8601DateFormat,

    /**
     * {"a":1,,,"b":2}
     */
    AllowArbitraryCommas,

    /**
     * 
     */
    UseBigDecimal,
    
    /**
     * @since 1.1.2
     */
    IgnoreNotMatch,

    /**
     * @since 1.1.3
     */
    SortFeidFastMatch,
    
    /**
     * @since 1.1.3
     */
    DisableASM,
    
    /**
     * @since 1.1.7
     */
    DisableCircularReferenceDetect,
    
    /**
     * @since 1.1.10
     */
    InitStringFieldAsEmpty,
    
    /**
     * @since 1.1.35
     * 
     */
    SupportArrayToBean,
    
    /**
     * @since 1.2.3
     * 
     */
    OrderedField,
    
    /**
     * @since 1.2.5
     * 
     */
    DisableSpecialKeyDetect,
    
    /**
     * @since 1.2.9
     */
    UseObjectArray,

    /**
     * @since 1.2.22, 1.1.54.android
     */
    SupportNonPublicField,

    /**
     * @since 1.2.29
     *
     * disable autotype key '@type'
     */
    IgnoreAutoType,

    /**
     * @since 1.2.30
     *
     * disable field smart match, improve performance in some scenarios.
     */
    DisableFieldSmartMatch,

    /**
     * @since 1.2.41, backport to 1.1.66.android
     */
    SupportAutoType,

    /**
     * @since 1.2.42
     */
    NonStringKeyAsString,

    /**
     * @since 1.2.45
     */
    CustomMapDeserializer,

    /**
     * @since 1.2.55
     */
    ErrorOnEnumNotMatch
    ;

    Feature(){
        mask = (1 << ordinal());
    }

每一个Feature,都会被设置一个Mask: int mask = (1 << ordinal()); ordinal 是Feature在枚举中的位置

设置默认的features
static {
        int features = 0;
        features |= Feature.AutoCloseSource.getMask();
        features |= Feature.InternFieldNames.getMask();
        features |= Feature.UseBigDecimal.getMask();
        features |= Feature.AllowUnQuotedFieldNames.getMask();
        features |= Feature.AllowSingleQuotes.getMask();
        features |= Feature.AllowArbitraryCommas.getMask();
        features |= Feature.SortFeidFastMatch.getMask();
        features |= Feature.IgnoreNotMatch.getMask();
        DEFAULT_PARSER_FEATURE = features;
    }
判断feature 是否打开
return (features & feature.mask) != 0;
开启feature
features |= feature.mask;
关闭feature
features &= ~feature.mask;

结论

单个接口要提供灵活的功能,可以参考这种实现方式。