Skip to content

Token-Oriented Object Notation for Go with encoding / decoding

License

Notifications You must be signed in to change notification settings

sstraus/toon_go

Repository files navigation

TOON Go Library

Go Reference Go Report Card CI License: MIT

A Go implementation of TOON (Token-Oriented Object Notation) format encoder and decoder, compliant with TOON Specification v2.0.

Encoder & Decoder Complete

Currently implemented:

  • ✅ Complete encoder with all three array formats (inline, tabular, list)
  • ✅ Complete decoder with structural parsing
  • ✅ Primitive types (nil, bool, int, float, string)
  • ✅ Objects (maps) with nested support
  • ✅ Arrays with format auto-detection
  • ✅ Custom delimiters and length markers
  • ✅ String quoting and escaping
  • ✅ Round-trip encoding/decoding
  • ✅ Official specification fixtures (340/340 passing)
  • ✅ Comprehensive test suite (1,088 tests total)
  • ✅ 83.1% code coverage

About TOON

TOON is a compact data format optimized for LLM token efficiency, achieving 30-60% token reduction compared to JSON while maintaining readability.

Key Features

  • Token Efficient: 30-60% fewer tokens than JSON
  • Human Readable: Indentation-based structure like YAML
  • Three Array Formats:
    • Inline: tags[2]: a,b (for primitives)
    • Tabular: users[2]{name,age}: Alice,30 / Bob,25 (for uniform objects)
    • List: items[2]: - item1 / - item2 (for mixed/nested)

Implementation Highlights

This implementation is built with production-grade quality and maintainability in mind:

Superior Code Quality

  • Low Complexity: Average cyclomatic complexity of 5.39 across all functions
  • Maximum Complexity: Highest production function complexity is only 17
  • Well-Structured: 60+ focused helper functions following SOLID principles
  • Thoroughly Refactored: All complex functions decomposed for readability

Exceptional Test Coverage

  • Comprehensive Testing: 1,088 tests covering all functionality
  • High Coverage: 83.1% code coverage with detailed edge case handling
  • Specification Compliant: 340/340 official TOON fixtures passing
  • Continuous Integration: Automated testing on every commit

Enterprise-Grade Reliability

  • Battle-Tested: Handles complex nested structures and edge cases
  • Zero Failures: All tests passing throughout development
  • Well-Documented: Extensive inline comments and examples
  • Maintainable: Clean architecture for easy debugging and extension

Perfect for: Production systems requiring high reliability, maintainability, and comprehensive testing.

Installation

Requires Go 1.22 or newer.

go get github.com/sstraus/toon_go/toon@latest

Usage

Encoding & Decoding

package main

import (
    "bytes"
    "fmt"
    "strings"
    "github.com/sstraus/toon_go/toon"
)

func main() {
    // Encoding: Simple object
    data := map[string]interface{}{
        "name": "Alice",
        "age":  30,
    }

    // Using io.Writer
    var buf bytes.Buffer
    err := toon.Marshal(data, &buf)
    if err != nil {
        panic(err)
    }
    fmt.Println(buf.String())
    // Output:
    // age: 30
    // name: Alice

    // Or use string convenience function
    result, err := toon.MarshalToString(data)
    if err != nil {
        panic(err)
    }
    fmt.Println(result)
    // Output:
    // age: 30
    // name: Alice

    // Decoding: Read from io.Reader
    input := `name: Bob
age: 25`

    var decoded map[string]interface{}
    err = toon.Unmarshal(strings.NewReader(input), &decoded)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%+v\n", decoded)
    // Output: map[age:25 name:Bob]

    // Or use string convenience function
    var decoded2 map[string]interface{}
    err = toon.UnmarshalFromString(input, &decoded2)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%+v\n", decoded2)
    // Output: map[age:25 name:Bob]
}

Arrays

// Inline array (primitives)
data := map[string]interface{}{
    "tags": []interface{}{"go", "toon", "llm"},
}

result, _ := toon.MarshalToString(data)
fmt.Println(result)
// Output: tags[3]: go,toon,llm

// Tabular array (uniform objects)
users := map[string]interface{}{
    "users": []interface{}{
        map[string]interface{}{"name": "Alice", "age": 30},
        map[string]interface{}{"name": "Bob", "age": 25},
    },
}

result, _ = toon.MarshalToString(users)
fmt.Println(result)
// Output:
// users[2]{age,name}:
//   30,Alice
//   25,Bob

Functional Options

TOON Go uses the functional options pattern for clean, flexible configuration:

// Encoding with multiple options
data := map[string]interface{}{
    "values": []interface{}{1, 2, 3},
}

result, _ := toon.MarshalToString(data,
    toon.WithIndent(4),           // 4 spaces instead of 2
    toon.WithDelimiter("\t"),     // Tab delimiter
    toon.WithLengthMarker("#"),   // Prefix length with #
)
fmt.Println(result)
// Output: values[#3	]: 1	2	3

// Decoding with options
input := "name: Alice\nage: 30"
var result map[string]interface{}

err := toon.UnmarshalFromString(input, &result,
    toon.WithStrictDecoding(false),  // Disable strict mode
    toon.WithExpandPaths("safe"),    // Expand dotted keys
)

Available Encoding Options:

  • WithIndent(n) - Set indentation size
  • WithDelimiter(s) - Set array delimiter ("," | "\t" | "|")
  • WithLengthMarker(s) - Set length marker prefix
  • WithFlattenPaths(bool) - Enable path flattening
  • WithFlattenDepth(n) - Limit flattening depth
  • WithStrict(bool) - Enable strict collision detection

Available Decoding Options:

  • WithStrictDecoding(bool) - Enable strict validation
  • WithIndentSize(n) - Expected indent size
  • WithExpandPaths(mode) - Expand dotted keys ("off" | "safe")
  • WithKeyMode(mode) - Key decoding mode

## Project Structure

toon/ ├── doc.go # Package documentation ├── api.go # Public API (Marshal/Unmarshal) ├── types.go # Type definitions ├── errors.go # Error types ├── orderedmap.go # Ordered map implementation │ ├── encode.go # Encoding entry point ├── encode_objects.go # Object encoding ├── encode_arrays.go # Array format logic ├── encode_primitives.go # Primitive encoding │ ├── decode.go # Decoding entry point ├── decode_parser.go # Structural/indentation-based parser ├── decode_tokens.go # Token parser │ ├── options.go # Option types ├── writer.go # Output writer ├── utils.go # Utilities ├── constants.go # Format constants │ ├── toon_test.go # Encoder tests ├── decode_test.go # Decoder tests └── *_test.go # Additional test files


## Testing

The implementation includes comprehensive test coverage:

```bash
# Run all tests
go test

# Run with verbose output
go test -v

# Check coverage
go test -cover

Test Status:

  • ✅ 340/340 specification fixtures passing
  • ✅ 1,088 total tests passing
  • ✅ 83.1% code coverage
  • ✅ Average complexity: 5.39 (industry-leading)

Continuous integration runs the full test suite and publishes coverage on every push and pull request via GitHub Actions (.github/workflows/ci.yml).

Versioning

The library follows Semantic Versioning. The current release number is exposed programmatically via the toon.Version constant, and human-readable release notes are maintained in CHANGELOG.md.

Contributing

Contributions are welcome! Please review CONTRIBUTING.md for development guidelines before opening an issue or pull request.

Specification

This implementation follows TOON Specification v2.0.

License

MIT

Author

Created by Stefano Straus in 2025.

Based on Johann Schopplich specifications.

Packages

No packages published

Languages