Package com.alibaba.fastjson2.writer
Interface ObjectWriter<T>
- Type Parameters:
T- the type of objects that this ObjectWriter can serialize
- All Known Implementing Classes:
ApacheLang3Support.PairWriter,ObjectWriter1,ObjectWriter10,ObjectWriter11,ObjectWriter12,ObjectWriter2,ObjectWriter3,ObjectWriter4,ObjectWriter5,ObjectWriter6,ObjectWriter7,ObjectWriter8,ObjectWriter9,ObjectWriterAdapter,ObjectWriterException,ObjectWriterImplJSONP,ObjectWriterImplMap,ObjectWriterImplToString,ObjectWriterPrimitiveImpl,ObjectWriterRootName
public interface ObjectWriter<T>
ObjectWriter is responsible for serializing Java objects into JSON format.
It provides a set of methods for writing objects to JSON, handling field mapping,
and supporting various serialization features.
This interface supports various features including:
- Object serialization to JSON and JSONB formats
- Field mapping and value extraction
- Array mapping serialization
- Type information writing
- Custom feature configuration
- Filter support for property filtering
Example usage:
// Get an ObjectWriter for a specific type
ObjectWriter<User> writer = JSONFactory.getDefaultObjectWriterProvider().getObjectWriter(User.class);
// Write to JSON string
User user = new User(1, "John");
String jsonString = writer.toJSONString(user);
// Write to JSONWriter
try (JSONWriter jsonWriter = JSONWriter.of()) {
writer.write(jsonWriter, user);
String result = jsonWriter.toString();
}
- Since:
- 2.0.0
-
Method Summary
Modifier and TypeMethodDescriptiondefault longGets the features enabled by this ObjectWriter.default ObjectgetFieldValue(Object object, String fieldName) Gets the value of a field from the specified object.default FieldWritergetFieldWriter(long hashCode) Gets the FieldWriter for the specified field hash code.default FieldWritergetFieldWriter(String name) Gets the FieldWriter for the specified field name.default List<FieldWriter> Gets the list of FieldWriters associated with this ObjectWriter.default booleanhasFilter(JSONWriter jsonWriter) Checks if the JSONWriter has any filters enabled that would affect serialization.default voidSets a filter for this ObjectWriter.default voidsetNameFilter(NameFilter nameFilter) Sets the name filter for this ObjectWriter.default voidsetPropertyFilter(PropertyFilter propertyFilter) Sets the property filter for this ObjectWriter.default voidsetPropertyPreFilter(PropertyPreFilter propertyPreFilter) Sets the property pre-filter for this ObjectWriter.default voidsetValueFilter(ValueFilter valueFilter) Sets the value filter for this ObjectWriter.default StringtoJSONString(T object, JSONWriter.Feature... features) Converts an object to its JSON string representation using the specified features.default voidwrite(JSONWriter jsonWriter, Object object) Writes an object to the JSONWriter using default parameters.voidwrite(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) Writes an object to the JSONWriter with the given field name, field type, and features.default voidwriteArrayMapping(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) Writes an object to the JSONWriter in array mapping format.default voidwriteArrayMappingJSONB(JSONWriter jsonWriter, Object object) Writes an object to the JSONWriter in array mapping JSONB format.default voidwriteArrayMappingJSONB(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) Writes an object to the JSONWriter in array mapping JSONB format with additional parameters.default voidwriteJSONB(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) Writes an object to the JSONWriter in JSONB format.default booleanwriteTypeInfo(JSONWriter jsonWriter) Writes type information to the JSON output.default voidwriteWithFilter(JSONWriter jsonWriter, Object object) Writes an object to the JSONWriter with filter support using default parameters.default voidwriteWithFilter(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) Writes an object to the JSONWriter with filter support.
-
Method Details
-
getFeatures
default long getFeatures()Gets the features enabled by this ObjectWriter.- Returns:
- the enabled features as a bit mask
-
getFieldWriters
Gets the list of FieldWriters associated with this ObjectWriter.- Returns:
- the list of FieldWriters, or an empty list if none are available
-
getFieldWriter
Gets the FieldWriter for the specified field hash code.- Parameters:
hashCode- the hash code of the field name- Returns:
- the FieldWriter for the field, or null if not found
-
getFieldValue
Gets the value of a field from the specified object.- Parameters:
object- the object from which to get the field valuefieldName- the name of the field to get- Returns:
- the value of the field, or null if the field is not found
-
getFieldWriter
Gets the FieldWriter for the specified field name.- Parameters:
name- the name of the field- Returns:
- the FieldWriter for the field, or null if not found
-
writeTypeInfo
Writes type information to the JSON output. This is used for polymorphic serialization to include type information in the JSON output.- Parameters:
jsonWriter- the JSONWriter to which type information should be written- Returns:
- true if type information was written, false otherwise
-
writeJSONB
default void writeJSONB(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) Writes an object to the JSONWriter in JSONB format.- Parameters:
jsonWriter- the JSONWriter to which the object should be writtenobject- the object to writefieldName- the name of the field being writtenfieldType- the type of the field being writtenfeatures- the features to use for writing
-
writeArrayMappingJSONB
Writes an object to the JSONWriter in array mapping JSONB format.- Parameters:
jsonWriter- the JSONWriter to which the object should be writtenobject- the object to write
-
writeArrayMappingJSONB
default void writeArrayMappingJSONB(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) Writes an object to the JSONWriter in array mapping JSONB format with additional parameters.- Parameters:
jsonWriter- the JSONWriter to which the object should be writtenobject- the object to writefieldName- the name of the field being writtenfieldType- the type of the field being writtenfeatures- the features to use for writing
-
writeArrayMapping
default void writeArrayMapping(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) Writes an object to the JSONWriter in array mapping format.- Parameters:
jsonWriter- the JSONWriter to which the object should be writtenobject- the object to writefieldName- the name of the field being writtenfieldType- the type of the field being writtenfeatures- the features to use for writing
-
hasFilter
Checks if the JSONWriter has any filters enabled that would affect serialization.- Parameters:
jsonWriter- the JSONWriter to check- Returns:
- true if filters are enabled, false otherwise
-
write
Writes an object to the JSONWriter using default parameters.- Parameters:
jsonWriter- the JSONWriter to which the object should be writtenobject- the object to write
-
toJSONString
Converts an object to its JSON string representation using the specified features.- Parameters:
object- the object to convert to JSONfeatures- the JSON writer features to use- Returns:
- the JSON string representation of the object
-
write
Writes an object to the JSONWriter with the given field name, field type, and features.- Parameters:
jsonWriter- the JSONWriter to which the object should be writtenobject- the object to writefieldName- the name of the field being writtenfieldType- the type of the field being writtenfeatures- the features to use for writing
-
writeWithFilter
Writes an object to the JSONWriter with filter support using default parameters.- Parameters:
jsonWriter- the JSONWriter to which the object should be writtenobject- the object to write
-
writeWithFilter
default void writeWithFilter(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) Writes an object to the JSONWriter with filter support.- Parameters:
jsonWriter- the JSONWriter to which the object should be writtenobject- the object to writefieldName- the name of the field being writtenfieldType- the type of the field being writtenfeatures- the features to use for writing- Throws:
UnsupportedOperationException- if the method is not implemented
-
setPropertyFilter
Sets the property filter for this ObjectWriter.- Parameters:
propertyFilter- the property filter to set
-
setValueFilter
Sets the value filter for this ObjectWriter.- Parameters:
valueFilter- the value filter to set
-
setNameFilter
Sets the name filter for this ObjectWriter.- Parameters:
nameFilter- the name filter to set
-
setPropertyPreFilter
Sets the property pre-filter for this ObjectWriter.- Parameters:
propertyPreFilter- the property pre-filter to set
-
setFilter
Sets a filter for this ObjectWriter. The filter type is determined at runtime and the appropriate setter method is called.- Parameters:
filter- the filter to set
-