Configure serialization and deserialization with Features

December 25, 2025 ยท View on GitHub

1. Feature Introduction

In fastjson 2.x, there are two Features, which are used to configure the behavior of serialization and deserialization respectively.

  • JSONWriter.Feature Configure serialization behavior
  • JSONReader.Feature Configure deserialization behavior

2. Using Feature in JSON toJSONString and parseObject methods

2.1 Using JSONWriter.Feature in JSON toJSONString method

Bean bean = ...;
String json = JSON.toJSONString(bean, JSONWriter.Feature.WriteNulls); // Fields in the output object whose value is null

2.2 Using JSONReader.Feature in JSON parseObject method

String jsonStr = ...;
Bean bean = JSON.parseObject(jsonStr, Bean.class, JSONReader.Feature.UseBigDecimalForDoubles); // Read decimal values as BigDecimal

3. Configure Features on JSONField and JSONType

class Model {
    @JSONField(serializeFeatures = JSONWriter.Feature.BrowserCompatible)
    public long value;
}

You can also use the @JSONType annotation at the class level:

@JSONType(serializeFeatures = JSONWriter.Feature.WriteMapNullValue)
public class Model {
    public String name;
    public int age;
}

4. JSONReader.Feature

JSONReader.FeatureDescription
FieldBasedField-based deserialization. If not configured, it will be serialized based on public field and getter methods by default. After configuration, it will be deserialized based on non-static fields (including private). It will be safer under FieldBased configuration.
IgnoreNoneSerializableDeserialization ignores fields of non-Serializable types.
SupportArrayToBeanSupport mapping arrays to Bean objects.
InitStringFieldAsEmptyInitialize the String field to the empty string, e.g: "".
SupportAutoTypeAutomatic type is supported. To read JSON data with "@type" type information, you need to open SupportAutoType explicitly.
SupportSmartMatchThe default is camel case exact match, after opening this, it can intelligently identify the case in camel/upper/pascal/snake/Kebab.
UseNativeObjectThe default is to use JSONObject and JSONArray, and LinkedHashMap and ArrayList will be used after configuration.
SupportClassForNameTo support fields of type Class, use Class.forName. This is disabled by default for security.
IgnoreSetNullValueFields with null input are ignored.
UseDefaultConstructorAsPossibleUse the default constructor as much as possible, and use Unsafe.allocateInstance to implement this option when fieldBase is turned on and this option is not turned on.
UseBigDecimalForFloatsThe default configuration will use BigDecimal to parse decimals, and will use Float when turned on.
UseBigDecimalForDoublesThe default configuration will use BigDecimal to parse decimals, and Double will be used when turned on.
ErrorOnEnumNotMatchBy default, if the name of the Enum does not match, it will be ignored, and an exception will be thrown if it does not match when turned on.
TrimStringTrim the string values read.
ErrorOnNotSupportAutoTypeThrow an error when encountering AutoType (default is to ignore).
DuplicateKeyValueAsArrayDuplicate Key Values are not replaced but combined into an array.
AllowUnQuotedFieldNamesSupport field names without double quotes.
NonStringKeyAsStringTreat non-String keys as String.
Base64StringAsByteArrayDeserialize Base64 formatted strings as byte[].
DisableSingleQuoteDo not allow single quote in key name and values.

5. JSONWriter.Feature

JSONWriter.FeatureDescription
FieldBasedField-based serialization. If not configured, it will be serialized based on public field and getter methods by default. After configuration, it will be serialized based on non-static fields (including private).
IgnoreNoneSerializableSerialization ignores fields of non-Serializable types.
BeanToArraySequence the objects into an array format like [101,"XX"], which will be smaller.
WriteNullsSerialize and output null value fields.
BrowserCompatibleFor integers that exceed the range supported by JavaScript, output them in string format.
NullAsDefaultValueOutput null values as default values: Number types as 0, decimal Number types as 0.0, String type as "", Character type as \u0000, array and Collection types as [], and others as {}.
WriteBooleanAsNumberWrite true as 1 and false as 0.
WriteNonStringValueAsStringWrite values of non-String types as Strings, excluding objects and data types.
WriteClassNameWrite type information when serializing.
NotWriteRootClassNameWhen WriteClassName is turned on, the type information of the root object is not output.
NotWriteHashMapArrayListClassNameWhen WriteClassName is opened, the type information of objects of type HashMap/ArrayList is not output, and the deserialization combined with UseNativeObject can save the size of the serialized result.
NotWriteDefaultValueWhen the value of the field is the default value, it is not output, which can save the size of the serialized result.
WriteEnumsUsingNameSerialize enum using name.
WriteEnumUsingToStringSerialize enum using toString method.
IgnoreErrorGetterIgnore errors in getter methods.
PrettyFormatFormat the output.
ReferenceDetectionTurn on reference detection, which is turned off by default, which is inconsistent with fastjson 1.x.
WriteNameAsSymbolOutput field names as symbols, this only works under JSONB.
WriteBigDecimalAsPlainSerialize BigDecimal using toPlainString, avoiding scientific notation.
UseSingleQuotesUse single quotes.
MapSortFieldSort the KeyValue in Map by Key before output. This Feature is needed in some signature verification scenarios.
WriteNullListAsEmptySerialize null value fields of List type as empty array "[]".
WriteNullStringAsEmptySerialize null value fields of String type as empty string "".
WriteNullNumberAsZeroSerialize null value fields of Number type as 0.
WriteNullBooleanAsFalseSerialize null value fields of Boolean type as false.
NotWriteEmptyArrayDo not output array type fields when length is 0.
WriteNonStringKeyAsStringTreat non-String keys in Map as String type for output.
ErrorOnNoneSerializableThrow an error when serializing non-Serializable objects.
WritePairAsJavaBeanSerialize Pair objects from Apache Commons as JavaBean.
BrowserSecureBrowser security, will escape '<' '>' '(' ')' characters for output.
WriteLongAsStringSerialize Long as String.
WriteEnumUsingOrdinalSerialize Enum using Ordinal, the default is name.
WriteThrowableClassNameInclude type information when serializing Throwable.
LargeObjectThis is a protection measure to prevent serialization of circular reference objects from consuming excessive resources.
UnquoteFieldNameOutput Key without quotes.
NotWriteSetClassNameWhen WriteClassName is turned on and you don't want to output the type information of Set, use this Feature.
NotWriteNumberClassNameWhen WriteClassName is turned on and you don't want to output the type information of Number, such as the suffixes L/S/B/F/D, use this Feature.
WriteFloatSpecialAsStringWhen enabled, NaN/Infinity will be serialized as "NaN", "Infinity", "-Infinity".

6. Usage Examples

6.1 Serialization Examples

// Basic usage
User user = new User("John", 25, null);
String json = JSON.toJSONString(user, JSONWriter.Feature.WriteNulls);

// Combining multiple Features
String json2 = JSON.toJSONString(user, 
    JSONWriter.Feature.WriteNulls, 
    JSONWriter.Feature.PrettyFormat);

// Using BeanToArray feature
String json3 = JSON.toJSONString(user, JSONWriter.Feature.BeanToArray);

6.2 Deserialization Examples

// Basic usage
String json = "{\"name\":\"John\",\"age\":25}";
User user = JSON.parseObject(json, User.class, JSONReader.Feature.SupportSmartMatch);

// Combining multiple Features
User user2 = JSON.parseObject(json, User.class, 
    JSONReader.Feature.SupportSmartMatch, 
    JSONReader.Feature.InitStringFieldAsEmpty);

7. 1.x Feature Migration Guide

7.1 Features Enabled by Default

In fastjson 1.x, the default enabled features are as follows:

Serialization

  • SerializerFeature.QuoteFieldNames
  • SerializerFeature.SkipTransientField
  • SerializerFeature.WriteEnumUsingName
  • SerializerFeature.SortField

Deserialization

  • Feature.AutoCloseSource
  • Feature.InternFieldNames
  • Feature.UseBigDecimal
  • Feature.AllowUnQuotedFieldNames
  • Feature.AllowSingleQuotes
  • Feature.AllowArbitraryCommas
  • Feature.SortFeidFastMatch
  • Feature.IgnoreNotMatch

