FastJson 在序列化和反序列化的過程當中提供了不少特性,好比Feature.DisableFieldSmartMatch。若是沒有選擇該Feature,那麼在反序列的過程當中,FastJson 會自動把下劃線命名的Json字符串轉化到駝峯式命名的Java對象字段中。android
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在枚舉中的位置ios
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; }
return (features & feature.mask) != 0;
features |= feature.mask;
features &= ~feature.mask;
單個接口要提供靈活的功能,能夠參考這種實現方式。code