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 Details

    • getFeatures

      default long getFeatures()
      Gets the features enabled by this ObjectWriter.
      Returns:
      the enabled features as a bit mask
    • getFieldWriters

      default List<FieldWriter> getFieldWriters()
      Gets the list of FieldWriters associated with this ObjectWriter.
      Returns:
      the list of FieldWriters, or an empty list if none are available
    • getFieldWriter

      default FieldWriter getFieldWriter(long hashCode)
      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

      default Object getFieldValue(Object object, String fieldName)
      Gets the value of a field from the specified object.
      Parameters:
      object - the object from which to get the field value
      fieldName - the name of the field to get
      Returns:
      the value of the field, or null if the field is not found
    • getFieldWriter

      default FieldWriter getFieldWriter(String name)
      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

      default boolean writeTypeInfo(JSONWriter jsonWriter)
      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 written
      object - the object to write
      fieldName - the name of the field being written
      fieldType - the type of the field being written
      features - the features to use for writing
    • writeArrayMappingJSONB

      default void writeArrayMappingJSONB(JSONWriter jsonWriter, Object object)
      Writes an object to the JSONWriter in array mapping JSONB format.
      Parameters:
      jsonWriter - the JSONWriter to which the object should be written
      object - 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 written
      object - the object to write
      fieldName - the name of the field being written
      fieldType - the type of the field being written
      features - 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 written
      object - the object to write
      fieldName - the name of the field being written
      fieldType - the type of the field being written
      features - the features to use for writing
    • hasFilter

      default boolean hasFilter(JSONWriter jsonWriter)
      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

      default void write(JSONWriter jsonWriter, Object object)
      Writes an object to the JSONWriter using default parameters.
      Parameters:
      jsonWriter - the JSONWriter to which the object should be written
      object - the object to write
    • toJSONString

      default String toJSONString(T object, JSONWriter.Feature... features)
      Converts an object to its JSON string representation using the specified features.
      Parameters:
      object - the object to convert to JSON
      features - the JSON writer features to use
      Returns:
      the JSON string representation of the object
    • write

      void write(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.
      Parameters:
      jsonWriter - the JSONWriter to which the object should be written
      object - the object to write
      fieldName - the name of the field being written
      fieldType - the type of the field being written
      features - the features to use for writing
    • writeWithFilter

      default void writeWithFilter(JSONWriter jsonWriter, Object object)
      Writes an object to the JSONWriter with filter support using default parameters.
      Parameters:
      jsonWriter - the JSONWriter to which the object should be written
      object - 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 written
      object - the object to write
      fieldName - the name of the field being written
      fieldType - the type of the field being written
      features - the features to use for writing
      Throws:
      UnsupportedOperationException - if the method is not implemented
    • setPropertyFilter

      default void setPropertyFilter(PropertyFilter propertyFilter)
      Sets the property filter for this ObjectWriter.
      Parameters:
      propertyFilter - the property filter to set
    • setValueFilter

      default void setValueFilter(ValueFilter valueFilter)
      Sets the value filter for this ObjectWriter.
      Parameters:
      valueFilter - the value filter to set
    • setNameFilter

      default void setNameFilter(NameFilter nameFilter)
      Sets the name filter for this ObjectWriter.
      Parameters:
      nameFilter - the name filter to set
    • setPropertyPreFilter

      default void setPropertyPreFilter(PropertyPreFilter propertyPreFilter)
      Sets the property pre-filter for this ObjectWriter.
      Parameters:
      propertyPreFilter - the property pre-filter to set
    • setFilter

      default void setFilter(Filter filter)
      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