Package com.tnsai.output
Interface OutputSerializer
- All Known Implementing Classes:
AbstractOutputSerializer,JsonOutputSerializer,TextOutputSerializer,TonlOutputSerializer,ToonOutputSerializer,YamlOutputSerializer
public interface OutputSerializer
Interface for serializing structured data to various output formats.
Implementations handle format-specific serialization logic.
The serializer is designed to work with:
- POJOs (Plain Old Java Objects)
- Maps (Map<String, Object>)
- Lists of objects
- Primitive types and their wrappers
- See Also:
-
Method Summary
Modifier and TypeMethodDescription<T> Tdeserialize(String data, Class<T> targetClass) Deserializes a string to an object of the specified type.<T> List<T> deserializeList(String data, Class<T> itemClass) Deserializes a string to a list of objects.Gets the output format this serializer handles.getFormatInstructions(Class<?> targetClass) Generates format instructions for LLM to produce output in this format.getListFormatInstructions(Class<?> itemClass) Generates format instructions for list output.default StringSerializes an object with default formatting (pretty print enabled).Serializes an object to the target format.default <T> StringserializeList(List<T> items, Class<T> itemClass) Serializes a list with default formatting.<T> StringserializeList(List<T> items, Class<T> itemClass, boolean prettyPrint) Serializes a list of objects optimized for tabular formats.booleansupportsType(Class<?> dataClass) Checks if this format supports the given data structure.
-
Method Details
-
getFormat
OutputFormat getFormat()Gets the output format this serializer handles.- Returns:
- the output format
-
serialize
Serializes an object to the target format.- Parameters:
data- the object to serializeprettyPrint- whether to format output for readability- Returns:
- the serialized string representation
- Throws:
OutputSerializationException- if serialization fails
-
serialize
Serializes an object with default formatting (pretty print enabled).- Parameters:
data- the object to serialize- Returns:
- the serialized string representation
-
serializeList
Serializes a list of objects optimized for tabular formats. For formats like TOON, TONL, CSV this produces more efficient output.- Type Parameters:
T- the item type- Parameters:
items- the list of objects (should be uniform type)itemClass- the class of list items (for schema inference)prettyPrint- whether to format output for readability- Returns:
- the serialized string representation
-
serializeList
Serializes a list with default formatting. -
deserialize
Deserializes a string to an object of the specified type.- Type Parameters:
T- the target type- Parameters:
data- the serialized stringtargetClass- the target class- Returns:
- the deserialized object
- Throws:
OutputSerializationException- if deserialization fails
-
deserializeList
Deserializes a string to a list of objects.- Type Parameters:
T- the item type- Parameters:
data- the serialized stringitemClass- the class of list items- Returns:
- the deserialized list
-
getFormatInstructions
Generates format instructions for LLM to produce output in this format. Used in prompts to guide LLM response formatting.- Parameters:
targetClass- the expected output class (for schema hints)- Returns:
- format instructions string
-
getListFormatInstructions
Generates format instructions for list output.- Parameters:
itemClass- the expected item class- Returns:
- format instructions string
-
supportsType
Checks if this format supports the given data structure. For example, CSV doesn't support nested objects.- Parameters:
dataClass- the data class to check- Returns:
- true if supported
-