Documentation
¶
Overview ¶
Package toon encodes Go values to TOON (Token-Oriented Object Notation) and decodes TOON back.
Example ¶
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{{1, "Ada", "admin"}, {2, "Bob", "user"}}}
encoded, err := toon.MarshalString(in)
if err != nil {
panic(err)
}
fmt.Println(encoded)
var out Payload
if err := toon.UnmarshalString(encoded, &out); err != nil {
panic(err)
}
fmt.Printf("%+v\n", out)
}
Output: users[2]{id,name,role}: 1,Ada,admin 2,Bob,user {Users:[{ID:1 Name:Ada Role:admin} {ID:2 Name:Bob Role:user}]}
Index ¶
- func Decode(data []byte, opts ...DecoderOption) (any, error)
- func DecodeString(s string, opts ...DecoderOption) (any, error)
- func Marshal(v any, opts ...EncoderOption) ([]byte, error)
- func MarshalString(v any, opts ...EncoderOption) (string, error)
- func Unmarshal(data []byte, v any, opts ...DecoderOption) error
- func UnmarshalString(s string, v any, opts ...DecoderOption) error
- type Decoder
- type DecoderOption
- type Delimiter
- type Encoder
- type EncoderOption
- type Field
- type Object
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
func Decode(data []byte, opts ...DecoderOption) (any, error)
Decode parses the TOON document in data.
func DecodeString ¶
func DecodeString(s string, opts ...DecoderOption) (any, error)
DecodeString parses the TOON document s.
func Marshal ¶
func Marshal(v any, opts ...EncoderOption) ([]byte, error)
Marshal returns the TOON encoding of v.
func MarshalString ¶
func MarshalString(v any, opts ...EncoderOption) (string, error)
MarshalString encodes v as a TOON document string.
func Unmarshal ¶
func Unmarshal(data []byte, v any, opts ...DecoderOption) error
Unmarshal decodes the TOON document in data into v, which must be a non-nil pointer. Struct fields use `toon` struct tags, falling back to `json` tags, for naming, mirroring Marshal behavior.
func UnmarshalString ¶
func UnmarshalString(s string, v any, opts ...DecoderOption) error
UnmarshalString decodes the TOON document in s into v.
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder parses TOON documents into float64 numbers, map[string]any objects, and []any arrays. Go maps keep no insertion order, so document key order is lost. The zero value decodes with the default options. A Decoder is safe for concurrent use.
func NewDecoder ¶
func NewDecoder(opts ...DecoderOption) *Decoder
NewDecoder constructs a Decoder with the given options.
type DecoderOption ¶
type DecoderOption func(*decoderOptions)
DecoderOption configures a Decoder.
func WithDecoderIndent ¶
func WithDecoderIndent(spaces int) DecoderOption
WithDecoderIndent configures the expected indentation step. A value below 1 is ignored.
func WithStrictMode ¶
func WithStrictMode(strict bool) DecoderOption
WithStrictMode toggles strict decoding. With false, the decoder accepts exactly five deviations: a declared length that doesn't match the content, duplicate keys and field names (the last one wins), tab or misaligned indentation (each tab one level, spaces floored), blank lines inside an array or keyed tabular object, and a block whose first line is indented too deep, which then sets the block's depth. Every other decoding error applies in both modes.
type Delimiter ¶
type Delimiter rune
Delimiter identifies the character used to split field entries, inline array values, tabular row cells, and keyed entry-row cells.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder serializes Go values as TOON documents. The zero value encodes with the default options. An Encoder is safe for concurrent use.
func NewEncoder ¶
func NewEncoder(opts ...EncoderOption) *Encoder
NewEncoder constructs an Encoder using the supplied options.
type EncoderOption ¶
type EncoderOption func(*encoderOptions)
EncoderOption configures an Encoder.
func WithDelimiter ¶
func WithDelimiter(delimiter Delimiter) EncoderOption
WithDelimiter sets the delimiter that every emitted header declares. A value other than DelimiterComma, DelimiterTab, or DelimiterPipe is ignored.
func WithIndent ¶
func WithIndent(spaces int) EncoderOption
WithIndent configures the number of spaces used per indentation level. A value below 1 is ignored.
func WithTimeFormatter ¶
func WithTimeFormatter(formatter func(time.Time) string) EncoderOption
WithTimeFormatter specifies the formatter used for time.Time normalization. A nil formatter is ignored.
type Object ¶
type Object struct {
Fields []Field
}
Object preserves the encounter order of its fields, ensuring deterministic emission by the encoder.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
format
Package format implements the string, key, and number rendering rules of the TOON specification.
|
Package format implements the string, key, and number rendering rules of the TOON specification. |
|
parse
Package parse implements the lexical helpers the TOON decoder needs: unescaping quoted tokens, scanning for unquoted characters, and splitting delimiter-separated cell sequences.
|
Package parse implements the lexical helpers the TOON decoder needs: unescaping quoted tokens, scanning for unquoted characters, and splitting delimiter-separated cell sequences. |