Class ObjectReaderProvider

java.lang.Object
com.alibaba.fastjson2.reader.ObjectReaderProvider
All Implemented Interfaces:
ObjectCodecProvider

public class ObjectReaderProvider extends Object implements ObjectCodecProvider
ObjectReaderProvider is responsible for providing and managing ObjectReader instances for deserializing JSON data into Java objects. It handles object creation, caching, type conversion, and auto-type support.

This provider supports various features including:

  • Object reader caching for performance optimization
  • Auto-type support with security controls
  • Type conversion between different Java types
  • Mixin support for modifying serialization behavior
  • Module-based extensibility

Example usage:

 // Get default provider
 ObjectReaderProvider provider = JSONFactory.getDefaultObjectReaderProvider();

 // Get object reader for a specific type
 ObjectReader<User> reader = provider.getObjectReader(User.class);

 // Parse JSON string using the reader
 User user = reader.readObject(JSONReader.of(jsonString));

 // Register custom type converter
 provider.registerTypeConvert(String.class, Integer.class, Integer::valueOf);
 
Since:
2.0.0
  • Field Details

    • SAFE_MODE

      public static final boolean SAFE_MODE
  • Constructor Details

    • ObjectReaderProvider

      public ObjectReaderProvider()
      Constructs an ObjectReaderProvider with the default ObjectReaderCreator based on system configuration. The creator selection follows this priority: 1. ASM creator (default, if not Android or GraalVM) 2. Reflection/Lambda creator (fallback)

      The provider is initialized with the base module and all registered modules.

    • ObjectReaderProvider

      public ObjectReaderProvider(ObjectReaderCreator creator)
      Constructs an ObjectReaderProvider with the specified ObjectReaderCreator.
      Parameters:
      creator - the ObjectReaderCreator to use for creating ObjectReader instances
  • Method Details

    • registerIfAbsent

      public void registerIfAbsent(long hashCode, ObjectReader objectReader)
      Registers an ObjectReader for the specified hash code if it is not already registered. This method handles both thread-local and global caching.
      Parameters:
      hashCode - the hash code for which to register the ObjectReader
      objectReader - the ObjectReader to register
    • addAutoTypeAccept

      public void addAutoTypeAccept(String name)
      Adds a type name to the auto-type accept list. Types in this list are allowed for auto-type deserialization.
      Parameters:
      name - the type name to add to the accept list
    • addAutoTypeDeny

      @Deprecated public void addAutoTypeDeny(String name)
      Deprecated.
    • getAutoTypeHandler

      public Consumer<Class> getAutoTypeHandler()
      Gets the auto-type handler that is invoked when a type is auto-resolved.
      Returns:
      the auto-type handler, or null if none is set
    • setAutoTypeHandler

      public void setAutoTypeHandler(Consumer<Class> autoTypeHandler)
      Sets the auto-type handler that will be invoked when a type is auto-resolved.
      Parameters:
      autoTypeHandler - the auto-type handler to set
    • 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
    • cleanupMixIn

      public void cleanupMixIn()
      Clears all mixin mappings.
    • 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/deserialization 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
    • registerSeeAlsoSubType

      public void registerSeeAlsoSubType(Class subTypeClass)
      Registers a subtype class for see-also support. This allows the provider to recognize and handle subtypes of the specified superclass.
      Parameters:
      subTypeClass - the subtype class to register
    • registerSeeAlsoSubType

      public void registerSeeAlsoSubType(Class subTypeClass, String subTypeClassName)
      Registers a subtype class with a specific name for see-also support.
      Parameters:
      subTypeClass - the subtype class to register
      subTypeClassName - the name of the subtype class, or null to use the class's simple name
      Throws:
      JSONException - if the superclass is null
    • register

      public ObjectReader register(Type type, ObjectReader objectReader, boolean fieldBased)
      Registers an ObjectReader for the specified type. If an ObjectReader is already registered for the type, it will be replaced.
      Parameters:
      type - the type for which to register the ObjectReader
      objectReader - the ObjectReader to register, or null to unregister
      fieldBased - whether the ObjectReader is field-based
      Returns:
      the previous ObjectReader for the type, or null if there was no previous ObjectReader
    • register

      public ObjectReader register(Type type, ObjectReader objectReader)
      Registers an ObjectReader for the specified type using method-based reading. If an ObjectReader is already registered for the type, it will be replaced.
      Parameters:
      type - the type for which to register the ObjectReader
      objectReader - the ObjectReader to register, or null to unregister
      Returns:
      the previous ObjectReader for the type, or null if there was no previous ObjectReader
    • registerIfAbsent

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

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

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

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

      public boolean unregisterObjectReader(Type type, ObjectReader reader)
      Unregisters the specified ObjectReader for the given type using method-based reading, but only if the currently registered reader matches the specified reader.
      Parameters:
      type - the type for which to unregister the ObjectReader
      reader - the ObjectReader to unregister
      Returns:
      true if the ObjectReader was unregistered, false otherwise
    • unregisterObjectReader

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

      public boolean register(ObjectReaderModule module)
      Registers an ObjectReaderModule. 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(ObjectReaderModule module)
      Unregisters an ObjectReaderModule.
      Parameters:
      module - the module to unregister
      Returns:
      true if the module was unregistered, false if it was not registered
    • cleanup

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

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

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

      public ObjectReaderCreator getCreator()
      Gets the ObjectReaderCreator 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 ObjectReaderCreator
    • getTypeConvert

      public Function getTypeConvert(Type from, Type to)
      Gets the type converter function that can convert values from one type to another.
      Parameters:
      from - the source type
      to - the target type
      Returns:
      the converter function for the type pair, or null if no converter is registered
    • registerTypeConvert

      public Function registerTypeConvert(Type from, Type to, Function typeConvert)
      Registers a type converter function that can convert values from one type to another.
      Parameters:
      from - the source type
      to - the target type
      typeConvert - the function to convert from source type to target type
      Returns:
      the previous converter function for the type pair, or null if there was no previous converter
    • getObjectReader

      public ObjectReader getObjectReader(long hashCode)
      Gets an ObjectReader by its hash code. This method first checks thread-local cache, then global cache for performance optimization.
      Parameters:
      hashCode - the hash code of the ObjectReader to retrieve
      Returns:
      the ObjectReader associated with the hash code, or null if not found
    • getObjectReader

      public ObjectReader getObjectReader(String typeName, Class<?> expectClass, long features)
      Gets an ObjectReader for the specified type name, expected class, and features. This method handles auto-type resolution and ObjectReader caching.
      Parameters:
      typeName - the name of the type
      expectClass - the expected class type
      features - the JSON reader features
      Returns:
      the ObjectReader for the specified type, or null if the type cannot be resolved
    • checkAutoType

      public Class<?> checkAutoType(String typeName, Class<?> expectClass, long features)
      Checks and resolves the class for auto-type support. This method handles security validation and class loading for auto-type deserialization.
      Parameters:
      typeName - the name of the type to check
      expectClass - the expected class type
      features - the JSON reader features
      Returns:
      the resolved Class, or null if the type cannot be resolved or is not allowed
      Throws:
      JSONException - if the type is not supported or security checks fail
    • getModules

      public List<ObjectReaderModule> getModules()
      Gets the list of registered ObjectReader modules.
      Returns:
      the list of modules
    • 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
    • getFieldInfo

      public void getFieldInfo(FieldInfo fieldInfo, Class objectClass, Field field)
      Gets field information for the specified field of a class.
      Parameters:
      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(FieldInfo fieldInfo, Class objectClass, Constructor constructor, int paramIndex, Parameter parameter)
      Gets field information for the specified constructor parameter.
      Parameters:
      fieldInfo - the FieldInfo object to populate with field information
      objectClass - the class containing the constructor
      constructor - the constructor containing the parameter
      paramIndex - the index of the parameter in the constructor
      parameter - the parameter for which to get information
    • getFieldInfo

      public void getFieldInfo(FieldInfo fieldInfo, Class objectClass, Method method, int paramIndex, Parameter parameter)
      Gets field information for the specified method parameter.
      Parameters:
      fieldInfo - the FieldInfo object to populate with field information
      objectClass - the class containing the method
      method - the method containing the parameter
      paramIndex - the index of the parameter in the method
      parameter - the parameter for which to get information
    • getObjectReader

      public ObjectReader getObjectReader(Type objectType)
      Gets an ObjectReader for the specified type. If an ObjectReader for the type is already cached, it will be returned directly. Otherwise, a new ObjectReader will be created and cached.
      Parameters:
      objectType - the type for which to get an ObjectReader
      Returns:
      the ObjectReader for the specified type
    • createValueConsumerCreator

      public Function<Consumer,ByteArrayValueConsumer> createValueConsumerCreator(Class objectClass, FieldReader[] fieldReaderArray)
      Creates a value consumer creator for byte array values.
      Parameters:
      objectClass - the class for which to create the value consumer creator
      fieldReaderArray - the field readers to use
      Returns:
      a function that creates ByteArrayValueConsumer instances
    • createCharArrayValueConsumerCreator

      public Function<Consumer,CharArrayValueConsumer> createCharArrayValueConsumerCreator(Class objectClass, FieldReader[] fieldReaderArray)
      Creates a value consumer creator for char array values.
      Parameters:
      objectClass - the class for which to create the value consumer creator
      fieldReaderArray - the field readers to use
      Returns:
      a function that creates CharArrayValueConsumer instances
    • getObjectReader

      public ObjectReader getObjectReader(Type objectType, boolean fieldBased)
      Gets an ObjectReader for the specified type with field-based option. If an ObjectReader for the type is already cached, it will be returned directly. Otherwise, a new ObjectReader will be created and cached.
      Parameters:
      objectType - the type for which to get an ObjectReader
      fieldBased - whether to use field-based reading (true) or method-based reading (false)
      Returns:
      the ObjectReader for the specified type
    • getAutoTypeBeforeHandler

      public JSONReader.AutoTypeBeforeHandler getAutoTypeBeforeHandler()
      Gets the auto-type before handler that is invoked before type resolution.
      Returns:
      the auto-type before handler, or null if none is set
    • setAutoTypeBeforeHandler

      public void setAutoTypeBeforeHandler(JSONReader.AutoTypeBeforeHandler autoTypeBeforeHandler)
      Sets the auto-type before handler that will be invoked before type resolution.
      Parameters:
      autoTypeBeforeHandler - the auto-type before handler to set
    • getAutoTypeList

      public Map<String,Date> getAutoTypeList()
    • getFieldInfo

      public void getFieldInfo(FieldInfo fieldInfo, Class objectClass, Method method)
      Gets field information for the specified method of a class. This method also handles setter methods by attempting to find corresponding fields.
      Parameters:
      fieldInfo - the FieldInfo object to populate with field information
      objectClass - the class containing the method
      method - the method for which to get information
    • createObjectCreator

      public <T> Supplier<T> createObjectCreator(Class<T> objectClass, long readerFeatures)
      Creates an object creator (supplier) for the specified class and reader features.
      Type Parameters:
      T - the type of the object
      Parameters:
      objectClass - the class for which to create an object creator
      readerFeatures - the reader features to use
      Returns:
      a supplier function that creates new instances of the object
      Throws:
      JSONException - if no default constructor is found for the class
    • createFieldReader

      public FieldReader createFieldReader(Class objectClass, String fieldName, long readerFeatures)
      Creates a FieldReader for the specified class, field name, and reader features.
      Parameters:
      objectClass - the class containing the field
      fieldName - the name of the field
      readerFeatures - the reader features to use
      Returns:
      a FieldReader for the specified field, or null if the field is not found
    • createObjectReader

      public <T> ObjectReader<T> createObjectReader(String[] names, Type[] types, Supplier<T> supplier, FieldConsumer<T> c)
      Creates an ObjectReader for a custom object with specified field names, types, and consumer.
      Type Parameters:
      T - the type of the object
      Parameters:
      names - the field names
      types - the field types
      supplier - the supplier function to create new instances of the object
      c - the field consumer to set field values
      Returns:
      the created ObjectReader
    • createObjectReader

      public <T> ObjectReader<T> createObjectReader(String[] names, Type[] types, long[] features, Supplier<T> supplier, FieldConsumer<T> c)
      Creates an ObjectReader for a custom object with specified field names, types, features, and consumer.
      Type Parameters:
      T - the type of the object
      Parameters:
      names - the field names
      types - the field types
      features - the field features (can be null)
      supplier - the supplier function to create new instances of the object
      c - the field consumer to set field values
      Returns:
      the created ObjectReader
    • 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
    • isDisableSmartMatch

      public boolean isDisableSmartMatch()
      Checks if smart match is disabled.
      Returns:
      true if smart match is disabled, false otherwise
    • setDisableSmartMatch

      public void setDisableSmartMatch(boolean disableSmartMatch)
      Sets whether smart match is disabled.
      Parameters:
      disableSmartMatch - true to disable smart match, false to enable it
    • getNamingStrategy

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

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