Skip to content

ason-lab/ason-go

Repository files navigation

ason-go

License: MIT Go

High-performance Go support for ASON, a schema-driven format for compact structured data.

中文文档

Why ASON

ASON writes field names once and stores rows positionally:

[
  {"id": 1, "name": "Alice", "active": true},
  {"id": 2, "name": "Bob", "active": false}
]
[{id@int,name@str,active@bool}]:(1,Alice,true),(2,Bob,false)

That reduces repeated keys, payload size, and often parsing cost.

Highlights

  • Standard library only
  • Current API uses Encode / Decode, not the older Marshal / Unmarshal names
  • Text, pretty text, and binary formats
  • Struct tags via ason:"...", with json tag fallback
  • No native map / dictionary field syntax; model key-value data as slices of entry structs
  • Good fit for LLM payloads, internal services, logs, and fixtures

Install

go get github.com/ason-lab/ason-go

Quick Start

package main

import (
    "fmt"
    ason "github.com/ason-lab/ason-go"
)

type User struct {
    ID     int64  `ason:"id"`
    Name   string `ason:"name"`
    Active bool   `ason:"active"`
}

func main() {
    user := User{ID: 1, Name: "Alice", Active: true}

    text, _ := ason.Encode(&user)
    fmt.Println(string(text))
    // {id,name,active}:(1,Alice,true)

    typed, _ := ason.EncodeTyped(&user)
    fmt.Println(string(typed))
    // {id@int,name@str,active@bool}:(1,Alice,true)

    var decoded User
    _ = ason.Decode(text, &decoded)
}

Encode a slice

users := []User{
    {ID: 1, Name: "Alice", Active: true},
    {ID: 2, Name: "Bob", Active: false},
}

text, _ := ason.Encode(users)
typed, _ := ason.EncodeTyped(users)

var decoded []User
_ = ason.Decode(text, &decoded)

Pretty and binary output

pretty, _ := ason.EncodePretty(users)
prettyTyped, _ := ason.EncodePrettyTyped(users)
bin, _ := ason.EncodeBinary(users)

var decoded []User
_ = ason.DecodeBinary(bin, &decoded)

Model key-value data with entry structs

type EnvEntry struct {
    Key   string `ason:"key"`
    Value string `ason:"value"`
}

type Config struct {
    Name string     `ason:"name"`
    Env  []EnvEntry `ason:"env"`
}

Typed ASON output:

{name@str,env@[{key@str,value@str}]}:(api,[(RUST_LOG,debug),(PORT,8080)])

Current API

Function Purpose
Encode / EncodeTyped Encode to text
Decode Decode from text
EncodePretty / EncodePrettyTyped Pretty text output
EncodeBinary Encode to binary
DecodeBinary Decode from binary

Run Examples

go test ./...
go run ./examples/basic
go run ./examples/complex
go run ./examples/bench

Contributors

Benchmarks

Run:

go run ./examples/bench

The benchmark output now follows the same layout as the C and C++ versions:

Serialize:   JSON    16.22ms | ASON    16.80ms (1x) | BIN    15.02ms (1.1x)
Deserialize: JSON   111.90ms | ASON    35.50ms (3.2x) | BIN    35.10ms (3.2x)
Size:        JSON   218737 B | ASON    84861 B (39%) | BIN    85282 B (39%)

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages