Enum JSONWriter.Feature

java.lang.Object
java.lang.Enum<JSONWriter.Feature>
com.alibaba.fastjson2.JSONWriter.Feature
All Implemented Interfaces:
Serializable, Comparable<JSONWriter.Feature>, java.lang.constant.Constable
Enclosing class:
JSONWriter

public static enum JSONWriter.Feature extends Enum<JSONWriter.Feature>
Feature is used to control the behavior of JSON writing and serialization in FASTJSON2. Each feature represents a specific configuration option that can be enabled or disabled to customize how Java objects are serialized to JSON format.

Features can be enabled in several ways:

Example usage:

 // Enable PrettyFormat feature for this writer only
 try (JSONWriter writer = JSONWriter.of(JSONWriter.Feature.PrettyFormat)) {
     writer.writeAny(object);
     String json = writer.toString();
 }

 // Enable multiple features
 try (JSONWriter writer = JSONWriter.of(
         JSONWriter.Feature.PrettyFormat,
         JSONWriter.Feature.WriteMapNullValue)) {
     writer.writeAny(object);
     String json = writer.toString();
 }

 // Using context configuration
 JSONWriter.Context context = new JSONWriter.Context();
 context.config(JSONWriter.Feature.PrettyFormat);
 try (JSONWriter writer = JSONWriter.of(context)) {
     writer.writeAny(object);
     String json = writer.toString();
 }
 

Features are implemented as bitmask flags for efficient storage and checking. Each feature has a unique mask value that is used internally to determine whether the feature is enabled in a given configuration.

