package msgpack
import "github.com/vmihailenco/msgpack"
Example (MapStringInterface)¶
Code:
{
in := map[string]interface{}{"foo": 1, "hello": "world"}
b, err := msgpack.Marshal(in)
_ = err
var out map[string]interface{}
err = msgpack.Unmarshal(b, &out)
var outKeys []string
for k := range out {
outKeys = append(outKeys, k)
}
sort.Strings(outKeys)
fmt.Printf("err: %v\n", err)
for _, k := range outKeys {
fmt.Printf("out[\"%v\"]: %#v\n", k, out[k])
}
// Output: err: <nil>
// out["foo"]: 1
// out["hello"]: "world"
}
Output:
err: <nil> out["foo"]: 1 out["hello"]: "world"
Example (RecursiveMapStringInterface)¶
Code:
{
buf := &bytes.Buffer{}
enc := msgpack.NewEncoder(buf)
in := map[string]interface{}{"foo": map[string]interface{}{"hello": "world"}}
_ = enc.Encode(in)
dec := msgpack.NewDecoder(buf)
dec.DecodeMapFunc = func(d *msgpack.Decoder) (interface{}, error) {
n, err := d.DecodeMapLen()
if err != nil {
return nil, err
}
m := make(map[string]interface{}, n)
for i := 0; i < n; i++ {
mk, err := d.DecodeString()
if err != nil {
return nil, err
}
mv, err := d.DecodeInterface()
if err != nil {
return nil, err
}
m[mk] = mv
}
return m, nil
}
out, err := dec.DecodeInterface()
fmt.Printf("%v %#v\n", err, out)
// Output: <nil> map[string]interface {}{"foo":map[string]interface {}{"hello":"world"}}
}
Output:
<nil> map[string]interface {}{"foo":map[string]interface {}{"hello":"world"}}
Index ¶
- func Marshal(v ...interface{}) ([]byte, error)
- func Register(typ reflect.Type, enc encoderFunc, dec decoderFunc)
- func RegisterExt(id int8, value interface{})
- func Unmarshal(b []byte, v ...interface{}) error
- type CustomDecoder
- type CustomEncoder
- type Decoder
- func NewDecoder(r io.Reader) *Decoder
- func (d *Decoder) Decode(v ...interface{}) error
- func (d *Decoder) DecodeBool() (bool, error)
- func (d *Decoder) DecodeBytes() ([]byte, error)
- func (d *Decoder) DecodeBytesLen() (int, error)
- func (d *Decoder) DecodeFloat32() (float32, error)
- func (d *Decoder) DecodeFloat64() (float64, error)
- func (d *Decoder) DecodeInt() (int, error)
- func (d *Decoder) DecodeInt16() (int16, error)
- func (d *Decoder) DecodeInt32() (int32, error)
- func (d *Decoder) DecodeInt64() (int64, error)
- func (d *Decoder) DecodeInt8() (int8, error)
- func (d *Decoder) DecodeInterface() (interface{}, error)
- func (d *Decoder) DecodeMap() (interface{}, error)
- func (d *Decoder) DecodeMapLen() (int, error)
- func (d *Decoder) DecodeNil() error
- func (d *Decoder) DecodeSlice() ([]interface{}, error)
- func (d *Decoder) DecodeSliceLen() (int, error)
- func (d *Decoder) DecodeString() (string, error)
- func (d *Decoder) DecodeTime() (time.Time, error)
- func (d *Decoder) DecodeUint() (uint, error)
- func (d *Decoder) DecodeUint16() (uint16, error)
- func (d *Decoder) DecodeUint32() (uint32, error)
- func (d *Decoder) DecodeUint64() (uint64, error)
- func (d *Decoder) DecodeUint8() (uint8, error)
- func (d *Decoder) DecodeValue(v reflect.Value) error
- func (d *Decoder) Skip() error
- type Encoder
- func NewEncoder(w io.Writer) *Encoder
- func (e *Encoder) Encode(v ...interface{}) error
- func (e *Encoder) EncodeBool(value bool) error
- func (e *Encoder) EncodeBytes(v []byte) error
- func (e *Encoder) EncodeFloat32(n float32) error
- func (e *Encoder) EncodeFloat64(n float64) error
- func (e *Encoder) EncodeInt(v int) error
- func (e *Encoder) EncodeInt16(v int16) error
- func (e *Encoder) EncodeInt32(v int32) error
- func (e *Encoder) EncodeInt64(v int64) error
- func (e *Encoder) EncodeInt8(v int8) error
- func (e *Encoder) EncodeNil() error
- func (e *Encoder) EncodeSliceLen(l int) error
- func (e *Encoder) EncodeString(v string) error
- func (e *Encoder) EncodeTime(tm time.Time) error
- func (e *Encoder) EncodeUint(v uint) error
- func (e *Encoder) EncodeUint16(v uint16) error
- func (e *Encoder) EncodeUint32(v uint32) error
- func (e *Encoder) EncodeUint64(v uint64) error
- func (e *Encoder) EncodeUint8(v uint8) error
- func (e *Encoder) EncodeValue(v reflect.Value) error
- type Marshaler
- type Unmarshaler
Examples ¶
- package (MapStringInterface)
- package (RecursiveMapStringInterface)
- CustomEncoder
- Marshal
- RegisterExt
Functions ¶
func Marshal ¶
Example¶
Code:
{ b, err := msgpack.Marshal(true) if err != nil { panic(err) } fmt.Printf("%#v\n", b) // Output: var out bool err = msgpack.Unmarshal([]byte{0xc3}, &out) if err != nil { panic(err) } fmt.Println(out) // Output: []byte{0xc3} // true }
Output:
[]byte{0xc3} true
func Register ¶
func RegisterExt ¶
func RegisterExt(id int8, value interface{})
Example¶
Code:
{
msgpack.RegisterExt(1, myStruct{})
b, err := msgpack.Marshal(&myStruct{S: "string"})
if err != nil {
panic(err)
}
var v interface{}
err = msgpack.Unmarshal(b, &v)
if err != nil {
panic(err)
}
fmt.Printf("%#v", v)
// Output: msgpack_test.myStruct{S:"string"}
}
Output:
msgpack_test.myStruct{S:"string"}
func Unmarshal ¶
Types ¶
type CustomDecoder ¶
type CustomEncoder ¶
Example¶
Code:play
package main import ( "fmt" "gopkg.in/vmihailenco/msgpack.v2" ) type customStruct struct { S string N int } var ( _ msgpack.CustomEncoder = &customStruct{} _ msgpack.CustomDecoder = &customStruct{} ) func (s *customStruct) EncodeMsgpack(enc *msgpack.Encoder) error { return enc.Encode(s.S, s.N) } func (s *customStruct) DecodeMsgpack(dec *msgpack.Decoder) error { return dec.Decode(&s.S, &s.N) } func main() { b, err := msgpack.Marshal(&customStruct{S: "hello", N: 42}) if err != nil { panic(err) } var v customStruct err = msgpack.Unmarshal(b, &v) if err != nil { panic(err) } fmt.Printf("%#v", v) }
Output:
msgpack_test.customStruct{S:"hello", N:42}
type Decoder ¶
type Decoder struct { DecodeMapFunc func(*Decoder) (interface{}, error) // contains filtered or unexported fields }
func NewDecoder ¶
func (*Decoder) Decode ¶
func (*Decoder) DecodeBool ¶
func (*Decoder) DecodeBytes ¶
func (*Decoder) DecodeBytesLen ¶
func (*Decoder) DecodeFloat32 ¶
func (*Decoder) DecodeFloat64 ¶
func (*Decoder) DecodeInt ¶
func (*Decoder) DecodeInt16 ¶
func (*Decoder) DecodeInt32 ¶
func (*Decoder) DecodeInt64 ¶
func (*Decoder) DecodeInt8 ¶
func (*Decoder) DecodeInterface ¶
Decodes value into interface. Possible value types are:
- nil,
- int64,
- uint64,
- bool,
- float32 and float64,
- string,
- slices of any of the above,
- maps of any of the above.
func (*Decoder) DecodeMap ¶
func (*Decoder) DecodeMapLen ¶
func (*Decoder) DecodeNil ¶
func (*Decoder) DecodeSlice ¶
func (*Decoder) DecodeSliceLen ¶
func (*Decoder) DecodeString ¶
func (*Decoder) DecodeTime ¶
func (*Decoder) DecodeUint ¶
func (*Decoder) DecodeUint16 ¶
func (*Decoder) DecodeUint32 ¶
func (*Decoder) DecodeUint64 ¶
func (*Decoder) DecodeUint8 ¶
func (*Decoder) DecodeValue ¶
func (*Decoder) Skip ¶
Skip skips next value.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
func NewEncoder ¶
func (*Encoder) Encode ¶
func (*Encoder) EncodeBool ¶
func (*Encoder) EncodeBytes ¶
func (*Encoder) EncodeFloat32 ¶
func (*Encoder) EncodeFloat64 ¶
func (*Encoder) EncodeInt ¶
func (*Encoder) EncodeInt16 ¶
func (*Encoder) EncodeInt32 ¶
func (*Encoder) EncodeInt64 ¶
func (*Encoder) EncodeInt8 ¶
func (*Encoder) EncodeNil ¶
func (*Encoder) EncodeSliceLen ¶
func (*Encoder) EncodeString ¶
func (*Encoder) EncodeTime ¶
func (*Encoder) EncodeUint ¶
func (*Encoder) EncodeUint16 ¶
func (*Encoder) EncodeUint32 ¶
func (*Encoder) EncodeUint64 ¶
func (*Encoder) EncodeUint8 ¶
func (*Encoder) EncodeValue ¶
type Marshaler ¶
Deprecated. Use CustomEncoder.
type Unmarshaler ¶
Deprecated. Use CustomDecoder.
Source Files ¶
custom.go decode.go decode_number.go encode.go encode_number.go ext.go map.go msgpack.go slice.go tags.go time.go typeinfo.go
Directories ¶
Path | Synopsis |
---|---|
codes |
- Version
- v2.4.5+incompatible
- Published
- Dec 13, 2015
- Platform
- js/wasm
- Imports
- 12 packages
- Last checked
- now –
Tools for package owners.