Class JSONReader.Context

java.lang.Object
com.alibaba.fastjson2.JSONReader.Context
Enclosing class:
JSONReader

public static final class JSONReader.Context extends Object
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 Details

    • Context

      public Context(ObjectReaderProvider provider)
      Creates a new Context with the specified object reader provider.
      Parameters:
      provider - the object reader provider to use
    • Context

      public Context(ObjectReaderProvider provider, long features)
      Creates a new Context with the specified object reader provider and features.
      Parameters:
      provider - the object reader provider to use
      features - the initial features bitmask
    • Context

      public Context(JSONReader.Feature... features)
      Creates a new Context with the specified features.
      Parameters:
      features - the features to enable
    • Context

      public Context(String dateFormat, JSONReader.Feature... features)
      Creates a new Context with the specified date format and features.
      Parameters:
      dateFormat - the date format pattern to use
      features - the features to enable
    • Context

      public Context(ObjectReaderProvider provider, JSONReader.Feature... features)
      Creates a new Context with the specified object reader provider and features.
      Parameters:
      provider - the object reader provider to use
      features - the features to enable
    • Context

      public Context(ObjectReaderProvider provider, Filter filter, JSONReader.Feature... features)
      Creates a new Context with the specified object reader provider, filter, and features.
      Parameters:
      provider - the object reader provider to use
      filter - the filter to configure
      features - the features to enable
    • Context

      public Context(ObjectReaderProvider provider, SymbolTable symbolTable)
      Creates a new Context with the specified object reader provider and symbol table.
      Parameters:
      provider - the object reader provider to use
      symbolTable - 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 use
      symbolTable - the symbol table to use
      features - 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 use
      symbolTable - the symbol table to use
      filters - the filters to configure
      features - 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

      public ObjectReader getObjectReader(Type type)
      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

      public ObjectReaderProvider getProvider()
      Gets the ObjectReaderProvider used by this context.
      Returns:
      The ObjectReaderProvider
    • getObjectReaderAutoType

      public ObjectReader getObjectReaderAutoType(long hashCode)
      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

      public ObjectReader getObjectReaderAutoType(String typeName, Class expectClass)
      Gets an ObjectReader for the specified type name and expected class.
      Parameters:
      typeName - The type name
      expectClass - The expected class
      Returns:
      An ObjectReader for the specified type, or null if not found
    • getContextAutoTypeBeforeHandler

      public JSONReader.AutoTypeBeforeHandler getContextAutoTypeBeforeHandler()
      Gets the AutoTypeBeforeHandler configured for this context.
      Returns:
      The AutoTypeBeforeHandler, or null if not configured
    • getObjectReaderAutoType

      public ObjectReader getObjectReaderAutoType(String typeName, Class expectClass, long features)
      Gets an ObjectReader for the specified type name, expected class, and additional features.
      Parameters:
      typeName - The type name
      expectClass - The expected class
      features - Additional features to consider
      Returns:
      An ObjectReader for the specified type, or null if not found
    • getExtraProcessor

      public ExtraProcessor getExtraProcessor()
      Gets the ExtraProcessor configured for this context.
      Returns:
      The ExtraProcessor, or null if not configured
    • setExtraProcessor

      public void setExtraProcessor(ExtraProcessor extraProcessor)
      Sets the ExtraProcessor for this context.
      Parameters:
      extraProcessor - The ExtraProcessor to set
    • getObjectSupplier

      public Supplier<Map> getObjectSupplier()
      Gets the object supplier configured for this context.
      Returns:
      The object supplier
    • setObjectSupplier

      public void setObjectSupplier(Supplier<Map> objectSupplier)
      Sets the object supplier for this context.
      Parameters:
      objectSupplier - The object supplier to set
    • getArraySupplier

      public Supplier<List> getArraySupplier()
      Gets the array supplier configured for this context.
      Returns:
      The array supplier
    • setArraySupplier

      public void setArraySupplier(Supplier<List> arraySupplier)
      Sets the array supplier for this context.
      Parameters:
      arraySupplier - The array supplier to set
    • getDateFormatter

      public DateTimeFormatter getDateFormatter()
      Gets the date formatter configured for this context.
      Returns:
      The DateTimeFormatter, or null if not configured
    • setDateFormatter

      public void setDateFormatter(DateTimeFormatter dateFormatter)
      Sets the date formatter for this context.
      Parameters:
      dateFormatter - The DateTimeFormatter to set
    • getDateFormat

      public String getDateFormat()
      Gets the date format pattern configured for this context.
      Returns:
      The date format pattern, or null if not set
    • setDateFormat

      public void setDateFormat(String format)
      Sets the date format pattern for this context.
      Parameters:
      format - The date format pattern to set
    • getZoneId

      public ZoneId 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

      public void setZoneId(ZoneId zoneId)
      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

      public JSONReader.Context setBufferSize(int bufferSize)
      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

      public Locale getLocale()
      Gets the Locale configured for this context.
      Returns:
      The Locale
    • setLocale

      public void setLocale(Locale locale)
      Sets the Locale for this context.
      Parameters:
      locale - The Locale to set
    • getTimeZone

      public TimeZone getTimeZone()
      Gets the TimeZone configured for this context.
      Returns:
      The TimeZone
    • setTimeZone

      public void setTimeZone(TimeZone timeZone)
      Sets the TimeZone for this context.
      Parameters:
      timeZone - The TimeZone to set
    • config

      public void config(JSONReader.Feature... features)
      Configures features for this context.
      Parameters:
      features - The features to enable
    • config

      public void config(Filter filter, JSONReader.Feature... features)
      Configures a filter and features for this context.
      Parameters:
      filter - The filter to configure
      features - The features to enable
    • config

      public void config(Filter filter)
      Configures a filter for this context.
      Parameters:
      filter - The filter to configure
    • config

      public void config(Filter[] filters, JSONReader.Feature... features)
      Configures filters and features for this context.
      Parameters:
      filters - The filters to configure
      features - The features to enable
    • config

      public void config(Filter[] filters)
      Configures filters for this context.
      Parameters:
      filters - The filters to configure
    • isEnabled

      public boolean isEnabled(JSONReader.Feature feature)
      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

      public void config(JSONReader.Feature feature, boolean state)
      Configures a specific feature for this context.
      Parameters:
      feature - The feature to configure
      state - true to enable the feature, false to disable it