Package com.alibaba.fastjson2.reader
Class ObjectReaderProvider
java.lang.Object
com.alibaba.fastjson2.reader.ObjectReaderProvider
- All Implemented Interfaces:
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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an ObjectReaderProvider with the default ObjectReaderCreator based on system configuration.ObjectReaderProvider(ObjectReaderCreator creator) Constructs an ObjectReaderProvider with the specified ObjectReaderCreator. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAutoTypeAccept(String name) Adds a type name to the auto-type accept list.voidaddAutoTypeDeny(String name) Deprecated.Class<?> checkAutoType(String typeName, Class<?> expectClass, long features) Checks and resolves the class for auto-type support.voidCleans up cached ObjectReaders and mixin mappings associated with the specified class.voidcleanup(ClassLoader classLoader) Cleans up cached ObjectReaders associated with the specified ClassLoader.voidClears all mixin mappings.voidclear()Clears all cached ObjectReaders and mixin mappings.createCharArrayValueConsumerCreator(Class objectClass, FieldReader[] fieldReaderArray) Creates a value consumer creator for char array values.createFieldReader(Class objectClass, String fieldName, long readerFeatures) Creates a FieldReader for the specified class, field name, and reader features.<T> Supplier<T> createObjectCreator(Class<T> objectClass, long readerFeatures) Creates an object creator (supplier) for the specified class and reader features.<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.<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.createValueConsumerCreator(Class objectClass, FieldReader[] fieldReaderArray) Creates a value consumer creator for byte array values.Gets the auto-type before handler that is invoked before type resolution.Gets the auto-type handler that is invoked when a type is auto-resolved.voidgetBeanInfo(BeanInfo beanInfo, Class objectClass) Gets bean information for the specified class by delegating to registered modules.Gets the ObjectReaderCreator used by this provider.voidgetFieldInfo(FieldInfo fieldInfo, Class objectClass, Constructor constructor, int paramIndex, Parameter parameter) Gets field information for the specified constructor parameter.voidgetFieldInfo(FieldInfo fieldInfo, Class objectClass, Field field) Gets field information for the specified field of a class.voidgetFieldInfo(FieldInfo fieldInfo, Class objectClass, Method method) Gets field information for the specified method of a class.voidgetFieldInfo(FieldInfo fieldInfo, Class objectClass, Method method, int paramIndex, Parameter parameter) Gets field information for the specified method parameter.Gets the mixin source class for the specified target class.Gets the list of registered ObjectReader modules.Gets the property naming strategy used by this provider.getObjectReader(long hashCode) Gets an ObjectReader by its hash code.getObjectReader(Type objectType) Gets an ObjectReader for the specified type.getObjectReader(Type objectType, boolean fieldBased) Gets an ObjectReader for the specified type with field-based option.getObjectReader(String typeName, Class<?> expectClass, long features) Gets an ObjectReader for the specified type name, expected class, and features.getTypeConvert(Type from, Type to) Gets the type converter function that can convert values from one type to another.booleanChecks if array mapping is disabled.booleanChecks if auto-type support is disabled.booleanChecks if JSONB support is disabled.booleanChecks if reference detection is disabled.booleanChecks if smart match is disabled.voidRegisters a mixin mapping between a target class and a mixin source class.booleanregister(ObjectReaderModule module) Registers an ObjectReaderModule.register(Type type, ObjectReader objectReader) Registers an ObjectReader for the specified type using method-based reading.register(Type type, ObjectReader objectReader, boolean fieldBased) Registers an ObjectReader for the specified type.voidregisterIfAbsent(long hashCode, ObjectReader objectReader) Registers an ObjectReader for the specified hash code if it is not already registered.registerIfAbsent(Type type, ObjectReader objectReader) Registers an ObjectReader for the specified type using method-based reading if it is not already registered.registerIfAbsent(Type type, ObjectReader objectReader, boolean fieldBased) Registers an ObjectReader for the specified type if it is not already registered.voidregisterSeeAlsoSubType(Class subTypeClass) Registers a subtype class for see-also support.voidregisterSeeAlsoSubType(Class subTypeClass, String subTypeClassName) Registers a subtype class with a specific name for see-also support.registerTypeConvert(Type from, Type to, Function typeConvert) Registers a type converter function that can convert values from one type to another.voidsetAutoTypeBeforeHandler(JSONReader.AutoTypeBeforeHandler autoTypeBeforeHandler) Sets the auto-type before handler that will be invoked before type resolution.voidsetAutoTypeHandler(Consumer<Class> autoTypeHandler) Sets the auto-type handler that will be invoked when a type is auto-resolved.voidsetDisableArrayMapping(boolean disableArrayMapping) Sets whether array mapping is disabled.voidsetDisableAutoType(boolean disableAutoType) Sets whether auto-type support is disabled.voidsetDisableJSONB(boolean disableJSONB) Sets whether JSONB support is disabled.voidsetDisableReferenceDetect(boolean disableReferenceDetect) Sets whether reference detection is disabled.voidsetDisableSmartMatch(boolean disableSmartMatch) Sets whether smart match is disabled.voidsetNamingStrategy(PropertyNamingStrategy namingStrategy) Sets the property naming strategy used by this provider.booleanunregister(ObjectReaderModule module) Unregisters an ObjectReaderModule.unregisterObjectReader(Type type) Unregisters the ObjectReader for the specified type using method-based reading.unregisterObjectReader(Type type, boolean fieldBased) Unregisters the ObjectReader for the specified type.booleanunregisterObjectReader(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.booleanunregisterObjectReader(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.
-
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
Constructs an ObjectReaderProvider with the specified ObjectReaderCreator.- Parameters:
creator- the ObjectReaderCreator to use for creating ObjectReader instances
-
-
Method Details
-
registerIfAbsent
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 ObjectReaderobjectReader- the ObjectReader to register
-
addAutoTypeAccept
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. -
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
Sets the auto-type handler that will be invoked when a type is auto-resolved.- Parameters:
autoTypeHandler- the auto-type handler to set
-
getMixIn
Gets the mixin source class for the specified target class.- Specified by:
getMixInin interfaceObjectCodecProvider- 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
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 appliedmixinSource- the source class from which annotations will be copied, or null to remove the mixin
-
registerSeeAlsoSubType
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
Registers a subtype class with a specific name for see-also support.- Parameters:
subTypeClass- the subtype class to registersubTypeClassName- the name of the subtype class, or null to use the class's simple name- Throws:
JSONException- if the superclass is null
-
register
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 ObjectReaderobjectReader- the ObjectReader to register, or null to unregisterfieldBased- whether the ObjectReader is field-based- Returns:
- the previous ObjectReader for the type, or null if there was no previous ObjectReader
-
register
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 ObjectReaderobjectReader- the ObjectReader to register, or null to unregister- Returns:
- the previous ObjectReader for the type, or null if there was no previous ObjectReader
-
registerIfAbsent
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 ObjectReaderobjectReader- the ObjectReader to register- Returns:
- the previous ObjectReader for the type, or null if there was no previous ObjectReader
-
registerIfAbsent
Registers an ObjectReader for the specified type if it is not already registered.- Parameters:
type- the type for which to register the ObjectReaderobjectReader- the ObjectReader to registerfieldBased- whether the ObjectReader is field-based- Returns:
- the previous ObjectReader for the type, or null if there was no previous ObjectReader
-
unregisterObjectReader
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
Unregisters the ObjectReader for the specified type.- Parameters:
type- the type for which to unregister the ObjectReaderfieldBased- whether the ObjectReader is field-based- Returns:
- the unregistered ObjectReader, or null if there was no ObjectReader for the type
-
unregisterObjectReader
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 ObjectReaderreader- the ObjectReader to unregister- Returns:
- true if the ObjectReader was unregistered, false otherwise
-
unregisterObjectReader
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 ObjectReaderreader- the ObjectReader to unregisterfieldBased- whether the ObjectReader is field-based- Returns:
- true if the ObjectReader was unregistered, false otherwise
-
register
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
Unregisters an ObjectReaderModule.- Parameters:
module- the module to unregister- Returns:
- true if the module was unregistered, false if it was not registered
-
cleanup
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
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
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
Gets the type converter function that can convert values from one type to another.- Parameters:
from- the source typeto- the target type- Returns:
- the converter function for the type pair, or null if no converter is registered
-
registerTypeConvert
Registers a type converter function that can convert values from one type to another.- Parameters:
from- the source typeto- the target typetypeConvert- 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
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
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 typeexpectClass- the expected class typefeatures- the JSON reader features- Returns:
- the ObjectReader for the specified type, or null if the type cannot be resolved
-
checkAutoType
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 checkexpectClass- the expected class typefeatures- 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
Gets the list of registered ObjectReader modules.- Returns:
- the list of modules
-
getBeanInfo
Gets bean information for the specified class by delegating to registered modules.- Parameters:
beanInfo- the BeanInfo object to populate with bean informationobjectClass- the class for which to get bean information
-
getFieldInfo
Gets field information for the specified field of a class.- Parameters:
fieldInfo- the FieldInfo object to populate with field informationobjectClass- the class containing the fieldfield- 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 informationobjectClass- the class containing the constructorconstructor- the constructor containing the parameterparamIndex- the index of the parameter in the constructorparameter- 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 informationobjectClass- the class containing the methodmethod- the method containing the parameterparamIndex- the index of the parameter in the methodparameter- the parameter for which to get information
-
getObjectReader
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 creatorfieldReaderArray- 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 creatorfieldReaderArray- the field readers to use- Returns:
- a function that creates CharArrayValueConsumer instances
-
getObjectReader
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 ObjectReaderfieldBased- whether to use field-based reading (true) or method-based reading (false)- Returns:
- the ObjectReader for the specified type
-
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
Sets the auto-type before handler that will be invoked before type resolution.- Parameters:
autoTypeBeforeHandler- the auto-type before handler to set
-
getAutoTypeList
-
getFieldInfo
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 informationobjectClass- the class containing the methodmethod- the method for which to get information
-
createObjectCreator
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 creatorreaderFeatures- 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
Creates a FieldReader for the specified class, field name, and reader features.- Parameters:
objectClass- the class containing the fieldfieldName- the name of the fieldreaderFeatures- 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 namestypes- the field typessupplier- the supplier function to create new instances of the objectc- 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 namestypes- the field typesfeatures- the field features (can be null)supplier- the supplier function to create new instances of the objectc- 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
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
Sets the property naming strategy used by this provider.- Parameters:
namingStrategy- the property naming strategy to set- Since:
- 2.0.52
-