Package com.alibaba.fastjson2
Class JSONWriter.Context
java.lang.Object
com.alibaba.fastjson2.JSONWriter.Context
- Enclosing class:
JSONWriter
Context holds the configuration and state information for JSON writing operations.
It controls various aspects of the serialization process including formatting,
features, providers, filters, and other settings that affect how Java objects
are converted to JSON format.
The Context class is responsible for:
- Managing writer features that control serialization behavior
- Handling date/time formatting and timezone settings
- Managing filters for customizing serialization output
- Storing serializer configuration such as max nesting level
- Providing object writer providers for type-specific serialization
Context instances can be created in several ways:
// Using default configuration
JSONWriter.Context context = new JSONWriter.Context();
// With specific features enabled
JSONWriter.Context context = new JSONWriter.Context(
JSONWriter.Feature.PrettyFormat,
JSONWriter.Feature.WriteMapNullValue
);
// With custom date format
JSONWriter.Context context = new JSONWriter.Context("yyyy-MM-dd HH:mm:ss");
// With custom provider and features
ObjectWriterProvider provider = JSONFactory.getDefaultObjectWriterProvider();
JSONWriter.Context context = new JSONWriter.Context(provider,
JSONWriter.Feature.PrettyFormat
);
Once created, a Context can be configured further:
context.setZoneId(ZoneId.of("UTC"));
context.setLocale(Locale.US);
context.setMaxLevel(1000);
context.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
Context instances are typically used when creating JSONWriter instances:
JSONWriter.Context context = new JSONWriter.Context();
context.config(JSONWriter.Feature.PrettyFormat);
try (JSONWriter writer = JSONWriter.of(context)) {
writer.writeAny(object);
String json = writer.toString();
}
Note that Context instances are not thread-safe and should not be shared between multiple concurrent writing operations. Each JSONWriter should have its own Context instance or use the default context provided by factory methods.
- Since:
- 2.0.0
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionContext(JSONWriter.Feature... features) Creates a new Context with the specified features.Context(ObjectWriterProvider provider) Creates a new Context with the specified object writer provider.Context(ObjectWriterProvider provider, JSONWriter.Feature... features) Creates a new Context with the specified object writer provider and features.Context(String format, JSONWriter.Feature... features) Creates a new Context with the specified date format and features. -
Method Summary
Modifier and TypeMethodDescriptionvoidconfig(JSONWriter.Feature... features) Configures features for this context.voidconfig(JSONWriter.Feature feature, boolean state) Configures a specific feature for this context.voidconfigFilter(Filter... filters) Configures filters for this context.Gets the after filter for this context.Gets the before filter for this context.Gets the context name filter for this context.Gets the context value filter for this context.Gets the date format pattern for this context.Gets the date formatter for this context.longGets the features bitmask for this context.Gets the label filter for this context.intGets the maximum nesting level allowed for this context.Gets the name filter for this context.<T> ObjectWriter<T> getObjectWriter(Class<T> objectType) Gets the ObjectWriter for the specified object type.<T> ObjectWriter<T> getObjectWriter(Type objectType, Class<T> objectClass) Gets the ObjectWriter for the specified object type and class.Gets the property filter for this context.Gets the property pre-filter for this context.Gets the ObjectWriterProvider used by this context.Gets the value filter for this context.Gets the ZoneId used by this context.booleanChecks if the date format includes day information.booleanChecks if the date format includes hour information.booleanChecks if the date format is configured to use ISO8601 format.booleanChecks if the date format is configured to use milliseconds.booleanChecks if the date format is configured to use Unix time.booleanisEnabled(long feature) Checks if the specified feature mask is enabled in this context.booleanisEnabled(JSONWriter.Feature feature) Checks if the specified feature is enabled in this context.booleanChecks if the date format is configured to use yyyy-MM-dd HH:mm:ss format (19 characters).voidsetAfterFilter(AfterFilter afterFilter) Sets the after filter for this context.voidsetBeforeFilter(BeforeFilter beforeFilter) Sets the before filter for this context.voidsetContextNameFilter(ContextNameFilter contextNameFilter) Sets the context name filter for this context.voidsetContextValueFilter(ContextValueFilter contextValueFilter) Sets the context value filter for this context.voidsetDateFormat(String dateFormat) Sets the date format pattern for this context.voidsetFeatures(long features) Sets the features bitmask for this context.voidsetLabelFilter(LabelFilter labelFilter) Sets the label filter for this context.voidsetMaxLevel(int maxLevel) Sets the maximum nesting level allowed for this context.voidsetNameFilter(NameFilter nameFilter) Sets the name filter for this context.voidsetPropertyFilter(PropertyFilter propertyFilter) Sets the property filter for this context.voidsetPropertyPreFilter(PropertyPreFilter propertyPreFilter) Sets the property pre-filter for this context.voidsetValueFilter(ValueFilter valueFilter) Sets the value filter for this context.voidSets the ZoneId for this context.
-
Field Details
-
provider
-
-
Constructor Details
-
Context
Creates a new Context with the specified object writer provider.- Parameters:
provider- the object writer provider to use- Throws:
IllegalArgumentException- if provider is null
-
Context
Creates a new Context with the specified features.- Parameters:
features- the features to enable
-
Context
Creates a new Context with the specified date format and features.- Parameters:
format- the date format pattern to usefeatures- the features to enable
-
Context
Creates a new Context with the specified object writer provider and features.- Parameters:
provider- the object writer provider to usefeatures- the features to enable- Throws:
IllegalArgumentException- if provider is null
-
-
Method Details
-
getFeatures
public long getFeatures()Gets the features bitmask for this context.- Returns:
- the features bitmask
-
setFeatures
public void setFeatures(long features) Sets the features bitmask for this context.- Parameters:
features- the features bitmask to set- Since:
- 2.0.51
-
isEnabled
Checks if the specified feature is enabled in this context.- Parameters:
feature- the feature to check- Returns:
- true if the feature is enabled, false otherwise
-
isEnabled
public boolean isEnabled(long feature) Checks if the specified feature mask is enabled in this context.- Parameters:
feature- the feature mask to check- Returns:
- true if the feature is enabled, false otherwise
-
config
Configures features for this context.- Parameters:
features- the features to enable
-
config
Configures a specific feature for this context.- Parameters:
feature- the feature to configurestate- true to enable the feature, false to disable it
-
configFilter
Configures filters for this context.- Parameters:
filters- the filters to configure
-
getObjectWriter
Gets the ObjectWriter for the specified object type. This method retrieves an ObjectWriter instance that can serialize objects of the specified type.- Type Parameters:
T- the type of objects to be serialized- Parameters:
objectType- the class of objects to be serialized- Returns:
- the ObjectWriter for the specified type
-
getObjectWriter
Gets the ObjectWriter for the specified object type and class. This method retrieves an ObjectWriter instance that can serialize objects of the specified type and class.- Type Parameters:
T- the type of objects to be serialized- Parameters:
objectType- the type of objects to be serializedobjectClass- the class of objects to be serialized- Returns:
- the ObjectWriter for the specified type and class
-
getProvider
Gets the ObjectWriterProvider used by this context.- Returns:
- the ObjectWriterProvider
-
getZoneId
Gets the ZoneId used by this context. If no ZoneId has been set, the system default ZoneId will be returned.- Returns:
- the ZoneId
-
setZoneId
Sets the ZoneId for this context.- Parameters:
zoneId- the ZoneId to set
-
getDateFormat
Gets the date format pattern for this context.- Returns:
- the date format pattern, or null if not set
-
isDateFormatMillis
public boolean isDateFormatMillis()Checks if the date format is configured to use milliseconds.- Returns:
- true if milliseconds format is enabled, false otherwise
-
isDateFormatUnixTime
public boolean isDateFormatUnixTime()Checks if the date format is configured to use Unix time.- Returns:
- true if Unix time format is enabled, false otherwise
-
isDateFormatISO8601
public boolean isDateFormatISO8601()Checks if the date format is configured to use ISO8601 format.- Returns:
- true if ISO8601 format is enabled, false otherwise
-
isDateFormatHasDay
public boolean isDateFormatHasDay()Checks if the date format includes day information.- Returns:
- true if day information is included, false otherwise
-
isDateFormatHasHour
public boolean isDateFormatHasHour()Checks if the date format includes hour information.- Returns:
- true if hour information is included, false otherwise
-
isFormatyyyyMMddhhmmss19
public boolean isFormatyyyyMMddhhmmss19()Checks if the date format is configured to use yyyy-MM-dd HH:mm:ss format (19 characters).- Returns:
- true if this format is enabled, false otherwise
-
getDateFormatter
Gets the date formatter for this context.- Returns:
- the date formatter, or null if not set
-
setDateFormat
Sets the date format pattern for this context.- Parameters:
dateFormat- the date format pattern to set
-
getPropertyPreFilter
Gets the property pre-filter for this context.- Returns:
- the property pre-filter, or null if not set
-
setPropertyPreFilter
Sets the property pre-filter for this context.- Parameters:
propertyPreFilter- the property pre-filter to set
-
getNameFilter
Gets the name filter for this context.- Returns:
- the name filter, or null if not set
-
setNameFilter
Sets the name filter for this context.- Parameters:
nameFilter- the name filter to set
-
getValueFilter
Gets the value filter for this context.- Returns:
- the value filter, or null if not set
-
setValueFilter
Sets the value filter for this context.- Parameters:
valueFilter- the value filter to set
-
getContextValueFilter
Gets the context value filter for this context.- Returns:
- the context value filter, or null if not set
-
setContextValueFilter
Sets the context value filter for this context.- Parameters:
contextValueFilter- the context value filter to set
-
getContextNameFilter
Gets the context name filter for this context.- Returns:
- the context name filter, or null if not set
-
setContextNameFilter
Sets the context name filter for this context.- Parameters:
contextNameFilter- the context name filter to set
-
getPropertyFilter
Gets the property filter for this context.- Returns:
- the property filter, or null if not set
-
setPropertyFilter
Sets the property filter for this context.- Parameters:
propertyFilter- the property filter to set
-
getAfterFilter
Gets the after filter for this context.- Returns:
- the after filter, or null if not set
-
setAfterFilter
Sets the after filter for this context.- Parameters:
afterFilter- the after filter to set
-
getBeforeFilter
Gets the before filter for this context.- Returns:
- the before filter, or null if not set
-
setBeforeFilter
Sets the before filter for this context.- Parameters:
beforeFilter- the before filter to set
-
getLabelFilter
Gets the label filter for this context.- Returns:
- the label filter, or null if not set
-
setLabelFilter
Sets the label filter for this context.- Parameters:
labelFilter- the label filter to set
-
getMaxLevel
public int getMaxLevel()Gets the maximum nesting level allowed for this context.- Returns:
- the maximum nesting level
-
setMaxLevel
public void setMaxLevel(int maxLevel) Sets the maximum nesting level allowed for this context.- Parameters:
maxLevel- the maximum nesting level to set
-