Class ObjectWriterProvider

java.lang.Object
com.alibaba.fastjson2.writer.ObjectWriterProvider
All Implemented Interfaces:
ObjectCodecProvider

public class ObjectWriterProvider extends Object implements ObjectCodecProvider
ObjectWriterProvider is responsible for providing and managing ObjectWriter instances for serializing Java objects into JSON format. It handles object writer creation, caching, type conversion, and various serialization features.

This provider supports various features including:

  • Object writer caching for performance optimization
  • Mixin support for modifying serialization behavior
  • Module-based extensibility
  • Custom type writer registration
  • Property naming strategy configuration

Example usage:

 // Get default provider
 ObjectWriterProvider provider = JSONFactory.getDefaultObjectWriterProvider();

 // Get object writer for a specific type
 ObjectWriter<User> writer = provider.getObjectWriter(User.class);

 // Serialize object to JSON string
 User user = new User(1, "John");
 String jsonString = writer.toJSONString(user);

 // Register custom object writer
 provider.register(User.class, new CustomUserWriter());
 
Since:
2.0.0
  • Constructor Details

    • ObjectWriterProvider

      public ObjectWriterProvider()
      Constructs an ObjectWriterProvider with default settings.
    • ObjectWriterProvider

      public ObjectWriterProvider(PropertyNamingStrategy namingStrategy)
      Constructs an ObjectWriterProvider with the specified naming strategy.
      Parameters:
      namingStrategy - the property naming strategy to use
    • ObjectWriterProvider

      public ObjectWriterProvider(ObjectWriterCreator creator)
      Constructs an ObjectWriterProvider with the specified ObjectWriterCreator.
      Parameters:
      creator - the ObjectWriterCreator to use for creating ObjectWriter instances
  • Method Details

    • getNamingStrategy

      public PropertyNamingStrategy getNamingStrategy()
      Gets the property naming strategy used by this provider.
      Returns:
      the property naming strategy, or null if none is set
    • setCompatibleWithFieldName

      public void setCompatibleWithFieldName(boolean stat)
      Deprecated.
      only use compatible with fastjson 1.x
      Sets whether to use compatible field name behavior.
      Parameters:
      stat - true to enable compatible field name behavior, false to disable
    • setNamingStrategy

      public void setNamingStrategy(PropertyNamingStrategy namingStrategy)
      Sets the property naming strategy used by this provider.
      Parameters:
      namingStrategy - the property naming strategy to set
    • mixIn

      public void mixIn(Class target, Class mixinSource)
      Registers a mixin mapping between a target class and a mixin source class. Mixin allows modifying the serialization behavior of a class by applying annotations from another class.
      Parameters:
      target - the target class to which the mixin will be applied
      mixinSource - the source class from which annotations will be copied, or null to remove the mixin
    • cleanupMixIn

      public void cleanupMixIn()
      Clears all mixin mappings.
    • getCreator

      public ObjectWriterCreator getCreator()
      Gets the ObjectWriterCreator used by this provider. If a context-specific creator is available, it will be returned; otherwise, the default creator for this provider will be returned.
      Returns:
      the ObjectWriterCreator
    • register

      public ObjectWriter register(Type type, ObjectWriter objectWriter)
      Registers an ObjectWriter for the specified type using the default field-based setting.
      Parameters:
      type - the type for which to register the ObjectWriter
      objectWriter - the ObjectWriter to register, or null to unregister
      Returns:
      the previous ObjectWriter for the type, or null if there was no previous ObjectWriter
    • register

      public ObjectWriter register(Type type, ObjectWriter objectWriter, boolean fieldBased)
      Registers an ObjectWriter for the specified type.
      Parameters:
      type - the type for which to register the ObjectWriter
      objectWriter - the ObjectWriter to register, or null to unregister
      fieldBased - whether the ObjectWriter is field-based
      Returns:
      the previous ObjectWriter for the type, or null if there was no previous ObjectWriter
    • registerIfAbsent

      public ObjectWriter registerIfAbsent(Type type, ObjectWriter objectWriter)
      Registers an ObjectWriter for the specified type using method-based writing if it is not already registered.
      Parameters:
      type - the type for which to register the ObjectWriter
      objectWriter - the ObjectWriter to register
      Returns:
      the previous ObjectWriter for the type, or null if there was no previous ObjectWriter
    • registerIfAbsent

      public ObjectWriter registerIfAbsent(Type type, ObjectWriter objectWriter, boolean fieldBased)
      Registers an ObjectWriter for the specified type if it is not already registered.
      Parameters:
      type - the type for which to register the ObjectWriter
      objectWriter - the ObjectWriter to register
      fieldBased - whether the ObjectWriter is field-based
      Returns:
      the previous ObjectWriter for the type, or null if there was no previous ObjectWriter
    • unregister

      public ObjectWriter unregister(Type type)
      Unregisters the ObjectWriter for the specified type using method-based writing.
      Parameters:
      type - the type for which to unregister the ObjectWriter
      Returns:
      the unregistered ObjectWriter, or null if there was no ObjectWriter for the type
    • unregister

      public ObjectWriter unregister(Type type, boolean fieldBased)
      Unregisters the ObjectWriter for the specified type.
      Parameters:
      type - the type for which to unregister the ObjectWriter
      fieldBased - whether the ObjectWriter is field-based
      Returns:
      the unregistered ObjectWriter, or null if there was no ObjectWriter for the type
    • unregister

      public boolean unregister(Type type, ObjectWriter objectWriter)
      Unregisters the specified ObjectWriter for the given type, but only if the currently registered writer matches the specified writer.
      Parameters:
      type - the type for which to unregister the ObjectWriter
      objectWriter - the ObjectWriter to unregister
      Returns:
      true if the ObjectWriter was unregistered, false otherwise
    • unregister

      public boolean unregister(Type type, ObjectWriter objectWriter, boolean fieldBased)
      Unregisters the specified ObjectWriter for the given type, but only if the currently registered writer matches the specified writer.
      Parameters:
      type - the type for which to unregister the ObjectWriter
      objectWriter - the ObjectWriter to unregister
      fieldBased - whether the ObjectWriter is field-based
      Returns:
      true if the ObjectWriter was unregistered, false otherwise
    • register

      public boolean register(ObjectWriterModule module)
      Registers an ObjectWriterModule. If the module is already registered, this method does nothing and returns false.
      Parameters:
      module - the module to register
      Returns:
      true if the module was registered, false if it was already registered
    • unregister

      public boolean unregister(ObjectWriterModule module)
      Unregisters an ObjectWriterModule.
      Parameters:
      module - the module to unregister
      Returns:
      true if the module was unregistered, false if it was not registered
    • getMixIn

      public Class getMixIn(Class target)
      Gets the mixin source class for the specified target class.
      Specified by:
      getMixIn in interface ObjectCodecProvider
      Parameters:
      target - the target class
      Returns:
      the mixin source class, or null if no mixin is registered for the target
    • init

      public void init()
      Initializes this provider with the base module.
    • getModules

      public List<ObjectWriterModule> getModules()
      Gets the list of registered ObjectWriter modules.
      Returns:
      the list of modules
    • getFieldInfo

      public void getFieldInfo(BeanInfo beanInfo, FieldInfo fieldInfo, Class objectClass, Field field)
      Gets field information for the specified field of a class.
      Parameters:
      beanInfo - the BeanInfo object to populate with bean information
      fieldInfo - the FieldInfo object to populate with field information
      objectClass - the class containing the field
      field - the field for which to get information
    • getFieldInfo

      public void getFieldInfo(BeanInfo beanInfo, FieldInfo fieldInfo, Class objectClass, Method method)
      Gets field information for the specified method of a class.
      Parameters:
      beanInfo - the BeanInfo object to populate with bean information
      fieldInfo - the FieldInfo object to populate with field information
      objectClass - the class containing the method
      method - the method for which to get information
    • getBeanInfo

      public void getBeanInfo(BeanInfo beanInfo, Class objectClass)
      Gets bean information for the specified class by delegating to registered modules.
      Parameters:
      beanInfo - the BeanInfo object to populate with bean information
      objectClass - the class for which to get bean information
    • getObjectWriter

      public ObjectWriter getObjectWriter(Type objectType, String format, Locale locale)
      Gets an ObjectWriter for the specified type with formatting.
      Parameters:
      objectType - the type for which to get an ObjectWriter
      format - the format string to use
      locale - the locale to use
      Returns:
      the ObjectWriter for the specified type
    • getObjectWriter

      public ObjectWriter getObjectWriter(Class objectClass)
      Gets an ObjectWriter for the specified class.
      Parameters:
      objectClass - the class for which to get an ObjectWriter
      Returns:
      the ObjectWriter for the specified class
    • getObjectWriter

      public ObjectWriter getObjectWriter(Type objectType, Class objectClass)
      Gets an ObjectWriter for the specified type and class.
      Parameters:
      objectType - the type for which to get an ObjectWriter
      objectClass - the class for which to get an ObjectWriter
      Returns:
      the ObjectWriter for the specified type and class
    • getObjectWriter

      public ObjectWriter getObjectWriter(Type objectType)
      Gets an ObjectWriter for the specified type.
      Parameters:
      objectType - the type for which to get an ObjectWriter
      Returns:
      the ObjectWriter for the specified type
    • getObjectWriterFromCache

      public ObjectWriter getObjectWriterFromCache(Type objectType, Class objectClass, boolean fieldBased)
      Gets an ObjectWriter from the cache for the specified type and class.
      Parameters:
      objectType - the type for which to get an ObjectWriter
      objectClass - the class for which to get an ObjectWriter
      fieldBased - whether to use field-based writing
      Returns:
      the ObjectWriter from the cache, or null if not found
    • getObjectWriter

      public ObjectWriter getObjectWriter(Type objectType, Class objectClass, String format, boolean fieldBased)
      Gets an ObjectWriter for the specified type, class, and format with field-based option.
      Parameters:
      objectType - the type for which to get an ObjectWriter
      objectClass - the class for which to get an ObjectWriter
      format - the format string to use
      fieldBased - whether to use field-based writing
      Returns:
      the ObjectWriter for the specified type, class, and format
    • getObjectWriter

      public ObjectWriter getObjectWriter(Type objectType, Class objectClass, boolean fieldBased)
      Gets an ObjectWriter for the specified type, class, and field-based option. If an ObjectWriter for the type is already cached, it will be returned directly. Otherwise, a new ObjectWriter will be created and cached.
      Parameters:
      objectType - the type for which to get an ObjectWriter
      objectClass - the class for which to get an ObjectWriter
      fieldBased - whether to use field-based writing
      Returns:
      the ObjectWriter for the specified type and class
    • isPrimitiveOrEnum

      public static boolean isPrimitiveOrEnum(Class<?> clazz)
      Checks if the specified class is a primitive type or an enum.
      Parameters:
      clazz - the class to check
      Returns:
      true if the class is a primitive type or an enum, false otherwise
    • isNotReferenceDetect

      public static boolean isNotReferenceDetect(Class<?> clazz)
      Checks if the specified class is a type that should not have reference detection.
      Parameters:
      clazz - the class to check
      Returns:
      true if the class should not have reference detection, false otherwise
    • clear

      public void clear()
      Clears all cached ObjectWriters and mixin mappings.
      Since:
      2.0.53
    • cleanup

      public void cleanup(Class objectClass)
      Cleans up cached ObjectWriters and mixin mappings associated with the specified class.
      Parameters:
      objectClass - the class for which to clean up cached ObjectWriters
    • cleanup

      public void cleanup(ClassLoader classLoader)
      Cleans up cached ObjectWriters associated with the specified ClassLoader. This method removes all cached writers that are related to classes loaded by the given ClassLoader.
      Parameters:
      classLoader - the ClassLoader for which to clean up cached ObjectWriters
    • isDisableReferenceDetect

      public boolean isDisableReferenceDetect()
      Checks if reference detection is disabled.
      Returns:
      true if reference detection is disabled, false otherwise
    • isDisableAutoType

      public boolean isDisableAutoType()
      Checks if auto-type support is disabled.
      Returns:
      true if auto-type support is disabled, false otherwise
    • isDisableJSONB

      public boolean isDisableJSONB()
      Checks if JSONB support is disabled.
      Returns:
      true if JSONB support is disabled, false otherwise
    • isDisableArrayMapping

      public boolean isDisableArrayMapping()
      Checks if array mapping is disabled.
      Returns:
      true if array mapping is disabled, false otherwise
    • setDisableReferenceDetect

      public void setDisableReferenceDetect(boolean disableReferenceDetect)
      Sets whether reference detection is disabled.
      Parameters:
      disableReferenceDetect - true to disable reference detection, false to enable it
    • setDisableArrayMapping

      public void setDisableArrayMapping(boolean disableArrayMapping)
      Sets whether array mapping is disabled.
      Parameters:
      disableArrayMapping - true to disable array mapping, false to enable it
    • setDisableJSONB

      public void setDisableJSONB(boolean disableJSONB)
      Sets whether JSONB support is disabled.
      Parameters:
      disableJSONB - true to disable JSONB support, false to enable it
    • setDisableAutoType

      public void setDisableAutoType(boolean disableAutoType)
      Sets whether auto-type support is disabled.
      Parameters:
      disableAutoType - true to disable auto-type support, false to enable it
    • isAlphabetic

      public boolean isAlphabetic()
      Checks if alphabetic ordering is enabled.
      Returns:
      true if alphabetic ordering is enabled, false otherwise
    • setAlphabetic

      public void setAlphabetic(boolean alphabetic)
      Sets whether alphabetic ordering is enabled.
      Parameters:
      alphabetic - true to enable alphabetic ordering, false to disable it
    • isSkipTransient

      public boolean isSkipTransient()
      Checks if transient fields should be skipped.
      Returns:
      true if transient fields should be skipped, false otherwise
    • setSkipTransient

      public void setSkipTransient(boolean skipTransient)
      Sets whether transient fields should be skipped.
      Parameters:
      skipTransient - true to skip transient fields, false to include them
    • createBeanInfo

      protected BeanInfo createBeanInfo()
      Creates a new BeanInfo instance.
      Returns:
      a new BeanInfo instance
    • configEnumAsJavaBean

      @SafeVarargs public final void configEnumAsJavaBean(Class<? extends Enum>... enumClasses)
      Configure the Enum classes as a JavaBean
      Parameters:
      enumClasses - enum classes
      Since:
      2.0.55