Package com.alibaba.fastjson2.writer
Class ObjectWriterProvider
java.lang.Object
com.alibaba.fastjson2.writer.ObjectWriterProvider
- All Implemented Interfaces:
ObjectCodecProvider
ObjectWriterProvider is responsible for providing and managing ObjectWriter instances
for serializing Java objects into JSON format. It handles object writer creation, caching,
type conversion, and various serialization features.
This provider supports various features including:
- Object writer caching for performance optimization
- Mixin support for modifying serialization behavior
- Module-based extensibility
- Custom type writer registration
- Property naming strategy configuration
Example usage:
// Get default provider ObjectWriterProvider provider = JSONFactory.getDefaultObjectWriterProvider(); // Get object writer for a specific type ObjectWriter<User> writer = provider.getObjectWriter(User.class); // Serialize object to JSON string User user = new User(1, "John"); String jsonString = writer.toJSONString(user); // Register custom object writer provider.register(User.class, new CustomUserWriter());
- Since:
- 2.0.0
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an ObjectWriterProvider with default settings.ObjectWriterProvider(PropertyNamingStrategy namingStrategy) Constructs an ObjectWriterProvider with the specified naming strategy.ObjectWriterProvider(ObjectWriterCreator creator) Constructs an ObjectWriterProvider with the specified ObjectWriterCreator. -
Method Summary
Modifier and TypeMethodDescriptionvoidCleans up cached ObjectWriters and mixin mappings associated with the specified class.voidcleanup(ClassLoader classLoader) Cleans up cached ObjectWriters associated with the specified ClassLoader.voidClears all mixin mappings.voidclear()Clears all cached ObjectWriters and mixin mappings.final voidconfigEnumAsJavaBean(Class<? extends Enum>... enumClasses) Configure the Enum classes as a JavaBeanprotected BeanInfoCreates a new BeanInfo instance.voidgetBeanInfo(BeanInfo beanInfo, Class objectClass) Gets bean information for the specified class by delegating to registered modules.Gets the ObjectWriterCreator used by this provider.voidgetFieldInfo(BeanInfo beanInfo, FieldInfo fieldInfo, Class objectClass, Field field) Gets field information for the specified field of a class.voidgetFieldInfo(BeanInfo beanInfo, FieldInfo fieldInfo, Class objectClass, Method method) Gets field information for the specified method of a class.Gets the mixin source class for the specified target class.Gets the list of registered ObjectWriter modules.Gets the property naming strategy used by this provider.getObjectWriter(Class objectClass) Gets an ObjectWriter for the specified class.getObjectWriter(Type objectType) Gets an ObjectWriter for the specified type.getObjectWriter(Type objectType, Class objectClass) Gets an ObjectWriter for the specified type and class.getObjectWriter(Type objectType, Class objectClass, boolean fieldBased) Gets an ObjectWriter for the specified type, class, and field-based option.getObjectWriter(Type objectType, Class objectClass, String format, boolean fieldBased) Gets an ObjectWriter for the specified type, class, and format with field-based option.getObjectWriter(Type objectType, String format, Locale locale) Gets an ObjectWriter for the specified type with formatting.getObjectWriterFromCache(Type objectType, Class objectClass, boolean fieldBased) Gets an ObjectWriter from the cache for the specified type and class.voidinit()Initializes this provider with the base module.booleanChecks if alphabetic ordering is enabled.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.static booleanisNotReferenceDetect(Class<?> clazz) Checks if the specified class is a type that should not have reference detection.static booleanisPrimitiveOrEnum(Class<?> clazz) Checks if the specified class is a primitive type or an enum.booleanChecks if transient fields should be skipped.voidRegisters a mixin mapping between a target class and a mixin source class.booleanregister(ObjectWriterModule module) Registers an ObjectWriterModule.register(Type type, ObjectWriter objectWriter) Registers an ObjectWriter for the specified type using the default field-based setting.register(Type type, ObjectWriter objectWriter, boolean fieldBased) Registers an ObjectWriter for the specified type.registerIfAbsent(Type type, ObjectWriter objectWriter) Registers an ObjectWriter for the specified type using method-based writing if it is not already registered.registerIfAbsent(Type type, ObjectWriter objectWriter, boolean fieldBased) Registers an ObjectWriter for the specified type if it is not already registered.voidsetAlphabetic(boolean alphabetic) Sets whether alphabetic ordering is enabled.voidsetCompatibleWithFieldName(boolean stat) Deprecated.only use compatible with fastjson 1.xvoidsetDisableArrayMapping(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.voidsetNamingStrategy(PropertyNamingStrategy namingStrategy) Sets the property naming strategy used by this provider.voidsetSkipTransient(boolean skipTransient) Sets whether transient fields should be skipped.booleanunregister(ObjectWriterModule module) Unregisters an ObjectWriterModule.unregister(Type type) Unregisters the ObjectWriter for the specified type using method-based writing.unregister(Type type, boolean fieldBased) Unregisters the ObjectWriter for the specified type.booleanunregister(Type type, ObjectWriter objectWriter) Unregisters the specified ObjectWriter for the given type, but only if the currently registered writer matches the specified writer.booleanunregister(Type type, ObjectWriter objectWriter, boolean fieldBased) Unregisters the specified ObjectWriter for the given type, but only if the currently registered writer matches the specified writer.
-
Constructor Details
-
ObjectWriterProvider
public ObjectWriterProvider()Constructs an ObjectWriterProvider with default settings. -
ObjectWriterProvider
Constructs an ObjectWriterProvider with the specified naming strategy.- Parameters:
namingStrategy- the property naming strategy to use
-
ObjectWriterProvider
Constructs an ObjectWriterProvider with the specified ObjectWriterCreator.- Parameters:
creator- the ObjectWriterCreator to use for creating ObjectWriter instances
-
-
Method Details
-
getNamingStrategy
Gets the property naming strategy used by this provider.- Returns:
- the property naming strategy, or null if none is set
-
setCompatibleWithFieldName
public void setCompatibleWithFieldName(boolean stat) Deprecated.only use compatible with fastjson 1.xSets whether to use compatible field name behavior.- Parameters:
stat- true to enable compatible field name behavior, false to disable
-
setNamingStrategy
Sets the property naming strategy used by this provider.- Parameters:
namingStrategy- the property naming strategy to set
-
mixIn
Registers a mixin mapping between a target class and a mixin source class. Mixin allows modifying the serialization 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
-
cleanupMixIn
public void cleanupMixIn()Clears all mixin mappings. -
getCreator
Gets the ObjectWriterCreator 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 ObjectWriterCreator
-
register
Registers an ObjectWriter for the specified type using the default field-based setting.- Parameters:
type- the type for which to register the ObjectWriterobjectWriter- the ObjectWriter to register, or null to unregister- Returns:
- the previous ObjectWriter for the type, or null if there was no previous ObjectWriter
-
register
Registers an ObjectWriter for the specified type.- Parameters:
type- the type for which to register the ObjectWriterobjectWriter- the ObjectWriter to register, or null to unregisterfieldBased- whether the ObjectWriter is field-based- Returns:
- the previous ObjectWriter for the type, or null if there was no previous ObjectWriter
-
registerIfAbsent
Registers an ObjectWriter for the specified type using method-based writing if it is not already registered.- Parameters:
type- the type for which to register the ObjectWriterobjectWriter- the ObjectWriter to register- Returns:
- the previous ObjectWriter for the type, or null if there was no previous ObjectWriter
-
registerIfAbsent
Registers an ObjectWriter for the specified type if it is not already registered.- Parameters:
type- the type for which to register the ObjectWriterobjectWriter- the ObjectWriter to registerfieldBased- whether the ObjectWriter is field-based- Returns:
- the previous ObjectWriter for the type, or null if there was no previous ObjectWriter
-
unregister
Unregisters the ObjectWriter for the specified type using method-based writing.- Parameters:
type- the type for which to unregister the ObjectWriter- Returns:
- the unregistered ObjectWriter, or null if there was no ObjectWriter for the type
-
unregister
Unregisters the ObjectWriter for the specified type.- Parameters:
type- the type for which to unregister the ObjectWriterfieldBased- whether the ObjectWriter is field-based- Returns:
- the unregistered ObjectWriter, or null if there was no ObjectWriter for the type
-
unregister
Unregisters the specified ObjectWriter for the given type, but only if the currently registered writer matches the specified writer.- Parameters:
type- the type for which to unregister the ObjectWriterobjectWriter- the ObjectWriter to unregister- Returns:
- true if the ObjectWriter was unregistered, false otherwise
-
unregister
Unregisters the specified ObjectWriter for the given type, but only if the currently registered writer matches the specified writer.- Parameters:
type- the type for which to unregister the ObjectWriterobjectWriter- the ObjectWriter to unregisterfieldBased- whether the ObjectWriter is field-based- Returns:
- true if the ObjectWriter was unregistered, false otherwise
-
register
Registers an ObjectWriterModule. 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 ObjectWriterModule.- Parameters:
module- the module to unregister- Returns:
- true if the module was unregistered, false if it was not registered
-
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
-
init
public void init()Initializes this provider with the base module. -
getModules
Gets the list of registered ObjectWriter modules.- Returns:
- the list of modules
-
getFieldInfo
Gets field information for the specified field of a class.- Parameters:
beanInfo- the BeanInfo object to populate with bean informationfieldInfo- the FieldInfo object to populate with field informationobjectClass- the class containing the fieldfield- the field for which to get information
-
getFieldInfo
Gets field information for the specified method of a class.- Parameters:
beanInfo- the BeanInfo object to populate with bean informationfieldInfo- the FieldInfo object to populate with field informationobjectClass- the class containing the methodmethod- the method for which to get information
-
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
-
getObjectWriter
Gets an ObjectWriter for the specified type with formatting.- Parameters:
objectType- the type for which to get an ObjectWriterformat- the format string to uselocale- the locale to use- Returns:
- the ObjectWriter for the specified type
-
getObjectWriter
Gets an ObjectWriter for the specified class.- Parameters:
objectClass- the class for which to get an ObjectWriter- Returns:
- the ObjectWriter for the specified class
-
getObjectWriter
Gets an ObjectWriter for the specified type and class.- Parameters:
objectType- the type for which to get an ObjectWriterobjectClass- the class for which to get an ObjectWriter- Returns:
- the ObjectWriter for the specified type and class
-
getObjectWriter
Gets an ObjectWriter for the specified type.- Parameters:
objectType- the type for which to get an ObjectWriter- Returns:
- the ObjectWriter for the specified type
-
getObjectWriterFromCache
public ObjectWriter getObjectWriterFromCache(Type objectType, Class objectClass, boolean fieldBased) Gets an ObjectWriter from the cache for the specified type and class.- Parameters:
objectType- the type for which to get an ObjectWriterobjectClass- the class for which to get an ObjectWriterfieldBased- whether to use field-based writing- Returns:
- the ObjectWriter from the cache, or null if not found
-
getObjectWriter
public ObjectWriter getObjectWriter(Type objectType, Class objectClass, String format, boolean fieldBased) Gets an ObjectWriter for the specified type, class, and format with field-based option.- Parameters:
objectType- the type for which to get an ObjectWriterobjectClass- the class for which to get an ObjectWriterformat- the format string to usefieldBased- whether to use field-based writing- Returns:
- the ObjectWriter for the specified type, class, and format
-
getObjectWriter
Gets an ObjectWriter for the specified type, class, and field-based option. If an ObjectWriter for the type is already cached, it will be returned directly. Otherwise, a new ObjectWriter will be created and cached.- Parameters:
objectType- the type for which to get an ObjectWriterobjectClass- the class for which to get an ObjectWriterfieldBased- whether to use field-based writing- Returns:
- the ObjectWriter for the specified type and class
-
isPrimitiveOrEnum
Checks if the specified class is a primitive type or an enum.- Parameters:
clazz- the class to check- Returns:
- true if the class is a primitive type or an enum, false otherwise
-
isNotReferenceDetect
Checks if the specified class is a type that should not have reference detection.- Parameters:
clazz- the class to check- Returns:
- true if the class should not have reference detection, false otherwise
-
clear
public void clear()Clears all cached ObjectWriters and mixin mappings.- Since:
- 2.0.53
-
cleanup
Cleans up cached ObjectWriters and mixin mappings associated with the specified class.- Parameters:
objectClass- the class for which to clean up cached ObjectWriters
-
cleanup
Cleans up cached ObjectWriters associated with the specified ClassLoader. This method removes all cached writers that are related to classes loaded by the given ClassLoader.- Parameters:
classLoader- the ClassLoader for which to clean up cached ObjectWriters
-
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
-
isAlphabetic
public boolean isAlphabetic()Checks if alphabetic ordering is enabled.- Returns:
- true if alphabetic ordering is enabled, false otherwise
-
setAlphabetic
public void setAlphabetic(boolean alphabetic) Sets whether alphabetic ordering is enabled.- Parameters:
alphabetic- true to enable alphabetic ordering, false to disable it
-
isSkipTransient
public boolean isSkipTransient()Checks if transient fields should be skipped.- Returns:
- true if transient fields should be skipped, false otherwise
-
setSkipTransient
public void setSkipTransient(boolean skipTransient) Sets whether transient fields should be skipped.- Parameters:
skipTransient- true to skip transient fields, false to include them
-
createBeanInfo
Creates a new BeanInfo instance.- Returns:
- a new BeanInfo instance
-
configEnumAsJavaBean
Configure the Enum classes as a JavaBean- Parameters:
enumClasses- enum classes- Since:
- 2.0.55
-