Documentation
¶
Overview ¶
Package gotoon provides a Token-Optimized Object Notation (TOON) encoder and decoder. TOON is designed to reduce token usage when sending data to LLMs while maintaining full round-trip fidelity.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Diff ¶
Diff estimates token savings between JSON and TOON formats. Returns a map with json_chars, toon_chars, saved_chars, and savings_percent.
Types ¶
type ArrayFlattener ¶
type ArrayFlattener struct {
// contains filtered or unexported fields
}
ArrayFlattener handles flattening of nested objects in arrays.
func NewArrayFlattener ¶
func NewArrayFlattener(maxDepth int) *ArrayFlattener
NewArrayFlattener creates a new ArrayFlattener with the specified max depth.
func (*ArrayFlattener) Flatten ¶
func (f *ArrayFlattener) Flatten(items []any) *FlattenedData
Flatten converts an array of objects with nested structures into a flat table format.
func (*ArrayFlattener) HasNestedObjects ¶
func (f *ArrayFlattener) HasNestedObjects(items []any) bool
HasNestedObjects checks if any item in the array has nested objects.
type ArrayUnflattener ¶
type ArrayUnflattener struct{}
ArrayUnflattener handles unflattening of flat table data back into nested objects.
func NewArrayUnflattener ¶
func NewArrayUnflattener() *ArrayUnflattener
NewArrayUnflattener creates a new ArrayUnflattener.
type Config ¶
type Config struct {
// MinRowsForTable is the minimum number of items required to use table format.
// Arrays with fewer items will be encoded as regular YAML-like objects.
MinRowsForTable int
// MaxFlattenDepth controls how many levels deep to flatten nested objects.
// Objects nested deeper than this will be JSON-encoded as strings.
MaxFlattenDepth int
// EscapeStyle determines how to escape special characters in string values.
// Currently only "backslash" is supported.
EscapeStyle string
// Omit specifies which value types to omit from output.
// Supported values: "null", "empty", "false", "all"
Omit []string
// OmitKeys specifies keys that should always be omitted from output.
OmitKeys []string
// KeyAliases maps long key names to shorter aliases to save tokens.
KeyAliases map[string]string
// DateFormat specifies the format for time.Time objects and ISO date strings.
// Uses Go's time format syntax. When empty, dates are passed through as-is.
DateFormat string
// TruncateStrings specifies the maximum length for string values.
// Strings exceeding this length will be truncated with "...".
// When 0, strings are not truncated.
TruncateStrings int
// NumberPrecision specifies the maximum decimal places for float values.
// When -1, floats are passed through as-is.
NumberPrecision int
}
Config holds configuration options for TOON encoding and decoding.
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a Config with sensible defaults.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder handles decoding TOON format strings to Go data structures.
func NewDecoder ¶
NewDecoder creates a new Decoder with the given configuration.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder handles encoding Go data structures to TOON format.
func NewEncoder ¶
NewEncoder creates a new Encoder with the given configuration.
type FlattenedData ¶
FlattenedData represents flattened array data with columns and rows.