public class ObjectReaderProvider extends java.lang.Object implements ObjectCodecProvider
This provider supports various features including:
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);
| Modifier and Type | Class and Description |
|---|---|
private static class |
ObjectReaderProvider.LRUAutoTypeCache |
private static class |
ObjectReaderProvider.ObjectReaderCachePair |
| Modifier and Type | Field and Description |
|---|---|
private long[] |
acceptHashCodes |
private java.util.Set<java.lang.String> |
acceptNameSet
The accept names behind
acceptHashCodes, normalized the same way the rolling hash in
checkAutoType(java.lang.String, java.lang.Class<?>, long) normalizes a type name. |
(package private) static java.lang.String |
ANTI_COLLISION_HASH_MAP
Always accepted, it is the map implementation fastjson 1.x substitutes for
HashMap
when hash collision protection is enabled. |
(package private) static long |
ANTI_COLLISION_HASH_MAP_HASH |
(package private) static java.lang.String[] |
AUTO_TYPE_ACCEPT_LIST |
private JSONReader.AutoTypeBeforeHandler |
autoTypeBeforeHandler |
private java.util.function.Consumer<java.lang.Class> |
autoTypeHandler |
(package private) ObjectReaderProvider.LRUAutoTypeCache |
autoTypeList |
(package private) java.util.concurrent.ConcurrentMap<java.lang.reflect.Type,ObjectReader> |
cache |
(package private) java.util.concurrent.ConcurrentMap<java.lang.reflect.Type,ObjectReader> |
cacheFieldBased |
(package private) ObjectReaderCreator |
creator |
(package private) static JSONReader.AutoTypeBeforeHandler |
DEFAULT_AUTO_TYPE_BEFORE_HANDLER |
(package private) static java.util.function.Consumer<java.lang.Class> |
DEFAULT_AUTO_TYPE_HANDLER |
(package private) static boolean |
DEFAULT_AUTO_TYPE_HANDLER_INIT_ERROR |
(package private) static java.lang.String[] |
DENYS |
(package private) boolean |
disableArrayMapping |
(package private) boolean |
disableAutoType |
(package private) boolean |
disableJSONB |
(package private) boolean |
disableReferenceDetect |
(package private) boolean |
disableSmartMatch |
(package private) static java.lang.ClassLoader |
FASTJSON2_CLASS_LOADER |
(package private) java.util.concurrent.ConcurrentMap<java.lang.Long,ObjectReader> |
hashCache |
(package private) java.util.concurrent.ConcurrentMap<java.lang.Class,java.lang.Class> |
mixInCache |
(package private) java.util.List<ObjectReaderModule> |
modules |
(package private) PropertyNamingStrategy |
namingStrategy |
(package private) static ObjectReaderProvider.ObjectReaderCachePair |
readerCache |
static boolean |
SAFE_MODE |
(package private) java.util.concurrent.ConcurrentMap<java.lang.Integer,java.util.concurrent.ConcurrentHashMap<java.lang.Long,ObjectReader>> |
tclHashCaches |
private java.util.concurrent.ConcurrentMap<java.lang.reflect.Type,java.util.Map<java.lang.reflect.Type,java.util.function.Function>> |
typeConverts |
| Constructor and Description |
|---|
ObjectReaderProvider()
Constructs an ObjectReaderProvider with the default ObjectReaderCreator based on
system configuration.
|
ObjectReaderProvider(ObjectReaderCreator creator)
Constructs an ObjectReaderProvider with the specified ObjectReaderCreator.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addAutoTypeAccept(java.lang.String name)
Adds a type name to the auto-type accept list.
|
void |
addAutoTypeDeny(java.lang.String name)
Deprecated.
|
(package private) void |
afterAutoType(java.lang.String typeName,
java.lang.Class type) |
java.lang.Class<?> |
checkAutoType(java.lang.String typeName,
java.lang.Class<?> expectClass,
long features)
Checks and resolves the class for auto-type support.
|
void |
cleanup(java.lang.Class objectClass)
Cleans up cached ObjectReaders and mixin mappings associated with the specified class.
|
void |
cleanup(java.lang.ClassLoader classLoader)
Cleans up cached ObjectReaders associated with the specified ClassLoader.
|
void |
cleanupMixIn()
Clears all mixin mappings.
|
void |
clear()
Clears all cached ObjectReaders and mixin mappings.
|
java.util.function.Function<java.util.function.Consumer,CharArrayValueConsumer> |
createCharArrayValueConsumerCreator(java.lang.Class objectClass,
FieldReader[] fieldReaderArray)
Creates a value consumer creator for char array values.
|
FieldReader |
createFieldReader(java.lang.Class objectClass,
java.lang.String fieldName,
long readerFeatures)
Creates a FieldReader for the specified class, field name, and reader features.
|
<T> java.util.function.Supplier<T> |
createObjectCreator(java.lang.Class<T> objectClass,
long readerFeatures)
Creates an object creator (supplier) for the specified class and reader features.
|
<T> ObjectReader<T> |
createObjectReader(java.lang.String[] names,
java.lang.reflect.Type[] types,
long[] features,
java.util.function.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(java.lang.String[] names,
java.lang.reflect.Type[] types,
java.util.function.Supplier<T> supplier,
FieldConsumer<T> c)
Creates an ObjectReader for a custom object with specified field names, types, and consumer.
|
java.util.function.Function<java.util.function.Consumer,ByteArrayValueConsumer> |
createValueConsumerCreator(java.lang.Class objectClass,
FieldReader[] fieldReaderArray)
Creates a value consumer creator for byte array values.
|
JSONReader.AutoTypeBeforeHandler |
getAutoTypeBeforeHandler()
Gets the auto-type before handler that is invoked before type resolution.
|
java.util.function.Consumer<java.lang.Class> |
getAutoTypeHandler()
Gets the auto-type handler that is invoked when a type is auto-resolved.
|
java.util.Map<java.lang.String,java.util.Date> |
getAutoTypeList() |
void |
getBeanInfo(BeanInfo beanInfo,
java.lang.Class objectClass)
Gets bean information for the specified class by delegating to registered modules.
|
ObjectReaderCreator |
getCreator()
Gets the ObjectReaderCreator used by this provider.
|
void |
getFieldInfo(FieldInfo fieldInfo,
java.lang.Class objectClass,
java.lang.reflect.Constructor constructor,
int paramIndex,
java.lang.reflect.Parameter parameter)
Gets field information for the specified constructor parameter.
|
void |
getFieldInfo(FieldInfo fieldInfo,
java.lang.Class objectClass,
java.lang.reflect.Field field)
Gets field information for the specified field of a class.
|
void |
getFieldInfo(FieldInfo fieldInfo,
java.lang.Class objectClass,
java.lang.reflect.Method method)
Gets field information for the specified method of a class.
|
void |
getFieldInfo(FieldInfo fieldInfo,
java.lang.Class objectClass,
java.lang.reflect.Method method,
int paramIndex,
java.lang.reflect.Parameter parameter)
Gets field information for the specified method parameter.
|
java.lang.Class |
getMixIn(java.lang.Class target)
Gets the mixin source class for the specified target class.
|
java.util.List<ObjectReaderModule> |
getModules()
Gets the list of registered ObjectReader modules.
|
PropertyNamingStrategy |
getNamingStrategy()
Gets the property naming strategy used by this provider.
|
ObjectReader |
getObjectReader(long hashCode)
Gets an ObjectReader by its hash code.
|
ObjectReader |
getObjectReader(java.lang.String typeName,
java.lang.Class<?> expectClass,
long features)
Gets an ObjectReader for the specified type name, expected class, and features.
|
ObjectReader |
getObjectReader(java.lang.reflect.Type objectType)
Gets an ObjectReader for the specified type.
|
ObjectReader |
getObjectReader(java.lang.reflect.Type objectType,
boolean fieldBased)
Gets an ObjectReader for the specified type with field-based option.
|
private ObjectReader |
getObjectReaderInternal(java.lang.reflect.Type objectType,
boolean fieldBased) |
private ObjectReader |
getPreviousObjectReader(boolean fieldBased,
java.lang.reflect.Type objectType,
ObjectReader boundObjectReader) |
java.util.function.Function |
getTypeConvert(java.lang.reflect.Type from,
java.lang.reflect.Type to)
Gets the type converter function that can convert values from one type to another.
|
(package private) void |
init() |
boolean |
isDisableArrayMapping()
Checks if array mapping is disabled.
|
boolean |
isDisableAutoType()
Checks if auto-type support is disabled.
|
boolean |
isDisableJSONB()
Checks if JSONB support is disabled.
|
boolean |
isDisableReferenceDetect()
Checks if reference detection is disabled.
|
boolean |
isDisableSmartMatch()
Checks if smart match is disabled.
|
(package private) static boolean |
match(java.lang.reflect.Type objectType,
ObjectReader objectReader,
java.lang.ClassLoader classLoader) |
void |
mixIn(java.lang.Class target,
java.lang.Class mixinSource)
Registers a mixin mapping between a target class and a mixin source class.
|
boolean |
register(ObjectReaderModule module)
Registers an ObjectReaderModule.
|
ObjectReader |
register(java.lang.reflect.Type type,
ObjectReader objectReader)
Registers an ObjectReader for the specified type using method-based reading.
|
ObjectReader |
register(java.lang.reflect.Type type,
ObjectReader objectReader,
boolean fieldBased)
Registers an ObjectReader for the specified type.
|
void |
registerIfAbsent(long hashCode,
ObjectReader objectReader)
Registers an ObjectReader for the specified hash code if it is not already registered.
|
ObjectReader |
registerIfAbsent(java.lang.reflect.Type type,
ObjectReader objectReader)
Registers an ObjectReader for the specified type using method-based reading
if it is not already registered.
|
ObjectReader |
registerIfAbsent(java.lang.reflect.Type type,
ObjectReader objectReader,
boolean fieldBased)
Registers an ObjectReader for the specified type if it is not already registered.
|
void |
registerSeeAlsoSubType(java.lang.Class subTypeClass)
Registers a subtype class for see-also support.
|
void |
registerSeeAlsoSubType(java.lang.Class subTypeClass,
java.lang.String subTypeClassName)
Registers a subtype class with a specific name for see-also support.
|
java.util.function.Function |
registerTypeConvert(java.lang.reflect.Type from,
java.lang.reflect.Type to,
java.util.function.Function typeConvert)
Registers a type converter function that can convert values from one type to another.
|
void |
setAutoTypeBeforeHandler(JSONReader.AutoTypeBeforeHandler autoTypeBeforeHandler)
Sets the auto-type before handler that will be invoked before type resolution.
|
void |
setAutoTypeHandler(java.util.function.Consumer<java.lang.Class> autoTypeHandler)
Sets the auto-type handler that will be invoked when a type is auto-resolved.
|
void |
setDisableArrayMapping(boolean disableArrayMapping)
Sets whether array mapping is disabled.
|
void |
setDisableAutoType(boolean disableAutoType)
Sets whether auto-type support is disabled.
|
void |
setDisableJSONB(boolean disableJSONB)
Sets whether JSONB support is disabled.
|
void |
setDisableReferenceDetect(boolean disableReferenceDetect)
Sets whether reference detection is disabled.
|
void |
setDisableSmartMatch(boolean disableSmartMatch)
Sets whether smart match is disabled.
|
void |
setNamingStrategy(PropertyNamingStrategy namingStrategy)
Sets the property naming strategy used by this provider.
|
boolean |
unregister(ObjectReaderModule module)
Unregisters an ObjectReaderModule.
|
ObjectReader |
unregisterObjectReader(java.lang.reflect.Type type)
Unregisters the ObjectReader for the specified type using method-based reading.
|
ObjectReader |
unregisterObjectReader(java.lang.reflect.Type type,
boolean fieldBased)
Unregisters the ObjectReader for the specified type.
|
boolean |
unregisterObjectReader(java.lang.reflect.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.
|
boolean |
unregisterObjectReader(java.lang.reflect.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.
|
static final java.lang.ClassLoader FASTJSON2_CLASS_LOADER
public static final boolean SAFE_MODE
static final java.lang.String[] DENYS
static final java.lang.String[] AUTO_TYPE_ACCEPT_LIST
static JSONReader.AutoTypeBeforeHandler DEFAULT_AUTO_TYPE_BEFORE_HANDLER
static java.util.function.Consumer<java.lang.Class> DEFAULT_AUTO_TYPE_HANDLER
static boolean DEFAULT_AUTO_TYPE_HANDLER_INIT_ERROR
static ObjectReaderProvider.ObjectReaderCachePair readerCache
final java.util.concurrent.ConcurrentMap<java.lang.reflect.Type,ObjectReader> cache
final java.util.concurrent.ConcurrentMap<java.lang.reflect.Type,ObjectReader> cacheFieldBased
final java.util.concurrent.ConcurrentMap<java.lang.Integer,java.util.concurrent.ConcurrentHashMap<java.lang.Long,ObjectReader>> tclHashCaches
final java.util.concurrent.ConcurrentMap<java.lang.Long,ObjectReader> hashCache
final java.util.concurrent.ConcurrentMap<java.lang.Class,java.lang.Class> mixInCache
final ObjectReaderProvider.LRUAutoTypeCache autoTypeList
private final java.util.concurrent.ConcurrentMap<java.lang.reflect.Type,java.util.Map<java.lang.reflect.Type,java.util.function.Function>> typeConverts
final ObjectReaderCreator creator
final java.util.List<ObjectReaderModule> modules
boolean disableReferenceDetect
boolean disableArrayMapping
boolean disableJSONB
boolean disableAutoType
boolean disableSmartMatch
static final java.lang.String ANTI_COLLISION_HASH_MAP
HashMap
when hash collision protection is enabled.static final long ANTI_COLLISION_HASH_MAP_HASH
private volatile long[] acceptHashCodes
private volatile java.util.Set<java.lang.String> acceptNameSet
acceptHashCodes, normalized the same way the rolling hash in
checkAutoType(java.lang.String, java.lang.Class<?>, long) normalizes a type name. A hash match is only honored when the matched
prefix text is in this set, so a hash collision alone cannot whitelist a type name.private JSONReader.AutoTypeBeforeHandler autoTypeBeforeHandler
private java.util.function.Consumer<java.lang.Class> autoTypeHandler
PropertyNamingStrategy namingStrategy
public ObjectReaderProvider()
The provider is initialized with the base module and all registered modules.
public ObjectReaderProvider(ObjectReaderCreator creator)
creator - the ObjectReaderCreator to use for creating ObjectReader instancespublic void registerIfAbsent(long hashCode,
ObjectReader objectReader)
hashCode - the hash code for which to register the ObjectReaderobjectReader - the ObjectReader to registerpublic void addAutoTypeAccept(java.lang.String name)
name - the type name to add to the accept list@Deprecated public void addAutoTypeDeny(java.lang.String name)
public java.util.function.Consumer<java.lang.Class> getAutoTypeHandler()
public void setAutoTypeHandler(java.util.function.Consumer<java.lang.Class> autoTypeHandler)
autoTypeHandler - the auto-type handler to setpublic java.lang.Class getMixIn(java.lang.Class target)
getMixIn in interface ObjectCodecProvidertarget - the target classpublic void cleanupMixIn()
public void mixIn(java.lang.Class target,
java.lang.Class mixinSource)
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 mixinpublic void registerSeeAlsoSubType(java.lang.Class subTypeClass)
subTypeClass - the subtype class to registerpublic void registerSeeAlsoSubType(java.lang.Class subTypeClass,
java.lang.String subTypeClassName)
subTypeClass - the subtype class to registersubTypeClassName - the name of the subtype class, or null to use the class's simple nameJSONException - if the superclass is nullpublic ObjectReader register(java.lang.reflect.Type type, ObjectReader objectReader, boolean fieldBased)
type - the type for which to register the ObjectReaderobjectReader - the ObjectReader to register, or null to unregisterfieldBased - whether the ObjectReader is field-basedpublic ObjectReader register(java.lang.reflect.Type type, ObjectReader objectReader)
type - the type for which to register the ObjectReaderobjectReader - the ObjectReader to register, or null to unregisterpublic ObjectReader registerIfAbsent(java.lang.reflect.Type type, ObjectReader objectReader)
type - the type for which to register the ObjectReaderobjectReader - the ObjectReader to registerpublic ObjectReader registerIfAbsent(java.lang.reflect.Type type, ObjectReader objectReader, boolean fieldBased)
type - the type for which to register the ObjectReaderobjectReader - the ObjectReader to registerfieldBased - whether the ObjectReader is field-basedpublic ObjectReader unregisterObjectReader(java.lang.reflect.Type type)
type - the type for which to unregister the ObjectReaderpublic ObjectReader unregisterObjectReader(java.lang.reflect.Type type, boolean fieldBased)
type - the type for which to unregister the ObjectReaderfieldBased - whether the ObjectReader is field-basedpublic boolean unregisterObjectReader(java.lang.reflect.Type type,
ObjectReader reader)
type - the type for which to unregister the ObjectReaderreader - the ObjectReader to unregisterpublic boolean unregisterObjectReader(java.lang.reflect.Type type,
ObjectReader reader,
boolean fieldBased)
type - the type for which to unregister the ObjectReaderreader - the ObjectReader to unregisterfieldBased - whether the ObjectReader is field-basedpublic boolean register(ObjectReaderModule module)
module - the module to registerpublic boolean unregister(ObjectReaderModule module)
module - the module to unregisterpublic void cleanup(java.lang.Class objectClass)
objectClass - the class for which to clean up cached ObjectReaderspublic void clear()
static boolean match(java.lang.reflect.Type objectType,
ObjectReader objectReader,
java.lang.ClassLoader classLoader)
public void cleanup(java.lang.ClassLoader classLoader)
classLoader - the ClassLoader for which to clean up cached ObjectReaderspublic ObjectReaderCreator getCreator()
void init()
public java.util.function.Function getTypeConvert(java.lang.reflect.Type from,
java.lang.reflect.Type to)
from - the source typeto - the target typepublic java.util.function.Function registerTypeConvert(java.lang.reflect.Type from,
java.lang.reflect.Type to,
java.util.function.Function typeConvert)
from - the source typeto - the target typetypeConvert - the function to convert from source type to target typepublic ObjectReader getObjectReader(long hashCode)
hashCode - the hash code of the ObjectReader to retrievepublic ObjectReader getObjectReader(java.lang.String typeName, java.lang.Class<?> expectClass, long features)
typeName - the name of the typeexpectClass - the expected class typefeatures - the JSON reader featuresfinal void afterAutoType(java.lang.String typeName,
java.lang.Class type)
public java.lang.Class<?> checkAutoType(java.lang.String typeName,
java.lang.Class<?> expectClass,
long features)
typeName - the name of the type to checkexpectClass - the expected class typefeatures - the JSON reader featuresJSONException - if the type is not supported or security checks failpublic java.util.List<ObjectReaderModule> getModules()
public void getBeanInfo(BeanInfo beanInfo, java.lang.Class objectClass)
beanInfo - the BeanInfo object to populate with bean informationobjectClass - the class for which to get bean informationpublic void getFieldInfo(FieldInfo fieldInfo, java.lang.Class objectClass, java.lang.reflect.Field field)
fieldInfo - the FieldInfo object to populate with field informationobjectClass - the class containing the fieldfield - the field for which to get informationpublic void getFieldInfo(FieldInfo fieldInfo, java.lang.Class objectClass, java.lang.reflect.Constructor constructor, int paramIndex, java.lang.reflect.Parameter parameter)
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 informationpublic void getFieldInfo(FieldInfo fieldInfo, java.lang.Class objectClass, java.lang.reflect.Method method, int paramIndex, java.lang.reflect.Parameter parameter)
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 informationpublic ObjectReader getObjectReader(java.lang.reflect.Type objectType)
objectType - the type for which to get an ObjectReaderpublic java.util.function.Function<java.util.function.Consumer,ByteArrayValueConsumer> createValueConsumerCreator(java.lang.Class objectClass, FieldReader[] fieldReaderArray)
objectClass - the class for which to create the value consumer creatorfieldReaderArray - the field readers to usepublic java.util.function.Function<java.util.function.Consumer,CharArrayValueConsumer> createCharArrayValueConsumerCreator(java.lang.Class objectClass, FieldReader[] fieldReaderArray)
objectClass - the class for which to create the value consumer creatorfieldReaderArray - the field readers to usepublic ObjectReader getObjectReader(java.lang.reflect.Type objectType, boolean fieldBased)
objectType - the type for which to get an ObjectReaderfieldBased - whether to use field-based reading (true) or method-based reading (false)private ObjectReader getObjectReaderInternal(java.lang.reflect.Type objectType, boolean fieldBased)
private ObjectReader getPreviousObjectReader(boolean fieldBased, java.lang.reflect.Type objectType, ObjectReader boundObjectReader)
public JSONReader.AutoTypeBeforeHandler getAutoTypeBeforeHandler()
public void setAutoTypeBeforeHandler(JSONReader.AutoTypeBeforeHandler autoTypeBeforeHandler)
autoTypeBeforeHandler - the auto-type before handler to setpublic java.util.Map<java.lang.String,java.util.Date> getAutoTypeList()
public void getFieldInfo(FieldInfo fieldInfo, java.lang.Class objectClass, java.lang.reflect.Method method)
fieldInfo - the FieldInfo object to populate with field informationobjectClass - the class containing the methodmethod - the method for which to get informationpublic <T> java.util.function.Supplier<T> createObjectCreator(java.lang.Class<T> objectClass,
long readerFeatures)
T - the type of the objectobjectClass - the class for which to create an object creatorreaderFeatures - the reader features to useJSONException - if no default constructor is found for the classpublic FieldReader createFieldReader(java.lang.Class objectClass, java.lang.String fieldName, long readerFeatures)
objectClass - the class containing the fieldfieldName - the name of the fieldreaderFeatures - the reader features to usepublic <T> ObjectReader<T> createObjectReader(java.lang.String[] names, java.lang.reflect.Type[] types, java.util.function.Supplier<T> supplier, FieldConsumer<T> c)
T - the type of the objectnames - the field namestypes - the field typessupplier - the supplier function to create new instances of the objectc - the field consumer to set field valuespublic <T> ObjectReader<T> createObjectReader(java.lang.String[] names, java.lang.reflect.Type[] types, long[] features, java.util.function.Supplier<T> supplier, FieldConsumer<T> c)
T - the type of the objectnames - 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 valuespublic boolean isDisableReferenceDetect()
public boolean isDisableAutoType()
public boolean isDisableJSONB()
public boolean isDisableArrayMapping()
public void setDisableReferenceDetect(boolean disableReferenceDetect)
disableReferenceDetect - true to disable reference detection, false to enable itpublic void setDisableArrayMapping(boolean disableArrayMapping)
disableArrayMapping - true to disable array mapping, false to enable itpublic void setDisableJSONB(boolean disableJSONB)
disableJSONB - true to disable JSONB support, false to enable itpublic void setDisableAutoType(boolean disableAutoType)
disableAutoType - true to disable auto-type support, false to enable itpublic boolean isDisableSmartMatch()
public void setDisableSmartMatch(boolean disableSmartMatch)
disableSmartMatch - true to disable smart match, false to enable itpublic PropertyNamingStrategy getNamingStrategy()
public void setNamingStrategy(PropertyNamingStrategy namingStrategy)
namingStrategy - the property naming strategy to set