Package com.tnsai.output
Class OutputSerializerRegistry
java.lang.Object
com.tnsai.output.OutputSerializerRegistry
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 Summary
Modifier and TypeMethodDescription<T> Tdeserialize(String data, OutputFormat format, Class<T> targetClass) Convenience method to deserialize data from a specific format.getFormatInstructions(OutputFormat format, Class<?> targetClass) Gets format instructions for LLM prompts.static OutputSerializerRegistryGets the singleton instance of the registry.getListFormatInstructions(OutputFormat format, Class<?> itemClass) Gets format instructions for list output.getSerializer(OutputFormat format) Gets the serializer for a specific format.booleanhasSerializer(OutputFormat format) Checks if a serializer is registered for a format.voidregister(OutputFormat format, OutputSerializer serializer) Registers a custom serializer for a format.voidreset()Resets registry to default serializers.serialize(Object data, OutputFormat format) Convenience method to serialize data with pretty printing enabled.serialize(Object data, OutputFormat format, boolean prettyPrint) Convenience method to serialize data with a specific format.booleansupportsType(OutputFormat format, Class<?> dataClass) Checks if a format supports a given data type.
-
Method Details
-
getInstance
Gets the singleton instance of the registry. -
getSerializer
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
Registers a custom serializer for a format. Replaces any existing serializer for that format. Thread-safe write operation.- Parameters:
format- the output formatserializer- the serializer to register
-
hasSerializer
Checks if a serializer is registered for a format. Thread-safe read operation. -
serialize
Convenience method to serialize data with a specific format.- Parameters:
data- the data to serializeformat- the output formatprettyPrint- whether to format for readability- Returns:
- the serialized string
-
serialize
Convenience method to serialize data with pretty printing enabled. -
deserialize
Convenience method to deserialize data from a specific format.- Type Parameters:
T- the target type- Parameters:
data- the serialized stringformat- the output formattargetClass- the target class- Returns:
- the deserialized object
-
getFormatInstructions
Gets format instructions for LLM prompts.- Parameters:
format- the output formattargetClass- the expected output class- Returns:
- format instructions string
-
getListFormatInstructions
Gets format instructions for list output.- Parameters:
format- the output formatitemClass- the expected item class- Returns:
- format instructions string
-
supportsType
Checks if a format supports a given data type.- Parameters:
format- the output formatdataClass- the data class- Returns:
- true if supported
-
reset
public void reset()Resets registry to default serializers. Thread-safe. Useful for testing.
-