public static enum JSONWriter.Feature extends java.lang.Enum<JSONWriter.Feature>
Features can be enabled in several ways:
JSONWriter.of(Feature...)JSONWriter.config(Feature...) methodJSONFactory.getDefaultWriterFeatures() for global configurationExample 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.
JSONWriter.Context,
JSONFactory| Enum Constant and Description |
|---|
BeanToArray
Feature that determines whether to serialize Java beans as JSON arrays instead of JSON objects.
|
BrowserCompatible
Feature that enables browser-compatible JSON output.
|
BrowserSecure
Feature that enables browser security measures during serialization.
|
ErrorOnNoneSerializable
Feature that determines whether to throw an exception when encountering non-serializable classes
during serialization.
|
EscapeNoneAscii
Feature that specifies that all characters beyond 7-bit ASCII range (i.e.
|
FieldBased
Feature that determines whether to use field-based serialization instead of getter-based serialization.
|
IgnoreEmpty
Feature that determines whether to ignore empty values during serialization.
|
IgnoreErrorGetter
Feature that determines whether to ignore errors when calling getter methods during serialization.
|
IgnoreNoneSerializable
Feature that determines whether to ignore non-serializable classes during serialization.
|
IgnoreNonFieldGetter
Feature that determines whether to ignore non-field getter methods during serialization.
|
LargeObject
Feature that enables support for large objects during serialization.
|
MapSortField
Deprecated.
Use
SortMapEntriesByKeys instead. |
NotWriteDefaultValue
Feature that determines whether to write default values during serialization.
|
NotWriteEmptyArray
Deprecated.
use IgnoreEmpty
|
NotWriteHashMapArrayListClassName
Feature that determines whether to write class names for HashMap and ArrayList during serialization.
|
NotWriteNumberClassName
Feature that determines whether to write class names for Number objects during serialization.
|
NotWriteRootClassName
Feature that determines whether to write the root class name during serialization.
|
NotWriteSetClassName
Feature that determines whether to write class names for Set collections during serialization.
|
NullAsDefaultValue
Feature that determines whether to write default values instead of null values during serialization.
|
OptimizedForAscii
Feature that enables optimization for ASCII characters during serialization.
|
PrettyFormat
Feature that enables pretty-printed JSON output with formatting and indentation.
|
PrettyFormatWith2Space
JSON formatting support using 2 spaces for indentation.
|
PrettyFormatWith4Space
JSON formatting support using 4 spaces for indentation.
|
ReferenceDetection
Feature that enables reference detection during serialization.
|
SortMapEntriesByKeys
The serialized Map will first be sorted according to Key,
and is used in some scenarios where serialized content needs to be signed.
|
UnquoteFieldName
Feature that determines whether to write field names without quotes during serialization.
|
UseSingleQuotes
Feature that determines whether to use single quotes instead of double quotes for strings.
|
WriteBigDecimalAsPlain
Feature that determines whether to write BigDecimal values in plain format during serialization.
|
WriteBooleanAsNumber
Feature that determines whether to write boolean values as numbers during serialization.
|
WriteByteArrayAsBase64
Feature that determines whether to write byte arrays as Base64-encoded strings during serialization.
|
WriteClassName
Feature that determines whether to write class names during serialization.
|
WriteEnumsUsingName
Feature that determines whether to write enum values using their name during serialization.
|
WriteEnumUsingOrdinal
Feature that determines whether to write enum values using their ordinal value during serialization.
|
WriteEnumUsingToString
Feature that determines whether to write enum values using their toString() representation during serialization.
|
WriteFloatSpecialAsString
Feature that determines whether to write float/double NaN and Infinite values as Strings.
|
WriteLongAsString
Feature that determines whether to write long values as strings during serialization.
|
WriteMapNullValue
Feature that determines whether to write null values for map entries during serialization.
|
WriteNameAsSymbol
Feature that determines whether to write field names as symbols during serialization.
|
WriteNonStringKeyAsString
Feature that determines whether to write non-string keys as strings during serialization.
|
WriteNonStringValueAsString
Feature that determines whether to write non-string values as strings during serialization.
|
WriteNullBooleanAsFalse
Feature that determines whether to write null booleans as false during serialization.
|
WriteNullListAsEmpty
Feature that determines whether to write null lists as empty arrays during serialization.
|
WriteNullNumberAsZero
Feature that determines whether to write null numbers as zero during serialization.
|
WriteNulls
Feature that determines whether to write null values during serialization.
|
WriteNullStringAsEmpty
Feature that determines whether to write null strings as empty strings during serialization.
|
WritePairAsJavaBean
Feature that determines whether to write key-value pairs as Java beans during serialization.
|
WriterUtilDateAsMillis
Feature that determines whether to write java.util.Date objects as milliseconds since epoch.
|
WriteThrowableClassName
Feature that determines whether to write the class name of Throwable objects during serialization.
|
| Modifier and Type | Field and Description |
|---|---|
long |
mask |
| Modifier and Type | Method and Description |
|---|---|
boolean |
isEnabled(long features)
Checks if this feature is enabled in the specified features bitmask.
|
static JSONWriter.Feature |
valueOf(java.lang.String name)
Returns the enum constant of this type with the specified name.
|
static JSONWriter.Feature[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static final JSONWriter.Feature FieldBased
By default, this feature is disabled, meaning that getter-based serialization is used.
public static final JSONWriter.Feature IgnoreNoneSerializable
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.
public static final JSONWriter.Feature ErrorOnNoneSerializable
Serializable.
By default, this feature is disabled, meaning that no exception is thrown for non-serializable classes.
public static final JSONWriter.Feature BeanToArray
By default, this feature is disabled, meaning that beans are serialized as JSON objects.
public static final JSONWriter.Feature WriteNulls
By default, this feature is disabled, meaning that null values are omitted from the output.
public static final JSONWriter.Feature WriteMapNullValue
By default, this feature is disabled, meaning that null map values are omitted from the output.
public static final JSONWriter.Feature BrowserCompatible
By default, this feature is disabled.
public static final JSONWriter.Feature NullAsDefaultValue
By default, this feature is disabled, meaning that null values are handled according to other features.
public static final JSONWriter.Feature WriteBooleanAsNumber
By default, this feature is disabled, meaning that boolean values are written as true/false.
public static final JSONWriter.Feature WriteNonStringValueAsString
By default, this feature is disabled, meaning that values are written in their native JSON types.
public static final JSONWriter.Feature WriteClassName
By default, this feature is disabled, meaning that class names are not included in the output.
public static final JSONWriter.Feature NotWriteRootClassName
By default, this feature is disabled.
public static final JSONWriter.Feature NotWriteHashMapArrayListClassName
By default, this feature is disabled.
public static final JSONWriter.Feature NotWriteDefaultValue
By default, this feature is disabled, meaning that all field values are written regardless of whether they are default values.
public static final JSONWriter.Feature WriteEnumsUsingName
By default, this feature is disabled.
public static final JSONWriter.Feature WriteEnumUsingToString
By default, this feature is disabled.
public static final JSONWriter.Feature IgnoreErrorGetter
By default, this feature is disabled, meaning that getter method exceptions are propagated.
public static final JSONWriter.Feature PrettyFormat
By default, this feature is disabled, meaning that JSON is output in compact form.
public static final JSONWriter.Feature ReferenceDetection
By default, this feature is disabled.
public static final JSONWriter.Feature WriteNameAsSymbol
By default, this feature is disabled.
public static final JSONWriter.Feature WriteBigDecimalAsPlain
By default, this feature is disabled.
public static final JSONWriter.Feature UseSingleQuotes
By default, this feature is disabled, meaning that double quotes are used.
public static final JSONWriter.Feature MapSortField
SortMapEntriesByKeys instead.public static final JSONWriter.Feature WriteNullListAsEmpty
By default, this feature is disabled.
public static final JSONWriter.Feature WriteNullStringAsEmpty
By default, this feature is disabled.
public static final JSONWriter.Feature WriteNullNumberAsZero
By default, this feature is disabled.
public static final JSONWriter.Feature WriteNullBooleanAsFalse
By default, this feature is disabled.
public static final JSONWriter.Feature NotWriteEmptyArray
By default, this feature is disabled.
public static final JSONWriter.Feature IgnoreEmpty
By default, this feature is disabled.
public static final JSONWriter.Feature WriteNonStringKeyAsString
By default, this feature is disabled.
public static final JSONWriter.Feature WritePairAsJavaBean
By default, this feature is disabled.
public static final JSONWriter.Feature OptimizedForAscii
By default, this feature is disabled.
public static final JSONWriter.Feature EscapeNoneAscii
public static final JSONWriter.Feature WriteByteArrayAsBase64
By default, this feature is disabled.
public static final JSONWriter.Feature IgnoreNonFieldGetter
By default, this feature is disabled.
public static final JSONWriter.Feature LargeObject
By default, this feature is disabled.
public static final JSONWriter.Feature WriteLongAsString
By default, this feature is disabled.
public static final JSONWriter.Feature BrowserSecure
By default, this feature is disabled.
public static final JSONWriter.Feature WriteEnumUsingOrdinal
By default, this feature is disabled.
public static final JSONWriter.Feature WriteThrowableClassName
By default, this feature is disabled.
public static final JSONWriter.Feature UnquoteFieldName
By default, this feature is disabled, meaning that field names are quoted.
public static final JSONWriter.Feature NotWriteSetClassName
By default, this feature is disabled.
public static final JSONWriter.Feature NotWriteNumberClassName
By default, this feature is disabled.
public static final JSONWriter.Feature SortMapEntriesByKeys
public static final JSONWriter.Feature PrettyFormatWith2Space
This feature requires PrettyFormat to also be enabled.
public static final JSONWriter.Feature PrettyFormatWith4Space
This feature requires PrettyFormat to also be enabled.
public static final JSONWriter.Feature WriterUtilDateAsMillis
By default, this feature is disabled.
public static final JSONWriter.Feature WriteFloatSpecialAsString
public static JSONWriter.Feature[] values()
for (JSONWriter.Feature c : JSONWriter.Feature.values()) System.out.println(c);
public static JSONWriter.Feature valueOf(java.lang.String name)
name - the name of the enum constant to be returned.java.lang.IllegalArgumentException - if this enum type has no constant with the specified namejava.lang.NullPointerException - if the argument is nullpublic boolean isEnabled(long features)
features - the features bitmask to check