Class JSONWriter.Context

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

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

  • Constructor Details

    • Context

      public Context(ObjectWriterProvider provider)
      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

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

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

      public Context(ObjectWriterProvider provider, JSONWriter.Feature... features)
      Creates a new Context with the specified object writer provider and features.
      Parameters:
      provider - the object writer provider to use
      features - 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

      public boolean isEnabled(JSONWriter.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
    • 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

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

      public void config(JSONWriter.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
    • configFilter

      public void configFilter(Filter... filters)
      Configures filters for this context.
      Parameters:
      filters - the filters to configure
    • getObjectWriter

      public <T> ObjectWriter<T> getObjectWriter(Class<T> objectType)
      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

      public <T> ObjectWriter<T> getObjectWriter(Type objectType, Class<T> objectClass)
      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 serialized
      objectClass - the class of objects to be serialized
      Returns:
      the ObjectWriter for the specified type and class
    • getProvider

      public ObjectWriterProvider getProvider()
      Gets the ObjectWriterProvider used by this context.
      Returns:
      the ObjectWriterProvider
    • getZoneId

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

      public void setZoneId(ZoneId zoneId)
      Sets the ZoneId for this context.
      Parameters:
      zoneId - the ZoneId to set
    • getDateFormat

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

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

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

      public PropertyPreFilter getPropertyPreFilter()
      Gets the property pre-filter for this context.
      Returns:
      the property pre-filter, or null if not set
    • setPropertyPreFilter

      public void setPropertyPreFilter(PropertyPreFilter propertyPreFilter)
      Sets the property pre-filter for this context.
      Parameters:
      propertyPreFilter - the property pre-filter to set
    • getNameFilter

      public NameFilter getNameFilter()
      Gets the name filter for this context.
      Returns:
      the name filter, or null if not set
    • setNameFilter

      public void setNameFilter(NameFilter nameFilter)
      Sets the name filter for this context.
      Parameters:
      nameFilter - the name filter to set
    • getValueFilter

      public ValueFilter getValueFilter()
      Gets the value filter for this context.
      Returns:
      the value filter, or null if not set
    • setValueFilter

      public void setValueFilter(ValueFilter valueFilter)
      Sets the value filter for this context.
      Parameters:
      valueFilter - the value filter to set
    • getContextValueFilter

      public ContextValueFilter getContextValueFilter()
      Gets the context value filter for this context.
      Returns:
      the context value filter, or null if not set
    • setContextValueFilter

      public void setContextValueFilter(ContextValueFilter contextValueFilter)
      Sets the context value filter for this context.
      Parameters:
      contextValueFilter - the context value filter to set
    • getContextNameFilter

      public ContextNameFilter getContextNameFilter()
      Gets the context name filter for this context.
      Returns:
      the context name filter, or null if not set
    • setContextNameFilter

      public void setContextNameFilter(ContextNameFilter contextNameFilter)
      Sets the context name filter for this context.
      Parameters:
      contextNameFilter - the context name filter to set
    • getPropertyFilter

      public PropertyFilter getPropertyFilter()
      Gets the property filter for this context.
      Returns:
      the property filter, or null if not set
    • setPropertyFilter

      public void setPropertyFilter(PropertyFilter propertyFilter)
      Sets the property filter for this context.
      Parameters:
      propertyFilter - the property filter to set
    • getAfterFilter

      public AfterFilter getAfterFilter()
      Gets the after filter for this context.
      Returns:
      the after filter, or null if not set
    • setAfterFilter

      public void setAfterFilter(AfterFilter afterFilter)
      Sets the after filter for this context.
      Parameters:
      afterFilter - the after filter to set
    • getBeforeFilter

      public BeforeFilter getBeforeFilter()
      Gets the before filter for this context.
      Returns:
      the before filter, or null if not set
    • setBeforeFilter

      public void setBeforeFilter(BeforeFilter beforeFilter)
      Sets the before filter for this context.
      Parameters:
      beforeFilter - the before filter to set
    • getLabelFilter

      public LabelFilter getLabelFilter()
      Gets the label filter for this context.
      Returns:
      the label filter, or null if not set
    • setLabelFilter

      public void setLabelFilter(LabelFilter labelFilter)
      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