Class ObjectReaderCreator

java.lang.Object
com.alibaba.fastjson2.reader.ObjectReaderCreator
Direct Known Subclasses:
ObjectReaderCreatorASM

public class ObjectReaderCreator extends Object
ObjectReaderCreator is responsible for creating ObjectReader instances for deserializing JSON data into Java objects. It provides factory methods for creating ObjectReaders for various types of objects and fields.

This class supports various features including:

  • Creation of ObjectReaders for different object types
  • Creation of FieldReaders for different field types
  • Lambda expression support for setter methods
  • Custom field reader creation with various configurations
  • JIT compilation support for improved performance

Example usage:

 // Get default creator
 ObjectReaderCreator creator = JSONFactory.getDefaultObjectReaderCreator();

 // Create ObjectReader for a class
 ObjectReader<User> reader = creator.createObjectReader(User.class);

 // Create FieldReader for a field
 Field field = User.class.getDeclaredField("name");
 FieldReader<User> fieldReader = creator.createFieldReader("name", String.class, field);
 
Since:
2.0.0
  • Field Details

    • JIT

      public static final boolean JIT
    • INSTANCE

      public static final ObjectReaderCreator INSTANCE
    • jitErrorCount

      protected final AtomicInteger jitErrorCount
    • jitErrorLast

      protected volatile Throwable jitErrorLast
    • methodTypeMapping

      protected static final Map<Class,com.alibaba.fastjson2.reader.ObjectReaderCreator.LambdaSetterInfo> methodTypeMapping
  • Constructor Details

    • ObjectReaderCreator

      public ObjectReaderCreator()
  • Method Details

    • createObjectReaderNoneDefaultConstructor

      public <T> ObjectReader<T> createObjectReaderNoneDefaultConstructor(Constructor constructor, String... paramNames)
    • createObjectReaderNoneDefaultConstructor

      public <T> ObjectReader<T> createObjectReaderNoneDefaultConstructor(Class objectClass, Function<Map<Long,Object>,T> creator, FieldReader... fieldReaders)
    • createObjectReaderFactoryMethod

      public <T> ObjectReader<T> createObjectReaderFactoryMethod(Method factoryMethod, String... paramNames)
    • createFieldReaders

      public FieldReader[] createFieldReaders(ObjectReaderProvider provider, Class objectClass, Type objectType, Executable owner, Parameter[] parameters, String... paramNames)
    • createFactoryFunction

      public <T> Function<Map<Long,Object>,T> createFactoryFunction(Method factoryMethod, String... paramNames)
      Creates a Function that can instantiate objects using the specified factory method and parameter names.
      Type Parameters:
      T - the type of objects that the Function can create
      Parameters:
      factoryMethod - the factory method to use for creating instances
      paramNames - the parameter names to use for the factory method
      Returns:
      a Function that can create new instances of the specified type using the factory method
    • createFunction

      public <T> Function<Map<Long,Object>,T> createFunction(Constructor constructor, String... paramNames)
      Creates a Function that can instantiate objects using the specified constructor and parameter names.
      Type Parameters:
      T - the type of objects that the Function can create
      Parameters:
      constructor - the constructor to use for creating instances
      paramNames - the parameter names to use for the constructor
      Returns:
      a Function that can create new instances of the specified type
    • createFunction

      public <T> Function<Map<Long,Object>,T> createFunction(Constructor constructor, Constructor markerConstructor, String... paramNames)
      Creates a Function that can instantiate objects using the specified constructor, marker constructor, and parameter names.
      Type Parameters:
      T - the type of objects that the Function can create
      Parameters:
      constructor - the constructor to use for creating instances
      markerConstructor - the marker constructor to use
      paramNames - the parameter names to use for the constructor
      Returns:
      a Function that can create new instances of the specified type
    • createObjectReader

      public <T> ObjectReader<T> createObjectReader(Class<T> objectClass, FieldReader... fieldReaders)
      Creates an ObjectReader for the specified object type with the given field readers.
      Type Parameters:
      T - the type of objects that the ObjectReader can deserialize
      Parameters:
      objectClass - the class of objects to deserialize
      fieldReaders - the field readers to use for deserialization
      Returns:
      an ObjectReader instance for the specified type
    • createObjectReader

      public <T> ObjectReader<T> createObjectReader(Class<T> objectClass, Supplier<T> defaultCreator, FieldReader... fieldReaders)
      Creates an ObjectReader for the specified object type with a default creator and field readers.
      Type Parameters:
      T - the type of objects that the ObjectReader can deserialize
      Parameters:
      objectClass - the class of objects to deserialize
      defaultCreator - the supplier function to create new instances of the object
      fieldReaders - the field readers to use for deserialization
      Returns:
      an ObjectReader instance for the specified type
    • createObjectReaderSeeAlso

      public <T> ObjectReader<T> createObjectReaderSeeAlso(Class<T> objectType, Class[] seeAlso, FieldReader... fieldReaders)
      Creates an ObjectReader for the specified object type with see-also support.
      Type Parameters:
      T - the type of objects that the ObjectReader can deserialize
      Parameters:
      objectType - the class of objects to deserialize
      seeAlso - the see-also classes
      fieldReaders - the field readers to use for deserialization
      Returns:
      an ObjectReader instance for the specified type
    • createObjectReaderSeeAlso

      public <T> ObjectReader<T> createObjectReaderSeeAlso(Class<T> objectClass, String typeKey, Class[] seeAlso, String[] seeAlsoNames, FieldReader... fieldReaders)
      Creates an ObjectReader for the specified object type with see-also support and custom type key.
      Type Parameters:
      T - the type of objects that the ObjectReader can deserialize
      Parameters:
      objectClass - the class of objects to deserialize
      typeKey - the type key to use
      seeAlso - the see-also classes
      seeAlsoNames - the see-also class names
      fieldReaders - the field readers to use for deserialization
      Returns:
      an ObjectReader instance for the specified type
    • createObjectReaderSeeAlso

      public <T> ObjectReader<T> createObjectReaderSeeAlso(Class<T> objectClass, String typeKey, Class[] seeAlso, String[] seeAlsoNames, Class seeAlsoDefault, FieldReader... fieldReaders)
      Creates an ObjectReader for the specified object type with see-also support, custom type key, and default class.
      Type Parameters:
      T - the type of objects that the ObjectReader can deserialize
      Parameters:
      objectClass - the class of objects to deserialize
      typeKey - the type key to use
      seeAlso - the see-also classes
      seeAlsoNames - the see-also class names
      seeAlsoDefault - the default see-also class
      fieldReaders - the field readers to use for deserialization
      Returns:
      an ObjectReader instance for the specified type
      Since:
      2.0.24
    • createObjectReaderSeeAlso

      public <T> ObjectReader<T> createObjectReaderSeeAlso(Class<T> objectType, Supplier<T> defaultCreator, String typeKey, Class[] seeAlso, String[] seeAlsoNames, FieldReader... fieldReaders)
      Creates an ObjectReader for the specified object type with see-also support, custom creator, and type key.
      Type Parameters:
      T - the type of objects that the ObjectReader can deserialize
      Parameters:
      objectType - the class of objects to deserialize
      defaultCreator - the supplier function to create new instances of the object
      typeKey - the type key to use
      seeAlso - the see-also classes
      seeAlsoNames - the see-also class names
      fieldReaders - the field readers to use for deserialization
      Returns:
      an ObjectReader instance for the specified type
    • createObjectReaderWithBuilder

      protected <T> ObjectReader<T> createObjectReaderWithBuilder(Class<T> objectClass, Type objectType, ObjectReaderProvider provider, BeanInfo beanInfo)
    • createObjectReaderWithCreator

      protected <T> ObjectReader<T> createObjectReaderWithCreator(Class<T> objectClass, Type objectType, ObjectReaderProvider provider, BeanInfo beanInfo)
    • createObjectReader

      public <T> ObjectReader<T> createObjectReader(Class<T> objectClass, long features, Supplier<T> defaultCreator, Function buildFunction, FieldReader... fieldReaders)
      Creates an ObjectReader for the specified object type with comprehensive configuration.
      Type Parameters:
      T - the type of objects that the ObjectReader can deserialize
      Parameters:
      objectClass - the class of objects to deserialize
      features - the features to use for deserialization
      defaultCreator - the supplier function to create new instances of the object
      buildFunction - the build function to use
      fieldReaders - the field readers to use for deserialization
      Returns:
      an ObjectReader instance for the specified type
    • createObjectReader

      public <T> ObjectReader<T> createObjectReader(Class<T> objectClass, String typeKey, long features, JSONSchema schema, Supplier<T> defaultCreator, Function buildFunction, FieldReader... fieldReaders)
      Creates an ObjectReader for the specified object type with type key, features, schema, and comprehensive configuration.
      Type Parameters:
      T - the type of objects that the ObjectReader can deserialize
      Parameters:
      objectClass - the class of objects to deserialize
      typeKey - the type key to use
      features - the features to use for deserialization
      schema - the JSON schema to use
      defaultCreator - the supplier function to create new instances of the object
      buildFunction - the build function to use
      fieldReaders - the field readers to use for deserialization
      Returns:
      an ObjectReader instance for the specified type
    • createObjectReader

      public <T> ObjectReader<T> createObjectReader(Class<T> objectClass, String typeKey, String rootName, long features, JSONSchema schema, Supplier<T> defaultCreator, Function buildFunction, FieldReader... fieldReaders)
      Creates an ObjectReader for the specified object type with comprehensive configuration including root name.
      Type Parameters:
      T - the type of objects that the ObjectReader can deserialize
      Parameters:
      objectClass - the class of objects to deserialize
      typeKey - the type key to use
      rootName - the root name to use
      features - the features to use for deserialization
      schema - the JSON schema to use
      defaultCreator - the supplier function to create new instances of the object
      buildFunction - the build function to use
      fieldReaders - the field readers to use for deserialization
      Returns:
      an ObjectReader instance for the specified type
    • createObjectReader

      public <T> ObjectReader<T> createObjectReader(Type objectType)
    • createObjectReader

      public <T> ObjectReader<T> createObjectReader(Class<T> objectType)
    • createObjectReader

      public <T> ObjectReader<T> createObjectReader(Class<T> objectType, boolean fieldBased)
    • createObjectReader

      public <T> ObjectReader<T> createObjectReader(Class<T> objectClass, Type objectType, boolean fieldBased, ObjectReaderProvider provider)
    • createNoneDefaultConstructorObjectReader

      protected <T> ObjectReaderNoneDefaultConstructor createNoneDefaultConstructorObjectReader(Class objectClass, BeanInfo beanInfo, Function<Map<Long,Object>,T> constructorFunction, List<Constructor> alternateConstructors, String[] parameterNames, FieldReader[] paramFieldReaders, FieldReader[] fieldReaderArray)
    • createFieldReaders

      public <T> FieldReader[] createFieldReaders(Class<T> objectClass)
    • createFieldReaders

      public <T> FieldReader[] createFieldReaders(Class<T> objectClass, Type objectType)
    • createFieldReader

      protected void createFieldReader(Class objectClass, Type objectType, String namingStrategy, String[] orders, FieldInfo fieldInfo, Field field, Map<String,List<FieldReader>> fieldReaders, ObjectReaderProvider provider)
    • createFieldReader

      protected void createFieldReader(Class objectClass, Type objectType, String namingStrategy, String[] orders, BeanInfo beanInfo, FieldInfo fieldInfo, Method method, Map<String,List<FieldReader>> fieldReaders, ObjectReaderProvider provider)
    • createFieldReaders

      protected <T> FieldReader[] createFieldReaders(Class<T> objectClass, Type objectType, BeanInfo beanInfo, boolean fieldBased, ObjectReaderProvider provider)
    • createSupplier

      public <T> Supplier<T> createSupplier(Class<T> objectClass)
      Creates a Supplier function for the specified object class that can create new instances.
      Type Parameters:
      T - the type of objects that the Supplier can create
      Parameters:
      objectClass - the class of objects to create
      Returns:
      a Supplier function that can create new instances of the specified class, or null if creation is not possible
    • createSupplier

      public <T> Supplier<T> createSupplier(Constructor<T> constructor)
      Creates a Supplier function for the specified constructor that can create new instances.
      Type Parameters:
      T - the type of objects that the Supplier can create
      Parameters:
      constructor - the constructor to use for creating instances
      Returns:
      a Supplier function that can create new instances using the specified constructor
    • createSupplier

      public <T> Supplier<T> createSupplier(Constructor constructor, boolean jit)
      Creates a Supplier function for the specified constructor with JIT compilation option.
      Type Parameters:
      T - the type of objects that the Supplier can create
      Parameters:
      constructor - the constructor to use for creating instances
      jit - whether to use JIT compilation for improved performance
      Returns:
      a Supplier function that can create new instances using the specified constructor
    • createIntFunction

      protected <T> IntFunction<T> createIntFunction(Constructor constructor)
    • createIntFunction

      protected <T> IntFunction<T> createIntFunction(Method factoryMethod)
    • createStringFunction

      protected <T> Function<String,T> createStringFunction(Constructor constructor)
    • createStringFunction

      protected <T> Function<String,T> createStringFunction(Method factoryMethod)
    • createValueFunction

      protected <I, T> Function<I,T> createValueFunction(Constructor<T> constructor, Class<I> valueClass)
    • createValueFunction

      protected <I, T> Function<I,T> createValueFunction(Method factoryMethod, Class valueClass)
    • createBuildFunction

      public <T, R> Function<T,R> createBuildFunction(Method builderMethod)
      Creates a Function that can build objects using the specified builder method.
      Type Parameters:
      T - the type of objects that the Function can build
      R - the return type of the builder method
      Parameters:
      builderMethod - the builder method to use for building objects
      Returns:
      a Function that can build objects using the specified builder method
    • createFieldReader

      public <T> FieldReader createFieldReader(Class<T> objectType, String fieldName, Type fieldType, Class fieldClass, Method method)
      Creates a FieldReader for the specified field with default configuration.
      Type Parameters:
      T - the type of objects that contain the field
      Parameters:
      objectType - the class containing the field
      fieldName - the name of the field
      fieldType - the type of the field
      fieldClass - the class of the field
      method - the method to use for reading the field
      Returns:
      a FieldReader instance for the specified field
    • createFieldReader

      public <T> FieldReader createFieldReader(Class<T> objectType, String fieldName, String format, Type fieldType, Class fieldClass, Method method)
      Creates a FieldReader for the specified field with format configuration.
      Type Parameters:
      T - the type of objects that contain the field
      Parameters:
      objectType - the class containing the field
      fieldName - the name of the field
      format - the date format to use for the field
      fieldType - the type of the field
      fieldClass - the class of the field
      method - the method to use for reading the field
      Returns:
      a FieldReader instance for the specified field
    • createFieldReaderMethod

      public <T> FieldReader createFieldReaderMethod(Class<T> objectType, String fieldName, String format, Type fieldType, Class fieldClass, Method method)
      Creates a FieldReader for the specified method with default configuration.
      Type Parameters:
      T - the type of objects that contain the field
      Parameters:
      objectType - the class containing the field
      fieldName - the name of the field
      format - the date format to use for the field
      fieldType - the type of the field
      fieldClass - the class of the field
      method - the method to use for reading the field
      Returns:
      a FieldReader instance for the specified field
    • createFieldReaderParam

      public <T> FieldReader createFieldReaderParam(Class<T> objectClass, Type objectType, String fieldName, int ordinal, long features, String format, Type fieldType, Class fieldClass, String paramName, Class declaringClass, Parameter parameter, JSONSchema schema)
      Creates a FieldReader for the specified parameter with default configuration.
      Type Parameters:
      T - the type of objects that contain the field
      Parameters:
      objectClass - the class containing the field
      objectType - the type of the object
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use for the field
      format - the date format to use for the field
      fieldType - the type of the field
      fieldClass - the class of the field
      paramName - the name of the parameter
      declaringClass - the declaring class of the parameter
      parameter - the parameter to create a reader for
      schema - the JSON schema to use
      Returns:
      a FieldReader instance for the specified parameter
    • createFieldReaderParam

      public <T> FieldReader createFieldReaderParam(Class<T> objectClass, Type objectType, String fieldName, int ordinal, long features, String format, Type fieldType, Class fieldClass, String paramName, Class declaringClass, Parameter parameter, JSONSchema schema, ObjectReader initReader)
      Creates a FieldReader for the specified parameter with initialization reader.
      Type Parameters:
      T - the type of objects that contain the field
      Parameters:
      objectClass - the class containing the field
      objectType - the type of the object
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use for the field
      format - the date format to use for the field
      fieldType - the type of the field
      fieldClass - the class of the field
      paramName - the name of the parameter
      declaringClass - the declaring class of the parameter
      parameter - the parameter to create a reader for
      schema - the JSON schema to use
      initReader - the initialization reader to use
      Returns:
      a FieldReader instance for the specified parameter
    • createFieldReaderParam

      public <T> FieldReader createFieldReaderParam(Class<T> objectClass, Type objectType, String fieldName, int ordinal, long features, String format, Locale locale, Object defaultValue, Type fieldType, Class fieldClass, String paramName, Class declaringClass, Parameter parameter, JSONSchema schema, ObjectReader initReader)
      Creates a FieldReader for the specified parameter with comprehensive configuration.
      Type Parameters:
      T - the type of objects that contain the field
      Parameters:
      objectClass - the class containing the field
      objectType - the type of the object
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use for the field
      format - the date format to use for the field
      locale - the locale to use for the field
      defaultValue - the default value for the field
      fieldType - the type of the field
      fieldClass - the class of the field
      paramName - the name of the parameter
      declaringClass - the declaring class of the parameter
      parameter - the parameter to create a reader for
      schema - the JSON schema to use for the field
      initReader - the initialization reader to use
      Returns:
      a FieldReader instance for the specified parameter
    • createFieldReaderMethod

      public <T> FieldReader createFieldReaderMethod(Class<T> objectClass, Type objectType, String fieldName, int ordinal, long features, String format, Locale locale, Object defaultValue, String schema, Type fieldType, Class fieldClass, Method method, ObjectReader initReader)
      Creates a FieldReader for the specified method with comprehensive configuration.
      Type Parameters:
      T - the type of objects that contain the field
      Parameters:
      objectClass - the class containing the field
      objectType - the type of the object
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use for the field
      format - the date format to use for the field
      locale - the locale to use for the field
      defaultValue - the default value for the field
      schema - the schema to use for the field
      fieldType - the type of the field
      fieldClass - the class of the field
      method - the method to use for reading the field
      initReader - the initialization reader to use
      Returns:
      a FieldReader instance for the specified method
    • createFieldReaderMethod

      public <T> FieldReader createFieldReaderMethod(Class<T> objectClass, Type objectType, String fieldName, int ordinal, long features, String format, Locale locale, Object defaultValue, String schema, Type fieldType, Class fieldClass, Method method, ObjectReader initReader, String keyName, BiConsumer arrayToMapDuplicateHandler)
      Creates a FieldReader for the specified method with comprehensive configuration including array-to-map options.
      Type Parameters:
      T - the type of objects that contain the field
      Parameters:
      objectClass - the class containing the field
      objectType - the type of the object
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use for the field
      format - the date format to use for the field
      locale - the locale to use for the field
      defaultValue - the default value for the field
      schema - the schema to use for the field
      fieldType - the type of the field
      fieldClass - the class of the field
      method - the method to use for reading the field
      initReader - the initialization reader to use
      keyName - the key name for array-to-map conversion
      arrayToMapDuplicateHandler - the duplicate handler for array-to-map conversion
      Returns:
      a FieldReader instance for the specified method
    • createFieldReader

      public <T> FieldReader<T> createFieldReader(String fieldName, Type fieldType, Field field)
    • createFieldReader

      public <T> FieldReader<T> createFieldReader(String fieldName, Field field)
    • createFieldReader

      public <T> FieldReader createFieldReader(String fieldName, Method method)
      Creates a FieldReader for the specified method with minimal configuration.
      Type Parameters:
      T - the type of objects that the FieldReader can deserialize
      Parameters:
      fieldName - the name of the field
      method - the method to create a reader for
      Returns:
      a FieldReader instance for the specified method
      Throws:
      JSONException - if the method has an illegal number of parameters
    • createFieldReader

      public <T> FieldReader<T> createFieldReader(String fieldName, String format, Type fieldType, Field field)
    • createFieldReader

      public <T> FieldReader<T> createFieldReader(Class objectClass, Type objectType, String fieldName, long features, String format, Type fieldType, Class fieldClass, Field field)
    • createFieldReader

      public <T> FieldReader<T> createFieldReader(Class objectClass, Type objectType, String fieldName, int ordinal, long features, String format, Locale locale, Object defaultValue, String schema, Type fieldType, Class fieldClass, Field field, ObjectReader initReader)
    • createFieldReader

      public <T> FieldReader<T> createFieldReader(Class objectClass, Type objectType, String fieldName, int ordinal, long features, String format, Locale locale, Object defaultValue, String schema, Type fieldType, Class fieldClass, Field field, ObjectReader initReader, String keyName, BiConsumer arrayToMapDuplicateHandler)
    • createFieldReader

      public <T, V> FieldReader createFieldReader(String fieldName, Type fieldType, Class<V> fieldClass, long features, BiConsumer<T,V> function)
    • createFieldReader

      public <T, V> FieldReader createFieldReader(String fieldName, Type fieldType, Class<V> fieldClass, Method method, BiConsumer<T,V> function)
    • createFieldReader

      public <T, V> FieldReader createFieldReader(Class objectClass, Type objectType, String fieldName, Type fieldType, Class<V> fieldClass, int ordinal, long features, String format, Locale locale, Object defaultValue, JSONSchema schema, Method method, BiConsumer<T,V> function, ObjectReader initReader)
    • createEnumReader

      protected ObjectReader createEnumReader(Class objectClass, Method createMethod, ObjectReaderProvider provider)
    • createFieldReaderLambda

      protected <T> FieldReader createFieldReaderLambda(Class<T> objectClass, Type objectType, String fieldName, int ordinal, long features, String format, Locale locale, Object defaultValue, String schema, Type fieldType, Class fieldClass, Method method, ObjectReader initReader)
      Creates a FieldReader using lambda expressions for improved performance.
      Type Parameters:
      T - the type of objects that the FieldReader can deserialize
      Parameters:
      objectClass - the class containing the field
      objectType - the type of the object
      fieldName - the name of the field
      ordinal - the ordinal position of the field
      features - the features to use for deserialization
      format - the date format to use for the field
      locale - the locale to use for the field
      defaultValue - the default value for the field
      schema - the JSON schema to use for the field
      fieldType - the type of the field
      fieldClass - the class of the field
      method - the method to use for reading the field
      initReader - the initialization reader to use
      Returns:
      a FieldReader instance for the specified field
    • lambdaSetter

      protected Object lambdaSetter(Class objectClass, Class fieldClass, Method method)
    • createByteArrayValueConsumerCreator

      public Function<Consumer,ByteArrayValueConsumer> createByteArrayValueConsumerCreator(Class objectClass, FieldReader[] fieldReaderArray)
    • createCharArrayValueConsumerCreator

      public Function<Consumer,CharArrayValueConsumer> createCharArrayValueConsumerCreator(Class objectClass, FieldReader[] fieldReaderArray)