Class ObjectWriterCreator

java.lang.Object
com.alibaba.fastjson2.writer.ObjectWriterCreator
Direct Known Subclasses:
ObjectWriterCreatorASM

public class ObjectWriterCreator extends Object
ObjectWriterCreator is responsible for creating ObjectWriter instances for serializing Java objects into JSON format. It provides factory methods for creating ObjectWriters for various types of objects and fields.

This class supports various features including:

  • Creation of ObjectWriters for different object types
  • Creation of FieldWriters for different field types
  • Lambda expression support for getter methods
  • Custom field writer creation with various configurations
  • JIT compilation support for improved performance

Example usage:

 // Get default creator
 ObjectWriterCreator creator = JSONFactory.getDefaultObjectWriterCreator();

 // Create ObjectWriter for a class
 ObjectWriter<User> writer = creator.createObjectWriter(User.class);

 // Create FieldWriter for a field
 Field field = User.class.getDeclaredField("name");
 FieldWriter<User> fieldWriter = creator.createFieldWriter("name", null, field);
 
Since:
2.0.0
  • Field Details

  • Constructor Details

    • ObjectWriterCreator

      public ObjectWriterCreator()
      Constructs a new ObjectWriterCreator instance.
  • Method Details

    • createObjectWriter

      public ObjectWriter createObjectWriter(List<FieldWriter> fieldWriters)
      Creates an ObjectWriter for the specified list of FieldWriters.
      Parameters:
      fieldWriters - the list of FieldWriters to use
      Returns:
      an ObjectWriter instance
    • createObjectWriter

      public ObjectWriter createObjectWriter(FieldWriter... fieldWriters)
      Creates an ObjectWriter for the specified array of FieldWriters.
      Parameters:
      fieldWriters - the array of FieldWriters to use
      Returns:
      an ObjectWriter instance
    • createObjectWriter

      public <T> ObjectWriter<T> createObjectWriter(String[] names, Type[] types, FieldSupplier<T> supplier)
      Creates an ObjectWriter for the specified object type with names, types, and supplier.
      Type Parameters:
      T - the type of objects that the ObjectWriter can serialize
      Parameters:
      names - the field names
      types - the field types
      supplier - the FieldSupplier to use
      Returns:
      an ObjectWriter instance
    • createObjectWriter

      public ObjectWriter createObjectWriter(Class objectType)
      Creates an ObjectWriter for the specified object type.
      Parameters:
      objectType - the class of objects to serialize
      Returns:
      an ObjectWriter instance
    • createObjectWriter

      public ObjectWriter createObjectWriter(Class objectType, FieldWriter... fieldWriters)
      Creates an ObjectWriter for the specified object type and field writers.
      Parameters:
      objectType - the class of objects to serialize
      fieldWriters - the field writers to use
      Returns:
      an ObjectWriter instance
    • createObjectWriter

      public ObjectWriter createObjectWriter(Class objectClass, long features, FieldWriter... fieldWriters)
      Creates an ObjectWriter for the specified object class, features, and field writers.
      Parameters:
      objectClass - the class of objects to serialize
      features - the features to use
      fieldWriters - the field writers to use
      Returns:
      an ObjectWriter instance
    • createFieldWriter

      protected FieldWriter createFieldWriter(Class objectClass, long writerFeatures, ObjectWriterProvider provider, BeanInfo beanInfo, FieldInfo fieldInfo, Field field)
      Creates a FieldWriter for the specified field.
      Parameters:
      objectClass - the class containing the field
      writerFeatures - the writer features to use
      provider - the ObjectWriterProvider to use
      beanInfo - the BeanInfo to use
      fieldInfo - the FieldInfo to use
      field - the Field to create a writer for
      Returns:
      a FieldWriter instance, or null if the field should be ignored
    • createObjectWriter

      public ObjectWriter createObjectWriter(Class objectClass, long features, List<ObjectWriterModule> modules)
      Creates an ObjectWriter for the specified object class, features, and modules.
      Parameters:
      objectClass - the class of objects to serialize
      features - the features to use
      modules - the ObjectWriterModules to use
      Returns:
      an ObjectWriter instance
    • setDefaultValue

      protected void setDefaultValue(List<FieldWriter> fieldWriters, Class objectClass)
      Sets default values for the specified field writers using the default constructor of the object class.
      Parameters:
      fieldWriters - the list of FieldWriters to set default values for
      objectClass - the class of objects to create default instances from
    • createObjectWriter

      public ObjectWriter createObjectWriter(Class objectClass, long features, ObjectWriterProvider provider)
      Creates an ObjectWriter for the specified object class, features, and provider. This is the main method for creating ObjectWriters that handles all the complexity of analyzing the class structure and creating appropriate FieldWriters.
      Parameters:
      objectClass - the class of objects to serialize
      features - the features to use
      provider - the ObjectWriterProvider to use
      Returns:
      an ObjectWriter instance
    • getFieldName

      protected static String getFieldName(Class objectClass, ObjectWriterProvider provider, BeanInfo beanInfo, boolean record, FieldInfo fieldInfo, Method method)
      Gets the field name for the specified method based on various naming strategies and configurations.
      Parameters:
      objectClass - the class containing the method
      provider - the ObjectWriterProvider to use
      beanInfo - the BeanInfo containing configuration
      record - whether the class is a record
      fieldInfo - the FieldInfo containing field configuration
      method - the method to get the field name for
      Returns:
      the field name
    • configSerializeFilters

      protected static void configSerializeFilters(BeanInfo beanInfo, ObjectWriterAdapter writerAdapter)
      Configures serialize filters for the specified ObjectWriterAdapter.
      Parameters:
      beanInfo - the BeanInfo containing filter configuration
      writerAdapter - the ObjectWriterAdapter to configure filters for
    • handleIgnores

      protected void handleIgnores(BeanInfo beanInfo, List<FieldWriter> fieldWriters)
      Handles field ignores based on the BeanInfo configuration.
      Parameters:
      beanInfo - the BeanInfo containing ignore configuration
      fieldWriters - the list of FieldWriters to process
    • createFieldWriter

      public <T> FieldWriter<T> createFieldWriter(String fieldName, String format, Field field)
      Creates a FieldWriter for the specified field with default configuration.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      fieldName - the name of the field
      format - the date format to use
      field - the Field to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter<T> createFieldWriter(String fieldName, int ordinal, long features, String format, Field field)
      Creates a FieldWriter for the specified field with ordinal and features.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use
      format - the date format to use
      field - the Field to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter<T> createFieldWriter(String fieldName, int ordinal, long features, String format, String label, Field field, ObjectWriter initObjectWriter)
      Creates a FieldWriter for the specified field with comprehensive configuration.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use
      format - the date format to use
      label - the label for the field
      field - the Field to create a writer for
      initObjectWriter - the initial ObjectWriter to use
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public final <T> FieldWriter<T> createFieldWriter(ObjectWriterProvider provider, String fieldName, int ordinal, long features, String format, String label, Field field, ObjectWriter initObjectWriter)
      Creates a FieldWriter for the specified field with provider and comprehensive configuration.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      provider - the ObjectWriterProvider to use
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use
      format - the date format to use
      label - the label for the field
      field - the Field to create a writer for
      initObjectWriter - the initial ObjectWriter to use
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter<T> createFieldWriter(ObjectWriterProvider provider, String fieldName, int ordinal, long features, String format, Locale locale, String label, Field field, ObjectWriter initObjectWriter)
      Creates a FieldWriter for the specified field with locale and comprehensive configuration.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      provider - the ObjectWriterProvider to use
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use
      format - the date format to use
      locale - the locale to use
      label - the label for the field
      field - the Field to create a writer for
      initObjectWriter - the initial ObjectWriter to use
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter<T> createFieldWriter(ObjectWriterProvider provider, String fieldName, int ordinal, long features, String format, Locale locale, String label, Field field, ObjectWriter initObjectWriter, Class<?> contentAs)
      Creates a FieldWriter for the specified field with contentAs and comprehensive configuration.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      provider - the ObjectWriterProvider to use
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use
      format - the date format to use
      locale - the locale to use
      label - the label for the field
      field - the Field to create a writer for
      initObjectWriter - the initial ObjectWriter to use
      contentAs - the contentAs class
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter<T> createFieldWriter(Class<T> objectType, String fieldName, String dateFormat, Method method)
      Creates a FieldWriter for the specified method with default configuration.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      objectType - the class containing the method
      fieldName - the name of the field
      dateFormat - the date format to use
      method - the Method to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter<T> createFieldWriter(Class<T> objectType, String fieldName, int ordinal, long features, String format, Method method)
    • createFieldWriter

      public <T> FieldWriter<T> createFieldWriter(ObjectWriterProvider provider, Class<T> objectType, String fieldName, int ordinal, long features, String format, String label, Method method, ObjectWriter initObjectWriter)
      Creates a FieldWriter for the specified method with comprehensive configuration.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      provider - the ObjectWriterProvider to use
      objectType - the class containing the method
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use
      format - the date format to use
      label - the label for the field
      method - the Method to create a writer for
      initObjectWriter - the initial ObjectWriter to use
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter<T> createFieldWriter(ObjectWriterProvider provider, Class<T> objectType, String fieldName, int ordinal, long features, String format, Locale locale, String label, Method method, ObjectWriter initObjectWriter)
      Creates a FieldWriter for the specified method with locale and comprehensive configuration.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      provider - the ObjectWriterProvider to use
      objectType - the class containing the method
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use
      format - the date format to use
      locale - the locale to use
      label - the label for the field
      method - the Method to create a writer for
      initObjectWriter - the initial ObjectWriter to use
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter<T> createFieldWriter(ObjectWriterProvider provider, Class<T> objectType, String fieldName, int ordinal, long features, String format, Locale locale, String label, Method method, ObjectWriter initObjectWriter, Class<?> contentAs)
      Creates a FieldWriter for the specified method with contentAs and comprehensive configuration.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      provider - the ObjectWriterProvider to use
      objectType - the class containing the method
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use
      format - the date format to use
      locale - the locale to use
      label - the label for the field
      method - the Method to create a writer for
      initObjectWriter - the initial ObjectWriter to use
      contentAs - the contentAs class
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter createFieldWriter(String fieldName, ToLongFunction<T> function)
      Creates a FieldWriter for the specified function that returns a long value.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      fieldName - the name of the field
      function - the ToLongFunction to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter createFieldWriter(String fieldName, ToIntFunction<T> function)
      Creates a FieldWriter for the specified function that returns an int value.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      fieldName - the name of the field
      function - the ToIntFunction to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter createFieldWriter(String fieldName, Field field, Method method, ToIntFunction<T> function)
      Creates a FieldWriter for the specified field, method, and function that returns an int value.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      fieldName - the name of the field
      field - the Field to create a writer for
      method - the Method to create a writer for
      function - the ToIntFunction to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter createFieldWriter(String fieldName, ToShortFunction<T> function)
      Creates a FieldWriter for the specified function that returns a short value.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      fieldName - the name of the field
      function - the ToShortFunction to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter createFieldWriter(String fieldName, ToByteFunction<T> function)
      Creates a FieldWriter for the specified function that returns a byte value.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      fieldName - the name of the field
      function - the ToByteFunction to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter createFieldWriter(String fieldName, ToFloatFunction<T> function)
      Creates a FieldWriter for the specified function that returns a float value.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      fieldName - the name of the field
      function - the ToFloatFunction to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter createFieldWriter(String fieldName, ToDoubleFunction<T> function)
      Creates a FieldWriter for the specified function that returns a double value.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      fieldName - the name of the field
      function - the ToDoubleFunction to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter createFieldWriter(String fieldName, ToCharFunction<T> function)
      Creates a FieldWriter for the specified function that returns a char value.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      fieldName - the name of the field
      function - the ToCharFunction to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T> FieldWriter createFieldWriter(String fieldName, Predicate<T> function)
      Creates a FieldWriter for the specified predicate function that returns a boolean value.
      Type Parameters:
      T - the type of objects that the FieldWriter can serialize
      Parameters:
      fieldName - the name of the field
      function - the Predicate to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T, V> FieldWriter createFieldWriter(String fieldName, Class fieldClass, Function<T,V> function)
      Creates a FieldWriter for the specified function with default configuration.
      Type Parameters:
      T - the type of objects that owns the field
      V - the type of field values
      Parameters:
      fieldName - the name of the field
      fieldClass - the class of the field
      function - the Function to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T, V> FieldWriter createFieldWriter(String fieldName, Class fieldClass, Field field, Method method, Function<T,V> function)
      Creates a FieldWriter for the specified field, method, and function.
      Type Parameters:
      T - the type of objects that owns the field
      V - the type of field values
      Parameters:
      fieldName - the name of the field
      fieldClass - the class of the field
      field - the Field to create a writer for
      method - the Method to create a writer for
      function - the Function to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T, V> FieldWriter createFieldWriter(String fieldName, Type fieldType, Class fieldClass, Function<T,V> function)
      Creates a FieldWriter for the specified function with field type and class.
      Type Parameters:
      T - the type of objects that owns the field
      V - the type of field values
      Parameters:
      fieldName - the name of the field
      fieldType - the type of the field
      fieldClass - the class of the field
      function - the Function to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T, V> FieldWriter createFieldWriter(String fieldName, long features, String format, Class fieldClass, Function<T,V> function)
      Creates a FieldWriter for the specified function with features and format.
      Type Parameters:
      T - the type of objects that owns the field
      V - the type of field values
      Parameters:
      fieldName - the name of the field
      features - the features to use
      format - the date format to use
      fieldClass - the class of the field
      function - the Function to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T, V> FieldWriter<T> createFieldWriter(ObjectWriterProvider provider, Class<T> objectClass, String fieldName, int ordinal, long features, String format, String label, Type fieldType, Class<V> fieldClass, Method method, Function<T,V> function)
      Creates a FieldWriter for the specified function with provider, object class, and comprehensive configuration.
      Type Parameters:
      T - the type of objects that owns the field
      V - the type of field values
      Parameters:
      provider - the ObjectWriterProvider to use
      objectClass - the class containing the field
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use
      format - the date format to use
      label - the label for the field
      fieldType - the type of the field
      fieldClass - the class of the field
      method - the Method to create a writer for
      function - the Function to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T, V> FieldWriter<T> createFieldWriter(ObjectWriterProvider provider, Class<T> objectClass, String fieldName, int ordinal, long features, String format, String label, Type fieldType, Class<V> fieldClass, Field field, Method method, Function<T,V> function)
      Creates a FieldWriter for the specified function with provider, object class, field, method, and comprehensive configuration.
      Type Parameters:
      T - the type of objects that owns the field
      V - the type of field values
      Parameters:
      provider - the ObjectWriterProvider to use
      objectClass - the class containing the field
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use
      format - the date format to use
      label - the label for the field
      fieldType - the type of the field
      fieldClass - the class of the field
      field - the Field to create a writer for
      method - the Method to create a writer for
      function - the Function to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T, V> FieldWriter<T> createFieldWriter(ObjectWriterProvider provider, Class<T> objectClass, String fieldName, int ordinal, long features, String format, Locale locale, String label, Type fieldType, Class<V> fieldClass, Field field, Method method, Function<T,V> function)
      Creates a FieldWriter for the specified function with provider, object class, locale, and comprehensive configuration.
      Type Parameters:
      T - the type of objects that owns the field
      V - the type of field values
      Parameters:
      provider - the ObjectWriterProvider to use
      objectClass - the class containing the field
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use
      format - the date format to use
      locale - the locale to use
      label - the label for the field
      fieldType - the type of the field
      fieldClass - the class of the field
      field - the Field to create a writer for
      method - the Method to create a writer for
      function - the Function to create a writer for
      Returns:
      a FieldWriter instance
    • createFieldWriter

      public <T, V> FieldWriter<T> createFieldWriter(ObjectWriterProvider provider, Class<T> objectClass, String fieldName, int ordinal, long features, String format, Locale locale, String label, Type fieldType, Class<V> fieldClass, Field field, Method method, Function<T,V> function, Class<?> contentAs)
      Creates a FieldWriter for the specified function with provider, object class, contentAs, and comprehensive configuration.
      Type Parameters:
      T - the type of objects that owns the field
      V - the type of field values
      Parameters:
      provider - the ObjectWriterProvider to use
      objectClass - the class containing the field
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use
      format - the date format to use
      locale - the locale to use
      label - the label for the field
      fieldType - the type of the field
      fieldClass - the class of the field
      field - the Field to create a writer for
      method - the Method to create a writer for
      function - the Function to create a writer for
      contentAs - the contentAs class
      Returns:
      a FieldWriter instance
    • getInitWriter

      protected ObjectWriter getInitWriter(ObjectWriterProvider provider, Class fieldClass)