Class OutputSerializerRegistry

java.lang.Object
com.tnsai.output.OutputSerializerRegistry

public final class OutputSerializerRegistry extends Object
Registry and factory for output serializers. Provides thread-safe access to serializers for all supported output formats.

Usage:

 // Get singleton instance
 OutputSerializerRegistry registry = OutputSerializerRegistry.getInstance();

 // Get serializer for a specific format
 OutputSerializer jsonSerializer = registry.getSerializer(OutputFormat.JSON);

 // Serialize data
 String output = jsonSerializer.serialize(myObject);

 // Or use convenience method
 String yamlOutput = registry.serialize(myObject, OutputFormat.YAML);
 

Custom serializers can be registered (thread-safe):

 registry.register(OutputFormat.JSON, new CustomJsonSerializer());
 
  • Method Details

    • getInstance

      public static OutputSerializerRegistry getInstance()
      Gets the singleton instance of the registry.
    • getSerializer

      public OutputSerializer getSerializer(OutputFormat format)
      Gets the serializer for a specific format. Thread-safe read operation.
      Parameters:
      format - the output format
      Returns:
      the serializer
      Throws:
      IllegalArgumentException - if no serializer registered for format
    • register

      public void register(OutputFormat format, OutputSerializer serializer)
      Registers a custom serializer for a format. Replaces any existing serializer for that format. Thread-safe write operation.
      Parameters:
      format - the output format
      serializer - the serializer to register
    • hasSerializer

      public boolean hasSerializer(OutputFormat format)
      Checks if a serializer is registered for a format. Thread-safe read operation.
    • serialize

      public String serialize(Object data, OutputFormat format, boolean prettyPrint)
      Convenience method to serialize data with a specific format.
      Parameters:
      data - the data to serialize
      format - the output format
      prettyPrint - whether to format for readability
      Returns:
      the serialized string
    • serialize

      public String serialize(Object data, OutputFormat format)
      Convenience method to serialize data with pretty printing enabled.
    • deserialize

      public <T> T deserialize(String data, OutputFormat format, Class<T> targetClass)
      Convenience method to deserialize data from a specific format.
      Type Parameters:
      T - the target type
      Parameters:
      data - the serialized string
      format - the output format
      targetClass - the target class
      Returns:
      the deserialized object
    • getFormatInstructions

      public String getFormatInstructions(OutputFormat format, Class<?> targetClass)
      Gets format instructions for LLM prompts.
      Parameters:
      format - the output format
      targetClass - the expected output class
      Returns:
      format instructions string
    • getListFormatInstructions

      public String getListFormatInstructions(OutputFormat format, Class<?> itemClass)
      Gets format instructions for list output.
      Parameters:
      format - the output format
      itemClass - the expected item class
      Returns:
      format instructions string
    • supportsType

      public boolean supportsType(OutputFormat format, Class<?> dataClass)
      Checks if a format supports a given data type.
      Parameters:
      format - the output format
      dataClass - the data class
      Returns:
      true if supported
    • reset

      public void reset()
      Resets registry to default serializers. Thread-safe. Useful for testing.