https://github.com/devalexandre/toon-go
https://github.com/devalexandre/toon-go
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/devalexandre/toon-go
- Owner: devalexandre
- Created: 2025-11-09T20:49:28.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-11-10T00:18:54.000Z (4 months ago)
- Last Synced: 2025-11-10T01:17:30.629Z (4 months ago)
- Language: Go
- Size: 16.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# TOON - Token Optimized Object Notation (Go Implementation)
TOON is a data serialization format designed as a more token-efficient and human-readable alternative to JSON. It's particularly well-suited for AI applications, APIs, and scenarios where reducing token usage is important.
For the complete TOON specification, see: https://github.com/toon-format/spec/blob/main/SPEC.md
## Features
- **Token Efficient**: Uses fewer tokens than JSON for equivalent data
- **Human Readable**: Clean, intuitive syntax
- **Tabular Arrays**: Optimized format for array-of-objects data
- **Backward Compatible**: Can represent the same data as JSON
- **Go Integration**: Native Go support with encoding/decoding
## Installation
```bash
go get github.com/devalexandre/toon-go
```
## Quick Start
```go
package main
import (
"fmt"
"github.com/devalexandre/toon-go"
)
func main() {
data := map[string]interface{}{
"name": "John Doe",
"age": 30,
"active": true,
"tags": []string{"developer", "golang"},
}
// Marshal to TOON format
toonData, err := toon.Marshal(data)
if err != nil {
panic(err)
}
fmt.Println(string(toonData))
// Output:
// name: "John Doe"
// age: 30
// active: true
// tags[2]: "developer","golang"
}
```
## TOON Syntax
### Basic Key-Value Pairs
```
name: "John Doe"
age: 30
active: true
balance: 1234.56
```
### Nested Objects
```
user:
id: 123
name: "John Doe"
profile:
email: "john@example.com"
settings:
theme: "dark"
notifications: true
```
### Arrays
Regular arrays:
```
numbers[5]: 1,2,3,4,5
strings[3]: "apple","banana","cherry"
```
### Tabular Arrays (TOON's Key Feature)
TOON excels at representing arrays of objects with consistent fields:
```
employees[3]{id,name,role,salary,active}:
1,"Alice Smith","Developer",75000,true
2,"Bob Johnson","Designer",65000,true
3,"Carol Brown","Manager",85000,false
```
This tabular format is much more token-efficient than JSON's verbose array-of-objects representation.
## API
### Marshal
```go
func Marshal(v interface{}) ([]byte, error)
```
Marshals a Go value to TOON format.
### MarshalIndent
```go
func MarshalIndent(v interface{}, indent string) ([]byte, error)
```
Marshals a Go value to TOON format with custom indentation.
### Unmarshal
```go
func Unmarshal(data []byte, v interface{}) error
```
Unmarshals TOON data into a Go value.
## Examples
See `example/toon_example.go` for comprehensive usage examples.
## Benefits Over JSON
1. **Token Efficiency**: Up to 40% fewer tokens for typical data
2. **Better Readability**: Cleaner syntax without excessive brackets
3. **Optimized Arrays**: Tabular format for array-of-objects data
4. **AI-Friendly**: Designed with LLM tokenization in mind
5. **Backward Compatible**: Can represent all JSON data
## Use Cases
- **AI/LLM Applications**: Reduce token usage in prompts and responses
- **APIs**: More efficient data transmission
- **Configuration Files**: Cleaner, more readable configs
- **Data Logging**: Compact log data representation
- **Microservices**: Efficient inter-service communication
## License
MIT
## Contributing
Contributions welcome! Please see CONTRIBUTING.md for details.