Token-Oriented Object Notation is a compact, human-readable format designed for passing structured data to Large Language Models with significantly reduced token usage.
JSON (verbose):
{
"users": [
{ "id": 1, "name": "Alice", "role": "admin" },
{ "id": 2, "name": "Bob", "role": "user" }
]
}TOON (compact):
users[2]{id,name,role}:
1,Alice,admin
2,Bob,user
package main
import (
"fmt"
"github.com/toon-format/toon-go"
)
type User struct {
ID int `toon:"id"`
Name string `toon:"name"`
Role string `toon:"role"`
}
type Payload struct {
Users []User `toon:"users"`
}
func main() {
in := Payload{
Users: []User{
{ID: 1, Name: "Alice", Role: "admin"},
{ID: 2, Name: "Bob", Role: "user"},
},
}
encoded, err := toon.Marshal(in, toon.WithLengthMarkers(true))
if err != nil {
panic(err)
}
fmt.Println(string(encoded))
var out Payload
if err := toon.Unmarshal(encoded, &out); err != nil {
panic(err)
}
fmt.Printf("first user: %+v\n", out.Users[0])
}Unmarshal can populate dynamic maps, mimicking the encoding/json package:
var doc map[string]any
if err := toon.Unmarshal(encoded, &doc); err != nil {
panic(err)
}
fmt.Printf("users: %#v\n", doc["users"])If you do not have a destination struct, use Decode for a dynamic representation:
package main
import (
"fmt"
"github.com/toon-format/toon-go"
)
func main() {
raw := []byte("users[2]{id,name,role}:\n 1,Alice,admin\n 2,Bob,user\n")
decoded, err := toon.Decode(raw)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", decoded)
}For more runnable samples, explore the programs in ./examples.
Interested in implementing TOON for Go? Check out the specification and feel free to contribute!
MIT License © 2025-PRESENT Johann Schopplich