Documentation
¶
Index ¶
- func Convert(jsonBytes []byte) ([]byte, error)
- func ConvertAuto(data []byte) ([]byte, error)
- func ConvertAutoFile(path string) ([]byte, error)
- func ConvertAutoString(s string) (string, error)
- func ConvertAutoWithOptions(data []byte, opts ...ConverterOption) ([]byte, error)
- func ConvertFile(path string) ([]byte, error)
- func ConvertFileToWriter(path string, w io.Writer) error
- func ConvertFileWithOptions(path string, opts ...ConverterOption) ([]byte, error)
- func ConvertJSONC(jsoncBytes []byte) ([]byte, error)
- func ConvertJSONCString(jsoncStr string) (string, error)
- func ConvertJSONCWithOptions(jsoncBytes []byte, opts ...ConverterOption) ([]byte, error)
- func ConvertJSONL(jsonlBytes []byte) ([]byte, error)
- func ConvertJSONLStream(jsonlBytes []byte) ([]byte, error)
- func ConvertJSONLStreamString(jsonlStr string) (string, error)
- func ConvertJSONLStreamWithOptions(jsonlBytes []byte, opts ...ConverterOption) ([]byte, error)
- func ConvertJSONLString(jsonlStr string) (string, error)
- func ConvertJSONLWithOptions(jsonlBytes []byte, opts ...ConverterOption) ([]byte, error)
- func ConvertString(jsonStr string) (string, error)
- func ConvertToJSON(toonBytes []byte) ([]byte, error)
- func ConvertToJSONFile(path string) ([]byte, error)
- func ConvertToJSONFileString(path string) (string, error)
- func ConvertToJSONFromReader(r io.Reader) ([]byte, error)
- func ConvertToJSONString(toonStr string) (string, error)
- func ConvertToJSONWithOptions(toonBytes []byte, opts ...DecodeOption) ([]byte, error)
- func ConvertWithOptions(jsonBytes []byte, opts ...ConverterOption) ([]byte, error)
- func DecodeFromReader(r io.Reader) ([]byte, error)
- func DecodeToJSON(toon []byte) ([]byte, error)
- func DecodeToJSONString(toon string) (string, error)
- func DecodeToJSONWithOptions(toon []byte, opts DecodeOptions) ([]byte, error)
- func DetectFormat(data []byte) string
- func StripComments(jsonc []byte) ([]byte, error)
- type Converter
- func (c *Converter) Close() error
- func (c *Converter) ConvertAuto(data []byte) error
- func (c *Converter) ConvertJSON(jsonBytes []byte) error
- func (c *Converter) ConvertJSONC(jsoncBytes []byte) error
- func (c *Converter) ConvertJSONL(jsonlBytes []byte) error
- func (c *Converter) ConvertJSONLFromReader(r io.Reader) error
- func (c *Converter) ConvertJSONLStream(r io.Reader) error
- type ConverterOption
- type ConverterOptions
- type DecodeOption
- type DecodeOptions
- type Decoder
- type DecoderError
- type DecoderOptions
- type Encoder
- type EncoderOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertAuto ¶
ConvertAuto auto-detects and converts input format (JSON/JSONC/JSONL) to TOON.
func ConvertAutoFile ¶
ConvertAutoFile converts a file with auto-detected format to TOON bytes.
func ConvertAutoString ¶
ConvertAutoString converts input string to TOON with auto format detection.
func ConvertAutoWithOptions ¶
func ConvertAutoWithOptions(data []byte, opts ...ConverterOption) ([]byte, error)
ConvertAutoWithOptions converts to TOON with auto format detection and custom options.
func ConvertFile ¶
ConvertFile converts a JSON file to TOON bytes.
func ConvertFileToWriter ¶
ConvertFileToWriter converts a JSON file and writes to an io.Writer.
func ConvertFileWithOptions ¶
func ConvertFileWithOptions(path string, opts ...ConverterOption) ([]byte, error)
ConvertFileWithOptions converts a JSON file with custom options.
func ConvertJSONC ¶
ConvertJSONC converts JSONC (JSON with comments) to TOON bytes.
func ConvertJSONCString ¶
ConvertJSONCString converts a JSONC string to TOON string.
func ConvertJSONCWithOptions ¶
func ConvertJSONCWithOptions(jsoncBytes []byte, opts ...ConverterOption) ([]byte, error)
ConvertJSONCWithOptions converts JSONC to TOON with custom options.
func ConvertJSONL ¶
ConvertJSONL converts JSON Lines (JSONL) to TOON bytes.
func ConvertJSONLStream ¶
ConvertJSONLStream converts JSON Lines to TOON bytes using streaming mode. Each line is processed and written immediately, separated by "---". This is more memory-efficient for large JSONL files.
func ConvertJSONLStreamString ¶
ConvertJSONLStreamString converts JSON Lines string to TOON using streaming mode.
func ConvertJSONLStreamWithOptions ¶
func ConvertJSONLStreamWithOptions(jsonlBytes []byte, opts ...ConverterOption) ([]byte, error)
ConvertJSONLStreamWithOptions converts JSONL to TOON with streaming mode and custom options.
func ConvertJSONLString ¶
ConvertJSONLString converts a JSONL string to TOON string.
func ConvertJSONLWithOptions ¶
func ConvertJSONLWithOptions(jsonlBytes []byte, opts ...ConverterOption) ([]byte, error)
ConvertJSONLWithOptions converts JSONL to TOON with custom options.
func ConvertString ¶
ConvertString converts a JSON string to TOON string.
func ConvertToJSON ¶
ConvertToJSON converts TOON bytes to JSON bytes.
func ConvertToJSONFile ¶
ConvertToJSONFile converts a TOON file to JSON bytes.
func ConvertToJSONFileString ¶
ConvertToJSONFileString converts a TOON file to JSON string.
func ConvertToJSONFromReader ¶
ConvertToJSONFromReader converts TOON from an io.Reader to JSON bytes.
func ConvertToJSONString ¶
ConvertToJSONString converts TOON string to JSON string.
func ConvertToJSONWithOptions ¶
func ConvertToJSONWithOptions(toonBytes []byte, opts ...DecodeOption) ([]byte, error)
ConvertToJSONWithOptions converts TOON to JSON with custom options.
func ConvertWithOptions ¶
func ConvertWithOptions(jsonBytes []byte, opts ...ConverterOption) ([]byte, error)
ConvertWithOptions converts JSON bytes to TOON with custom options.
func DecodeFromReader ¶
DecodeFromReader reads TOON from reader and returns JSON bytes.
func DecodeToJSON ¶
DecodeToJSON reads TOON and returns JSON bytes.
func DecodeToJSONString ¶
DecodeToJSONString reads TOON and returns JSON string.
func DecodeToJSONWithOptions ¶
func DecodeToJSONWithOptions(toon []byte, opts DecodeOptions) ([]byte, error)
DecodeToJSONWithOptions converts TOON to JSON with custom options.
func DetectFormat ¶
DetectFormat detects the input format: JSON, JSONC, or JSONL.
func StripComments ¶
StripComments removes JSONC-style comments from JSONC content. Supports:
- Line comments: // comment to end of line
- Block comments: /* multi-line comment */
- Comments inside strings (not stripped)
Types ¶
type Converter ¶
type Converter struct {
// contains filtered or unexported fields
}
Converter provides streaming JSON/JSONC/JSONL to TOON conversion. It writes directly to an io.Writer without buffering the entire output.
func NewConverter ¶
NewConverter creates a new Converter that writes TOON to w.
func NewConverterWithOptions ¶
func NewConverterWithOptions(w io.Writer, opts ConverterOptions) *Converter
NewConverterWithOptions creates a new Converter with custom options.
func (*Converter) ConvertAuto ¶
ConvertAuto auto-detects and converts the input format (JSON/JSONC/JSONL) to TOON. It examines the input to determine the format:
- JSONC: contains // or /* */ comments
- JSONL: multiple lines of valid JSON objects
- JSON: standard JSON format (default)
func (*Converter) ConvertJSON ¶
ConvertJSON converts JSON bytes to TOON using streaming token-based parsing. It is memory-efficient for large JSON documents as it doesn't load the entire input into memory.
func (*Converter) ConvertJSONC ¶
ConvertJSONC converts JSONC (JSON with comments) to TOON. It first strips comments (// and /* */) and then converts the result.
func (*Converter) ConvertJSONL ¶
ConvertJSONL converts JSON Lines (JSONL) to TOON. Each line is parsed as a separate JSON value. If all lines are objects with the same keys, output is in tabular format.
func (*Converter) ConvertJSONLFromReader ¶
ConvertJSONLFromReader converts JSON Lines from an io.Reader to TOON. This is more memory-efficient than ConvertJSONL for large inputs.
type ConverterOption ¶
type ConverterOption func(*ConverterOptions)
ConverterOption configures the converter behavior.
func WithDelimiter ¶
func WithDelimiter(r rune) ConverterOption
WithDelimiter sets the table delimiter character.
func WithKeyFolding ¶
func WithKeyFolding(mode string) ConverterOption
WithKeyFolding sets the key folding mode.
func WithStrict ¶
func WithStrict(strict bool) ConverterOption
WithStrict enables or disables strict mode.
type ConverterOptions ¶
type ConverterOptions struct {
// Encoder configures the TOON encoder.
Encoder EncoderOptions
// Strict enables strict TOON spec validation.
// When true, enforces strict compliance with TOON specification.
// Defaults to true.
Strict bool
}
ConverterOptions configures the JSON to TOON converter.
func DefaultConverterOptions ¶
func DefaultConverterOptions() ConverterOptions
DefaultConverterOptions returns the default converter options.
type DecodeOption ¶
type DecodeOption func(*DecodeOptions)
DecodeOption configures the decoder behavior.
func WithDecodeStrict ¶
func WithDecodeStrict(strict bool) DecodeOption
WithDecodeStrict enables or disables strict validation when decoding.
func WithExpandPaths ¶
func WithExpandPaths(expand bool) DecodeOption
WithExpandPaths enables path expansion for reconstructing nested structures from folded keys.
type DecodeOptions ¶
DecodeOptions holds decoder configuration.
func DefaultDecodeOptions ¶
func DefaultDecodeOptions() DecodeOptions
DefaultDecodeOptions returns the default decoder options.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder reads TOON format and converts to JSON.
func NewDecoder ¶
NewDecoder creates a new Decoder reading from r. The decoder will use a default indent of 2 spaces and strict validation.
func NewDecoderWithOptions ¶
func NewDecoderWithOptions(r io.Reader, opts DecodeOptions) *Decoder
NewDecoderWithOptions creates a new Decoder with custom options. Use DefaultDecodeOptions() as a starting point and modify as needed.
func (*Decoder) Decode ¶
Decode reads TOON from the reader and returns a JSON-compatible value. The returned value will be one of:
- map[string]interface{} for objects
- []interface{} for arrays
- string, float64, bool, or nil for primitives
Returns (nil, nil) if the input is empty.
On error, the returned error will be a *DecoderError with context information including line numbers when available.
type DecoderError ¶
type DecoderError struct {
Type string // Error type (e.g., "InvalidSyntax", "CountMismatch")
Message string // Human-readable error message
Line int // Line number (1-indexed, 0 if unknown)
Column int // Column position (1-indexed, 0 if unknown)
Context string // Input snippet for context
}
DecoderError represents a decoding error with context information.
func NewDecoderError ¶
func NewDecoderError(errType, message string) *DecoderError
NewDecoderError creates a new DecoderError with context.
func NewDecoderErrorWithContext ¶
func NewDecoderErrorWithContext(errType, message string, line, column int, context string) *DecoderError
NewDecoderErrorWithContext creates a DecoderError with line/column context.
func (*DecoderError) Error ¶
func (e *DecoderError) Error() string
Error implements the error interface.
func (*DecoderError) Unwrap ¶
func (e *DecoderError) Unwrap() error
Unwrap returns the underlying error (for error wrapping compatibility).
type DecoderOptions ¶
DecoderOptions holds decoder configuration.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder writes TOON format to an io.Writer.
func NewEncoder ¶
NewEncoder creates a new Encoder writing to w.
func NewEncoderWithOptions ¶
func NewEncoderWithOptions(w io.Writer, opts EncoderOptions) *Encoder
NewEncoderWithOptions creates a new Encoder with custom options.
type EncoderOptions ¶
type EncoderOptions struct {
// Indent is the number of spaces per indentation level.
// Defaults to 2.
Indent int
// Delimiter is the character used to separate values in tabular arrays.
// Options: ',', '\t', '|'
// Defaults to ','.
Delimiter rune
// KeyFolding enables key folding: "off" or "safe".
// "safe" collapses nested single-key objects into dotted paths.
// Defaults to "off".
KeyFolding string
// FlattenDepth limits key folding depth. 0 means unlimited.
// Defaults to 0 (unlimited).
FlattenDepth int
}
EncoderOptions configures the TOON encoder behavior.
func DefaultEncoderOptions ¶
func DefaultEncoderOptions() EncoderOptions
DefaultEncoderOptions returns the default encoder options.