In fastjson 2.x, all features are OFF by default.

7.2 Changes from 1.x to 2.x

Serialization:

  • QuoteFieldNames: Enabled by default in 2.x; no configuration required. And 2.x supports the UnquoteFieldName feature.
  • UseISO8601DateFormat: Replaced in 2.x by:
    • JSON.configWriterDateFormat("iso8601") (global)
    • JSON.toJSONString(bean, "iso8601")
    • @JSONField(format = "iso8601")
  • SkipTransientField: Enabled by default in 2.x. To disable:
    • JVM system property -Dfastjson2.writer.skipTransient=false
    • JSONFactory.setDefaultSkipTransient(false)
    • @JSONType(skipTransient = false)
    • @JSONField(skipTransient = false)
  • SortField: Enabled by default in 2.x; no configuration required.
  • WriteDateUseDateFormat: Alternatives in 2.x:
    • JSON.toJSONString(bean, "millis")
    • JSONWriter.Feature.WriterUtilDateAsMillis (since 2.0.58)
  • DisableCircularReferenceDetect: 1.x detects circular refs by default; 2.x does not. To turn detection on in 2.x use ReferenceDetection.
  • WriteEnumUsingName: Disabled by default in 2.x.
  • WriteSlashAsSpecial: Not supported in 2.x.
  • WriteTabAsSpecial & DisableCheckSpecialChar: Already deprecated in 1.x; removed in 2.x.
  • All other 1.x serialization features remain unchanged.

Deserialization:

  • AllowArbitraryCommas: 2.x uses strict syntax; multiple commas are not allowed.
  • AllowComment: Enabled by default in 2.x; no configuration required.
  • AllowISO8601DateFormat: Enabled by default in 2.x. Explicit settings:
    • JSON.configReaderDateFormat("iso8601")
    • JSON.parseObject(str, Bean.class, "iso8601")
  • AllowSingleQuotes: Enabled by default in 2.x; no configuration required.
  • AutoCloseSource (throw on incomplete JSON): Enabled by default in 2.x; no configuration required.
  • CustomMapDeserializer: Not supported in 2.x.
  • DisableCircularReferenceDetect: Renamed to DisableReferenceDetect.
  • DisableFieldSmartMatch: Replaced by SupportSmartMatch. (Smart matching was ON in 1.x, OFF by default in 2.x.)
  • DisableSpecialKeyDetect: Enabled by default in 2.x; no configuration required.
  • IgnoreAutoType: 2.x disables AutoType by default; safe out-of-the-box.
  • IgnoreNotMatch: Enabled by default in 2.x; no configuration required.
  • OrderedField (keep declaration order for JSONObject / JSONArray): Enabled by default in 2.x; no configuration required.
  • SupportNonPublicField: Use FieldBased in 2.x.
  • SafeMode: 2.x: JVM system property -Dfastjson2.parser.safeMode=true.
  • TrimStringFieldValue: Use JSONReader.Feature.TrimString in 2.x.
  • UseBigDecimal: Split into UseBigDecimalForFloats and UseBigDecimalForDoubles in 2.x.
  • UseNativeJavaObject: Renamed to UseNativeObject.
  • UseObjectArray (parse JSON arrays as Object[] instead of ArrayList): Not supported in 2.x.
  • All other 1.x deserialization features remain unchanged.

8. Best Practices

  1. Security Considerations:

    • Do not enable SupportAutoType by default unless you really need to process JSON with type information
    • Be cautious when using the FieldBased feature with untrusted data sources
  2. Performance Optimization:

    • For large data serialization, consider using the BeanToArray feature to reduce JSON size
    • Use the MapSortField feature when field order needs to be maintained
  3. Compatibility Handling:

    • Use BrowserCompatible for web front-end scenarios to handle large integers
    • Enable ErrorOnEnumNotMatch in scenarios requiring strict data format to quickly identify enum mismatches
  4. Null Value Handling:

    • Choose appropriate null value handling strategies based on business requirements, such as WriteNulls, WriteNullStringAsEmpty, etc.