Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "log" "github.com/bep/tmc" ) func main() { m1 := map[string]interface{}{"num": 42} c, err := tmc.New() if err != nil { log.Fatal(err) } data, err := c.Marshal(m1) if err != nil { log.Fatal(err) } m2 := make(map[string]interface{}) err = c.Unmarshal(data, &m2) if err != nil { log.Fatal(err) } num := m2["num"] fmt.Printf("%v (%T)", num, num) }
Output: 42 (int)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultTypeAdapters contains the default set of type adapters. DefaultTypeAdapters = []Adapter{ NewAdapter(time.Now(), nil, nil), NewAdapter( 3*time.Hour, func(s string) (interface{}, error) { return time.ParseDuration(s) }, func(v interface{}) (string, error) { return v.(time.Duration).String(), nil }, ), NewAdapter(big.NewRat(1, 2), nil, nil), NewAdapter( int(32), func(s string) (interface{}, error) { return strconv.Atoi(s) }, func(v interface{}) (string, error) { return strconv.Itoa(v.(int)), nil }, ), } )
var JSONMarshaler = new(jsonMarshaler)
JSONMarshaler encodes and decodes JSON and is the default used in this codec.
Functions ¶
func WithMarshalUnmarshaler ¶
func WithMarshalUnmarshaler(marshaler MarshalUnmarshaler) func(c *Codec) error
WithMarshalUnmarshaler sets the MarshalUnmarshaler to use. Default is JSONMarshaler.
Example ¶
package main import ( "fmt" "log" "github.com/bep/tmc" yaml "gopkg.in/yaml.v2" ) func main() { m1 := map[string]interface{}{"num": 42} c, err := tmc.New(tmc.WithMarshalUnmarshaler(new(yamlMarshaler))) if err != nil { log.Fatal(err) } data, err := c.Marshal(m1) if err != nil { log.Fatal(err) } m2 := make(map[string]interface{}) err = c.Unmarshal(data, &m2) if err != nil { log.Fatal(err) } num := m2["num"] fmt.Printf("%v (%T)", num, num) } type yamlMarshaler int func (yamlMarshaler) Marshal(v interface{}) ([]byte, error) { return yaml.Marshal(v) } func (yamlMarshaler) Unmarshal(b []byte, v interface{}) error { return yaml.Unmarshal(b, v) }
Output: 42 (int)
func WithTypeAdapters ¶
WithTypeAdapters sets the type adapters to use. Note that if more than one adapter exists for the same type, the last one will win. This means that if you want to use the default adapters, but override some of them, you can do:
adapters := append(typedmapcodec.DefaultTypeAdapters, mycustomAdapters ...) codec := typedmapcodec.New(WithTypeAdapters(adapters))
func WithTypeSep ¶
WithTypeSep sets the separator to use before the type information encoded in the key field. Default is "|".
Types ¶
type Adapter ¶
type Adapter interface { FromString(s string) (interface{}, error) MarshalText() (text []byte, err error) Type() reflect.Type Wrap(v interface{}) Adapter }
Adapter wraps a type to preserve type information when encoding and decoding a map.
The simples way to create new adapters is via the NewAdapter function.
func NewAdapter ¶
func NewAdapter( target interface{}, fromString func(s string) (interface{}, error), toString func(v interface{}) (string, error)) Adapter
NewAdapter creates a new adapter that wraps the target type.
fromString can be omitted if target implements encoding.TextUnmarshaler. toString can be omitted if target implements encoding.TextMarshaler.
It will panic if it can not be created.
type Codec ¶
type Codec struct {
// contains filtered or unexported fields
}
Codec provides methods to marshal and unmarshal a Go map while preserving type information.