Since:
2.0.0
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from class java.lang.Enum

    Enum.EnumDesc<E extends Enum<E>>
  • Enum Constant Summary

    Enum Constants
    Enum Constant
    Description
    Feature that determines whether to serialize Java beans as JSON arrays instead of JSON objects.
    Feature that enables browser-compatible JSON output.
    Feature that enables browser security measures during serialization.
    Feature that determines whether to throw an exception when encountering non-serializable classes during serialization.
    Feature that specifies that all characters beyond 7-bit ASCII range (i.e. code points of 128 and above) need to be output using format-specific escapes (for JSON, backslash escapes), if format uses escaping mechanisms (which is generally true for textual formats but not for binary formats).
    Feature that determines whether to use field-based serialization instead of getter-based serialization.
    Feature that determines whether to ignore empty values during serialization.
    Feature that determines whether to ignore errors when calling getter methods during serialization.
    Feature that determines whether to ignore non-serializable classes during serialization.
    Feature that determines whether to ignore non-field getter methods during serialization.
    Feature that enables support for large objects during serialization.
    Deprecated.
    Feature that determines whether to write default values during serialization.
    Deprecated.
    use IgnoreEmpty
    Feature that determines whether to write class names for HashMap and ArrayList during serialization.
    Feature that determines whether to write class names for Number objects during serialization.
    Feature that determines whether to write the root class name during serialization.
    Feature that determines whether to write class names for Set collections during serialization.
    Feature that determines whether to write default values instead of null values during serialization.
    Feature that enables optimization for ASCII characters during serialization.
    Feature that enables pretty-printed JSON output with formatting and indentation.
    JSON formatting support using 2 spaces for indentation.
    JSON formatting support using 4 spaces for indentation.
    Feature that enables reference detection during serialization.
    The serialized Map will first be sorted according to Key, and is used in some scenarios where serialized content needs to be signed.
    Feature that determines whether to write field names without quotes during serialization.
    Feature that determines whether to use single quotes instead of double quotes for strings.
    Feature that determines whether to write BigDecimal values in plain format during serialization.
    Feature that determines whether to write boolean values as numbers during serialization.
    Feature that determines whether to write byte arrays as Base64-encoded strings during serialization.
    Feature that determines whether to write class names during serialization.
    Feature that determines whether to write enum values using their name during serialization.
    Feature that determines whether to write enum values using their ordinal value during serialization.
    Feature that determines whether to write enum values using their toString() representation during serialization.
    Feature that determines whether to write long values as strings during serialization.
    Feature that determines whether to write null values for map entries during serialization.
    Feature that determines whether to write field names as symbols during serialization.
    Feature that determines whether to write non-string keys as strings during serialization.
    Feature that determines whether to write non-string values as strings during serialization.
    Feature that determines whether to write null booleans as false during serialization.
    Feature that determines whether to write null lists as empty arrays during serialization.
    Feature that determines whether to write null numbers as zero during serialization.
    Feature that determines whether to write null values during serialization.
    Feature that determines whether to write null strings as empty strings during serialization.
    Feature that determines whether to write key-value pairs as Java beans during serialization.
    Feature that determines whether to write java.util.Date objects as milliseconds since epoch.
    Feature that determines whether to write the class name of Throwable objects during serialization.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final long
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    isEnabled(long features)
    Checks if this feature is enabled in the specified features bitmask.
    Returns the enum constant of this type with the specified name.
    Returns an array containing the constants of this enum type, in the order they are declared.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Enum Constant Details

    • FieldBased

      public static final JSONWriter.Feature FieldBased
      Feature that determines whether to use field-based serialization instead of getter-based serialization. When enabled, fields are directly accessed rather than using getter methods. This can improve performance but may bypass validation logic in getters.

      By default, this feature is disabled, meaning that getter-based serialization is used.

      Since:
      2.0.0
    • IgnoreNoneSerializable

      public static final JSONWriter.Feature IgnoreNoneSerializable
      Feature that determines whether to ignore non-serializable classes during serialization. When enabled, classes that do not implement Serializable will be ignored rather than causing an exception to be thrown.

      By default, this feature is disabled, meaning that non-serializable classes are not ignored.

      Since:
      2.0.0
    • ErrorOnNoneSerializable

      public static final JSONWriter.Feature ErrorOnNoneSerializable
      Feature that determines whether to throw an exception when encountering non-serializable classes during serialization. When enabled, an exception will be thrown if a class does not implement Serializable.

      By default, this feature is disabled, meaning that no exception is thrown for non-serializable classes.

      Since:
      2.0.0
    • BeanToArray

      public static final JSONWriter.Feature BeanToArray
      Feature that determines whether to serialize Java beans as JSON arrays instead of JSON objects. When enabled, bean properties will be serialized as array elements in the order they are defined, rather than as key-value pairs in an object.

      By default, this feature is disabled, meaning that beans are serialized as JSON objects.

      Since:
      2.0.0
    • WriteNulls

      public static final JSONWriter.Feature WriteNulls
      Feature that determines whether to write null values during serialization. When enabled, null values will be included in the output JSON.

      By default, this feature is disabled, meaning that null values are omitted from the output.

      Since:
      2.0.0
    • WriteMapNullValue

      public static final JSONWriter.Feature WriteMapNullValue
      Feature that determines whether to write null values for map entries during serialization. When enabled, null values in maps will be included in the output JSON.

      By default, this feature is disabled, meaning that null map values are omitted from the output.

      Since:
      2.0.0
    • BrowserCompatible

      public static final JSONWriter.Feature BrowserCompatible
      Feature that enables browser-compatible JSON output. When enabled, the output will be formatted to be compatible with browser JavaScript engines.

      By default, this feature is disabled.

      Since:
      2.0.0
    • NullAsDefaultValue

      public static final JSONWriter.Feature NullAsDefaultValue
      Feature that determines whether to write default values instead of null values during serialization. When enabled, default values (0 for numbers, false for booleans, empty string for strings) will be written instead of null values.

      By default, this feature is disabled, meaning that null values are handled according to other features.

      Since:
      2.0.0
    • WriteBooleanAsNumber

      public static final JSONWriter.Feature WriteBooleanAsNumber
      Feature that determines whether to write boolean values as numbers during serialization. When enabled, boolean values will be serialized as 1 (for true) and 0 (for false) instead of true and false literals.

      By default, this feature is disabled, meaning that boolean values are written as true/false.

      Since:
      2.0.0
    • WriteNonStringValueAsString

      public static final JSONWriter.Feature WriteNonStringValueAsString
      Feature that determines whether to write non-string values as strings during serialization. When enabled, numeric and other non-string values will be converted to their string representation.

      By default, this feature is disabled, meaning that values are written in their native JSON types.

      Since:
      2.0.0
    • WriteClassName

      public static final JSONWriter.Feature WriteClassName
      Feature that determines whether to write class names during serialization. When enabled, class names will be included in the output JSON, typically using a special "@type" field.

      By default, this feature is disabled, meaning that class names are not included in the output.

      Since:
      2.0.0
    • NotWriteRootClassName

      public static final JSONWriter.Feature NotWriteRootClassName
      Feature that determines whether to write the root class name during serialization. When enabled, the class name of the root object will be included in the output JSON.

      By default, this feature is disabled.

      Since:
      2.0.0
    • NotWriteHashMapArrayListClassName

      public static final JSONWriter.Feature NotWriteHashMapArrayListClassName
      Feature that determines whether to write class names for HashMap and ArrayList during serialization. When enabled, class names for these common collection types will be omitted from the output.

      By default, this feature is disabled.

      Since:
      2.0.0
    • NotWriteDefaultValue

      public static final JSONWriter.Feature NotWriteDefaultValue
      Feature that determines whether to write default values during serialization. When enabled, fields with default values (0 for numbers, false for booleans, etc.) will be omitted.

      By default, this feature is disabled, meaning that all field values are written regardless of whether they are default values.

      Since:
      2.0.0
    • WriteEnumsUsingName

      public static final JSONWriter.Feature WriteEnumsUsingName
      Feature that determines whether to write enum values using their name during serialization. When enabled, enum values will be serialized as their name string rather than their ordinal value.

      By default, this feature is disabled.

      Since:
      2.0.0
    • WriteEnumUsingToString

      public static final JSONWriter.Feature WriteEnumUsingToString
      Feature that determines whether to write enum values using their toString() representation during serialization. When enabled, enum values will be serialized using their toString() method rather than their name or ordinal.

      By default, this feature is disabled.

      Since:
      2.0.0
    • IgnoreErrorGetter

      public static final JSONWriter.Feature IgnoreErrorGetter
      Feature that determines whether to ignore errors when calling getter methods during serialization. When enabled, exceptions thrown by getter methods will be ignored rather than propagated.

      By default, this feature is disabled, meaning that getter method exceptions are propagated.

      Since:
      2.0.0
    • PrettyFormat

      public static final JSONWriter.Feature PrettyFormat
      Feature that enables pretty-printed JSON output with formatting and indentation. When enabled, the output JSON will be formatted with line breaks and indentation for readability.

      By default, this feature is disabled, meaning that JSON is output in compact form.

      Since:
      2.0.0
    • ReferenceDetection

      public static final JSONWriter.Feature ReferenceDetection
      Feature that enables reference detection during serialization. When enabled, circular references and repeated objects will be detected and handled using reference markers to avoid infinite loops and duplicate serialization.

      By default, this feature is disabled.

      Since:
      2.0.0
    • WriteNameAsSymbol

      public static final JSONWriter.Feature WriteNameAsSymbol
      Feature that determines whether to write field names as symbols during serialization. When enabled, field names will be written as symbol references rather than string literals.

      By default, this feature is disabled.

      Since:
      2.0.0
    • WriteBigDecimalAsPlain

      public static final JSONWriter.Feature WriteBigDecimalAsPlain
      Feature that determines whether to write BigDecimal values in plain format during serialization. When enabled, BigDecimal values will be written without exponential notation when possible.

      By default, this feature is disabled.

      Since:
      2.0.0
    • UseSingleQuotes

      public static final JSONWriter.Feature UseSingleQuotes
      Feature that determines whether to use single quotes instead of double quotes for strings. When enabled, string values will be enclosed in single quotes rather than double quotes.

      By default, this feature is disabled, meaning that double quotes are used.

      Since:
      2.0.0
    • MapSortField

      public static final JSONWriter.Feature MapSortField
      Deprecated.
      The serialized Map will first be sorted according to Key, and is used in some scenarios where serialized content needs to be signed. SortedMap and derived classes do not need to do this. This Feature does not work for LinkedHashMap.
      Since:
      2.0.0
    • WriteNullListAsEmpty

      public static final JSONWriter.Feature WriteNullListAsEmpty
      Feature that determines whether to write null lists as empty arrays during serialization. When enabled, null collection values will be serialized as empty JSON arrays ([]) rather than null.

      By default, this feature is disabled.

      Since:
      2.0.0
    • WriteNullStringAsEmpty

      public static final JSONWriter.Feature WriteNullStringAsEmpty
      Feature that determines whether to write null strings as empty strings during serialization. When enabled, null string values will be serialized as empty strings ("") rather than null.

      By default, this feature is disabled.

      Since:
      1.1
    • WriteNullNumberAsZero

      public static final JSONWriter.Feature WriteNullNumberAsZero
      Feature that determines whether to write null numbers as zero during serialization. When enabled, null numeric values will be serialized as 0 rather than null.

      By default, this feature is disabled.

      Since:
      1.1
    • WriteNullBooleanAsFalse

      public static final JSONWriter.Feature WriteNullBooleanAsFalse
      Feature that determines whether to write null booleans as false during serialization. When enabled, null boolean values will be serialized as false rather than null.

      By default, this feature is disabled.

      Since:
      1.1
    • NotWriteEmptyArray

      public static final JSONWriter.Feature NotWriteEmptyArray
      Deprecated.
      use IgnoreEmpty
      Feature that determines whether to avoid writing empty arrays during serialization. When enabled, empty arrays will be omitted from the output rather than written as [].

      By default, this feature is disabled.

      Since:
      2.0.7
    • IgnoreEmpty

      public static final JSONWriter.Feature IgnoreEmpty
      Feature that determines whether to ignore empty values during serialization. When enabled, empty collections, empty strings, and other empty values will be omitted from the output.

      By default, this feature is disabled.

      Since:
      2.0.51
    • WriteNonStringKeyAsString

      public static final JSONWriter.Feature WriteNonStringKeyAsString
      Feature that determines whether to write non-string keys as strings during serialization. When enabled, map keys that are not strings will be converted to their string representation.

      By default, this feature is disabled.

      Since:
      2.0.0
    • WritePairAsJavaBean

      public static final JSONWriter.Feature WritePairAsJavaBean
      Feature that determines whether to write key-value pairs as Java beans during serialization. When enabled, key-value pairs will be serialized using Java bean conventions.

      By default, this feature is disabled.

      Since:
      2.0.11
    • OptimizedForAscii

      public static final JSONWriter.Feature OptimizedForAscii
      Feature that enables optimization for ASCII characters during serialization. When enabled, the serializer will use optimized paths for ASCII-only content.

      By default, this feature is disabled.

      Since:
      2.0.12
    • EscapeNoneAscii

      public static final JSONWriter.Feature EscapeNoneAscii
      Feature that specifies that all characters beyond 7-bit ASCII range (i.e. code points of 128 and above) need to be output using format-specific escapes (for JSON, backslash escapes), if format uses escaping mechanisms (which is generally true for textual formats but not for binary formats). Feature is disabled by default.
      Since:
      2.0.12
    • WriteByteArrayAsBase64

      public static final JSONWriter.Feature WriteByteArrayAsBase64
      Feature that determines whether to write byte arrays as Base64-encoded strings during serialization. When enabled, byte array values will be serialized as Base64-encoded strings rather than arrays of numbers.

      By default, this feature is disabled.

      Since:
      2.0.13
    • IgnoreNonFieldGetter

      public static final JSONWriter.Feature IgnoreNonFieldGetter
      Feature that determines whether to ignore non-field getter methods during serialization. When enabled, only getter methods that correspond to actual fields will be considered.

      By default, this feature is disabled.

      Since:
      2.0.13
    • LargeObject

      public static final JSONWriter.Feature LargeObject
      Feature that enables support for large objects during serialization. When enabled, the serializer will use configurations appropriate for very large object graphs.

      By default, this feature is disabled.

      Since:
      2.0.16
    • WriteLongAsString

      public static final JSONWriter.Feature WriteLongAsString
      Feature that determines whether to write long values as strings during serialization. When enabled, long numeric values will be serialized as strings rather than numbers to avoid precision loss in JavaScript and other environments with limited integer precision.

      By default, this feature is disabled.

      Since:
      2.0.17
    • BrowserSecure

      public static final JSONWriter.Feature BrowserSecure
      Feature that enables browser security measures during serialization. When enabled, the output will be formatted to be secure when used in browser environments.

      By default, this feature is disabled.

      Since:
      2.0.20
    • WriteEnumUsingOrdinal

      public static final JSONWriter.Feature WriteEnumUsingOrdinal
      Feature that determines whether to write enum values using their ordinal value during serialization. When enabled, enum values will be serialized as their ordinal (position) rather than their name.

      By default, this feature is disabled.

      Since:
      2.0.20
    • WriteThrowableClassName

      public static final JSONWriter.Feature WriteThrowableClassName
      Feature that determines whether to write the class name of Throwable objects during serialization. When enabled, the class name of exception and error objects will be included in the output.

      By default, this feature is disabled.

      Since:
      2.0.30
    • UnquoteFieldName

      public static final JSONWriter.Feature UnquoteFieldName
      Feature that determines whether to write field names without quotes during serialization. When enabled, field names in JSON objects will not be enclosed in quotes.

      By default, this feature is disabled, meaning that field names are quoted.

      Since:
      2.0.33
    • NotWriteSetClassName

      public static final JSONWriter.Feature NotWriteSetClassName
      Feature that determines whether to write class names for Set collections during serialization. When enabled, class names for Set collections will be omitted from the output.

      By default, this feature is disabled.

      Since:
      2.0.34
    • NotWriteNumberClassName

      public static final JSONWriter.Feature NotWriteNumberClassName
      Feature that determines whether to write class names for Number objects during serialization. When enabled, class names for Number objects will be omitted from the output.

      By default, this feature is disabled.

      Since:
      2.0.34
    • SortMapEntriesByKeys

      public static final JSONWriter.Feature SortMapEntriesByKeys
      The serialized Map will first be sorted according to Key, and is used in some scenarios where serialized content needs to be signed. SortedMap and derived classes do not need to do this.
      Since:
      2.0.48
    • PrettyFormatWith2Space

      public static final JSONWriter.Feature PrettyFormatWith2Space
      JSON formatting support using 2 spaces for indentation. When enabled, pretty-printed JSON will use 2 spaces for each indentation level.

      This feature requires PrettyFormat to also be enabled.

      Since:
      2.0.54
    • PrettyFormatWith4Space

      public static final JSONWriter.Feature PrettyFormatWith4Space
      JSON formatting support using 4 spaces for indentation. When enabled, pretty-printed JSON will use 4 spaces for each indentation level.

      This feature requires PrettyFormat to also be enabled.

      Since:
      2.0.54
    • WriterUtilDateAsMillis

      public static final JSONWriter.Feature WriterUtilDateAsMillis
      Feature that determines whether to write java.util.Date objects as milliseconds since epoch. When enabled, Date objects will be serialized as numeric timestamps rather than formatted strings.

      By default, this feature is disabled.

      Since:
      2.0.0
  • Field Details

    • mask

      public final long mask
  • Method Details

    • values

      public static JSONWriter.Feature[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      Returns:
      an array containing the constants of this enum type, in the order they are declared
    • valueOf

      public static JSONWriter.Feature valueOf(String name)
      Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum type has no constant with the specified name
      NullPointerException - if the argument is null
    • isEnabled

      public boolean isEnabled(long features)
      Checks if this feature is enabled in the specified features bitmask.
      Parameters:
      features - the features bitmask to check
      Returns:
      true if this feature is enabled, false otherwise