Package com.alibaba.fastjson2
Class JSONReader.Context
java.lang.Object
com.alibaba.fastjson2.JSONReader.Context
- Enclosing class:
JSONReader
Context holds the configuration and state information for JSON reading operations.
It controls various aspects of the deserialization process including formatting,
features, providers, and other settings that affect how JSON data is parsed and
converted to Java objects.
The Context class is responsible for:
- Managing reader features that control deserialization behavior
- Handling date/time formatting and timezone settings
- Providing object and array suppliers for custom collection creation
- Managing auto-type handlers for security and customization
- Storing parser configuration such as max nesting level and buffer size
Context instances can be created in several ways:
// Using default configuration
JSONReader.Context context = new JSONReader.Context();
// With specific features enabled
JSONReader.Context context = new JSONReader.Context(
JSONReader.Feature.FieldBased,
JSONReader.Feature.TrimString
);
// With custom date format
JSONReader.Context context = new JSONReader.Context("yyyy-MM-dd HH:mm:ss");
// With custom provider and features
ObjectReaderProvider provider = JSONFactory.getDefaultObjectReaderProvider();
JSONReader.Context context = new JSONReader.Context(provider,
JSONReader.Feature.FieldBased
);
Once created, a Context can be configured further:
context.setZoneId(ZoneId.of("UTC"));
context.setLocale(Locale.US);
context.setMaxLevel(1000);
context.setBufferSize(64 * 1024);
context.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
Context instances are typically used when creating JSONReader instances:
JSONReader.Context context = new JSONReader.Context();
context.config(JSONReader.Feature.FieldBased);
try (JSONReader reader = JSONReader.of(json, context)) {
MyObject obj = reader.read(MyObject.class);
}
Note that Context instances are not thread-safe and should not be shared between multiple concurrent reading operations. Each JSONReader should have its own Context instance or use the default context provided by factory methods.
- Since:
- 2.0.0
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionContext(JSONReader.Feature... features) Creates a new Context with the specified features.Context(ObjectReaderProvider provider) Creates a new Context with the specified object reader provider.Context(ObjectReaderProvider provider, long features) Creates a new Context with the specified object reader provider and features.Context(ObjectReaderProvider provider, Filter filter, JSONReader.Feature... features) Creates a new Context with the specified object reader provider, filter, and features.Context(ObjectReaderProvider provider, JSONReader.Feature... features) Creates a new Context with the specified object reader provider and features.Context(ObjectReaderProvider provider, SymbolTable symbolTable) Creates a new Context with the specified object reader provider and symbol table.Context(ObjectReaderProvider provider, SymbolTable symbolTable, Filter[] filters, JSONReader.Feature... features) Creates a new Context with the specified object reader provider, symbol table, filters, and features.Context(ObjectReaderProvider provider, SymbolTable symbolTable, JSONReader.Feature... features) Creates a new Context with the specified object reader provider, symbol table, and features.Context(String dateFormat, JSONReader.Feature... features) Creates a new Context with the specified date format and features. -
Method Summary
Modifier and TypeMethodDescriptionvoidConfigures a filter for this context.voidConfigures filters for this context.voidconfig(Filter[] filters, JSONReader.Feature... features) Configures filters and features for this context.voidconfig(Filter filter, JSONReader.Feature... features) Configures a filter and features for this context.voidconfig(JSONReader.Feature... features) Configures features for this context.voidconfig(JSONReader.Feature feature, boolean state) Configures a specific feature for this context.Gets the array supplier configured for this context.intGets the buffer size configured for this context.Gets the AutoTypeBeforeHandler configured for this context.Gets the date format pattern configured for this context.Gets the date formatter configured for this context.Gets the ExtraProcessor configured for this context.longGets the features bitmask for this context.Gets the Locale configured for this context.intGets the maximum nesting level allowed for this context.getObjectReader(Type type) Gets an ObjectReader for the specified type.getObjectReaderAutoType(long hashCode) Gets an ObjectReader for the specified type hash code.getObjectReaderAutoType(String typeName, Class expectClass) Gets an ObjectReader for the specified type name and expected class.getObjectReaderAutoType(String typeName, Class expectClass, long features) Gets an ObjectReader for the specified type name, expected class, and additional features.Gets the object supplier configured for this context.Gets the ObjectReaderProvider used by this context.Gets the TimeZone configured for this context.Gets the ZoneId configured for this context.booleanisEnabled(JSONReader.Feature feature) Checks if the specified feature is enabled in this context.booleanChecks if the context is configured to format dates with hour information.booleanChecks if the context is configured to format dates in ISO8601 format.booleanChecks if the context is configured to format milliseconds.booleanChecks if the context is configured to format Unix time.booleanChecks if the context is configured to format dates in yyyyMMdd format (8 characters).booleanChecks if the context is configured to format dates in yyyyMMddHHmmss format (19 characters).booleanChecks if the context is configured to format dates in yyyy-MM-dd'T'HH:mm:ss format (19 characters).voidsetArraySupplier(Supplier<List> arraySupplier) Sets the array supplier for this context.setBufferSize(int bufferSize) Sets the buffer size for this context.voidsetDateFormat(String format) Sets the date format pattern for this context.voidsetDateFormatter(DateTimeFormatter dateFormatter) Sets the date formatter for this context.voidsetExtraProcessor(ExtraProcessor extraProcessor) Sets the ExtraProcessor for this context.voidsetFeatures(long features) Sets the features bitmask for this context.voidSets the Locale for this context.voidsetMaxLevel(int maxLevel) Sets the maximum nesting level allowed for this context.voidsetObjectSupplier(Supplier<Map> objectSupplier) Sets the object supplier for this context.voidsetTimeZone(TimeZone timeZone) Sets the TimeZone for this context.voidSets the ZoneId for this context.
-
Constructor Details
-
Context
Creates a new Context with the specified object reader provider.- Parameters:
provider- the object reader provider to use
-
Context
Creates a new Context with the specified object reader provider and features.- Parameters:
provider- the object reader provider to usefeatures- the initial features bitmask
-
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:
dateFormat- the date format pattern to usefeatures- the features to enable
-
Context
Creates a new Context with the specified object reader provider and features.- Parameters:
provider- the object reader provider to usefeatures- the features to enable
-
Context
Creates a new Context with the specified object reader provider, filter, and features.- Parameters:
provider- the object reader provider to usefilter- the filter to configurefeatures- the features to enable
-
Context
Creates a new Context with the specified object reader provider and symbol table.- Parameters:
provider- the object reader provider to usesymbolTable- the symbol table to use
-
Context
public Context(ObjectReaderProvider provider, SymbolTable symbolTable, JSONReader.Feature... features) Creates a new Context with the specified object reader provider, symbol table, and features.- Parameters:
provider- the object reader provider to usesymbolTable- the symbol table to usefeatures- the features to enable
-
Context
public Context(ObjectReaderProvider provider, SymbolTable symbolTable, Filter[] filters, JSONReader.Feature... features) Creates a new Context with the specified object reader provider, symbol table, filters, and features.- Parameters:
provider- the object reader provider to usesymbolTable- the symbol table to usefilters- the filters to configurefeatures- the features to enable
-
-
Method Details
-
isFormatUnixTime
public boolean isFormatUnixTime()Checks if the context is configured to format Unix time.- Returns:
- true if Unix time formatting is enabled, false otherwise
-
isFormatyyyyMMddhhmmss19
public boolean isFormatyyyyMMddhhmmss19()Checks if the context is configured to format dates in yyyyMMddHHmmss format (19 characters).- Returns:
- true if this format is enabled, false otherwise
-
isFormatyyyyMMddhhmmssT19
public boolean isFormatyyyyMMddhhmmssT19()Checks if the context is configured to format dates in yyyy-MM-dd'T'HH:mm:ss format (19 characters).- Returns:
- true if this format is enabled, false otherwise
-
isFormatyyyyMMdd8
public boolean isFormatyyyyMMdd8()Checks if the context is configured to format dates in yyyyMMdd format (8 characters).- Returns:
- true if this format is enabled, false otherwise
-
isFormatMillis
public boolean isFormatMillis()Checks if the context is configured to format milliseconds.- Returns:
- true if millisecond formatting is enabled, false otherwise
-
isFormatISO8601
public boolean isFormatISO8601()Checks if the context is configured to format dates in ISO8601 format.- Returns:
- true if ISO8601 formatting is enabled, false otherwise
-
isFormatHasHour
public boolean isFormatHasHour()Checks if the context is configured to format dates with hour information.- Returns:
- true if hour formatting is enabled, false otherwise
-
getObjectReader
Gets an ObjectReader for the specified type.- Parameters:
type- The type for which to get an ObjectReader- Returns:
- An ObjectReader for the specified type
-
getProvider
Gets the ObjectReaderProvider used by this context.- Returns:
- The ObjectReaderProvider
-
getObjectReaderAutoType
Gets an ObjectReader for the specified type hash code.- Parameters:
hashCode- The hash code of the type- Returns:
- An ObjectReader for the specified type hash code, or null if not found
-
getObjectReaderAutoType
Gets an ObjectReader for the specified type name and expected class.- Parameters:
typeName- The type nameexpectClass- The expected class- Returns:
- An ObjectReader for the specified type, or null if not found
-
getContextAutoTypeBeforeHandler
Gets the AutoTypeBeforeHandler configured for this context.- Returns:
- The AutoTypeBeforeHandler, or null if not configured
-
getObjectReaderAutoType
Gets an ObjectReader for the specified type name, expected class, and additional features.- Parameters:
typeName- The type nameexpectClass- The expected classfeatures- Additional features to consider- Returns:
- An ObjectReader for the specified type, or null if not found
-
getExtraProcessor
Gets the ExtraProcessor configured for this context.- Returns:
- The ExtraProcessor, or null if not configured
-
setExtraProcessor
Sets the ExtraProcessor for this context.- Parameters:
extraProcessor- The ExtraProcessor to set
-
getObjectSupplier
Gets the object supplier configured for this context.- Returns:
- The object supplier
-
setObjectSupplier
Sets the object supplier for this context.- Parameters:
objectSupplier- The object supplier to set
-
getArraySupplier
Gets the array supplier configured for this context.- Returns:
- The array supplier
-
setArraySupplier
Sets the array supplier for this context.- Parameters:
arraySupplier- The array supplier to set
-
getDateFormatter
Gets the date formatter configured for this context.- Returns:
- The DateTimeFormatter, or null if not configured
-
setDateFormatter
Sets the date formatter for this context.- Parameters:
dateFormatter- The DateTimeFormatter to set
-
getDateFormat
Gets the date format pattern configured for this context.- Returns:
- The date format pattern, or null if not set
-
setDateFormat
Sets the date format pattern for this context.- Parameters:
format- The date format pattern to set
-
getZoneId
Gets the ZoneId configured for this context.- Returns:
- The ZoneId
-
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
-
setZoneId
Sets the ZoneId for this context.- Parameters:
zoneId- The ZoneId 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
-
getBufferSize
public int getBufferSize()Gets the buffer size configured for this context.- Returns:
- The buffer size in bytes
-
setBufferSize
Sets the buffer size for this context.- Parameters:
bufferSize- The buffer size to set in bytes- Returns:
- This Context instance for method chaining
- Throws:
IllegalArgumentException- if bufferSize is negative
-
getLocale
Gets the Locale configured for this context.- Returns:
- The Locale
-
setLocale
Sets the Locale for this context.- Parameters:
locale- The Locale to set
-
getTimeZone
Gets the TimeZone configured for this context.- Returns:
- The TimeZone
-
setTimeZone
Sets the TimeZone for this context.- Parameters:
timeZone- The TimeZone to set
-
config
Configures features for this context.- Parameters:
features- The features to enable
-
config
Configures a filter and features for this context.- Parameters:
filter- The filter to configurefeatures- The features to enable
-
config
Configures a filter for this context.- Parameters:
filter- The filter to configure
-
config
Configures filters and features for this context.- Parameters:
filters- The filters to configurefeatures- The features to enable
-
config
Configures filters for this context.- Parameters:
filters- The filters to configure
-
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
-
config
Configures a specific feature for this context.- Parameters:
feature- The feature to configurestate- true to enable the feature, false to disable it
-