msgpack

package module
Version: v2.4.12+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2016 License: BSD-3-Clause Imports: 12 Imported by: 1

README

MessagePack encoding for Golang Build Status

Supports:

  • Primitives, arrays, maps, structs, time.Time and interface{}.
  • Appengine *datastore.Key and datastore.Cursor.
  • CustomEncoder/CustomDecoder interfaces for custom encoding.
  • Extensions to encode type information.
  • Fields renaming, e.g. msgpack:"my_field_name".
  • Structs inlining, e.g. msgpack:",inline".
  • Omitempty flag, e.g. msgpack:",omitempty".

API docs: http://godoc.org/gopkg.in/vmihailenco/msgpack.v2. Examples: http://godoc.org/gopkg.in/vmihailenco/msgpack.v2#pkg-examples.

Installation

Install:

go get gopkg.in/vmihailenco/msgpack.v2

Quickstart

func ExampleMarshal() {
	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
}

Benchmark

BenchmarkStructVmihailencoMsgpack-4	  200000	     11430 ns/op	    3136 B/op	      26 allocs/op
BenchmarkStructUgorjiGoMsgpack-4   	  100000	     14731 ns/op	    3616 B/op	      70 allocs/op
BenchmarkStructUgorjiGoCodec-4     	  100000	     19947 ns/op	    7362 B/op	      29 allocs/op
BenchmarkStructJSON-4              	   30000	     56042 ns/op	    7896 B/op	      26 allocs/op
BenchmarkStructGOB-4               	   20000	     76835 ns/op	   14665 B/op	     278 allocs/op

Howto

Please go through examples to get an idea how to use this package.

Documentation

Overview

Example (DecodeMapStringString)
decodedMap := make(map[string]string)
buf := &bytes.Buffer{}

m := map[string]string{"foo1": "bar1", "foo2": "bar2", "foo3": "bar3"}
keys := []string{"foo1", "foo3", "foo2"}

encodedMap, err := encodeMap(m, keys...)
if err != nil {
	panic(err)
}

_, err = buf.Write(encodedMap)
if err != nil {
	panic(err)
}

decoder := msgpack.NewDecoder(buf)

n, err := decoder.DecodeMapLen()
if err != nil {
	panic(err)
}

for i := 0; i < n; i++ {
	key, err := decoder.DecodeString()
	if err != nil {
		panic(err)
	}
	value, err := decoder.DecodeString()
	if err != nil {
		panic(err)
	}
	decodedMap[key] = value
}

for _, key := range keys {
	fmt.Printf("%#v: %#v, ", key, decodedMap[key])
}
Output:

"foo1": "bar1", "foo3": "bar3", "foo2": "bar2",
Example (EncodeMapStringString)
buf := &bytes.Buffer{}

m := map[string]string{"foo1": "bar1", "foo2": "bar2", "foo3": "bar3"}
keys := []string{"foo1", "foo3"}

encodedMap, err := encodeMap(m, keys...)
if err != nil {
	panic(err)
}

_, err = buf.Write(encodedMap)
if err != nil {
	panic(err)
}

decoder := msgpack.NewDecoder(buf)
value, err := decoder.DecodeMap()
if err != nil {
	panic(err)
}

decodedMapValue := value.(map[interface{}]interface{})

for _, key := range keys {
	fmt.Printf("%#v: %#v, ", key, decodedMapValue[key])
}
Output:

"foo1": "bar1", "foo3": "bar3",
Example (MapStringInterface)
in := map[string]interface{}{"foo": 1, "hello": "world"}
b, err := msgpack.Marshal(in)
if err != nil {
	panic(err)
}

var out map[string]interface{}
err = msgpack.Unmarshal(b, &out)
if err != nil {
	panic(err)
}

fmt.Println("foo =", out["foo"])
fmt.Println("hello =", out["hello"])
Output:

foo = 1
hello = world
Example (RecursiveMapStringInterface)
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"}}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v ...interface{}) ([]byte, error)
Example
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

func Register

func Register(typ reflect.Type, enc encoderFunc, dec decoderFunc)

Register registers encoder and decoder functions for a type. In most cases you should prefer implementing CustomEncoder and CustomDecoder interfaces.

func RegisterExt

func RegisterExt(id int8, value interface{})
Example
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"}

func Unmarshal

func Unmarshal(b []byte, v ...interface{}) error

Types

type CustomDecoder

type CustomDecoder interface {
	DecodeMsgpack(*Decoder) error
}

type CustomEncoder

type CustomEncoder interface {
	EncodeMsgpack(*Encoder) error
}
Example
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 {
	// TODO: add map len arg
	DecodeMapFunc func(*Decoder) (interface{}, error)
	// contains filtered or unexported fields
}

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(v ...interface{}) error

func (*Decoder) DecodeBool

func (d *Decoder) DecodeBool() (bool, error)

func (*Decoder) DecodeBytes

func (d *Decoder) DecodeBytes() ([]byte, error)

func (*Decoder) DecodeBytesLen

func (d *Decoder) DecodeBytesLen() (int, error)

func (*Decoder) DecodeFloat32

func (d *Decoder) DecodeFloat32() (float32, error)

func (*Decoder) DecodeFloat64

func (d *Decoder) DecodeFloat64() (float64, error)

func (*Decoder) DecodeInt

func (d *Decoder) DecodeInt() (int, error)

func (*Decoder) DecodeInt16

func (d *Decoder) DecodeInt16() (int16, error)

func (*Decoder) DecodeInt32

func (d *Decoder) DecodeInt32() (int32, error)

func (*Decoder) DecodeInt64

func (d *Decoder) DecodeInt64() (int64, error)

func (*Decoder) DecodeInt8

func (d *Decoder) DecodeInt8() (int8, error)

func (*Decoder) DecodeInterface

func (d *Decoder) DecodeInterface() (interface{}, error)

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 (d *Decoder) DecodeMap() (interface{}, error)

func (*Decoder) DecodeMapLen

func (d *Decoder) DecodeMapLen() (int, error)

func (*Decoder) DecodeNil

func (d *Decoder) DecodeNil() error

func (*Decoder) DecodeSlice

func (d *Decoder) DecodeSlice() ([]interface{}, error)

func (*Decoder) DecodeSliceLen

func (d *Decoder) DecodeSliceLen() (int, error)

func (*Decoder) DecodeString

func (d *Decoder) DecodeString() (string, error)

func (*Decoder) DecodeTime

func (d *Decoder) DecodeTime() (time.Time, error)

func (*Decoder) DecodeUint

func (d *Decoder) DecodeUint() (uint, error)

func (*Decoder) DecodeUint16

func (d *Decoder) DecodeUint16() (uint16, error)

func (*Decoder) DecodeUint32

func (d *Decoder) DecodeUint32() (uint32, error)

func (*Decoder) DecodeUint64

func (d *Decoder) DecodeUint64() (uint64, error)

func (*Decoder) DecodeUint8

func (d *Decoder) DecodeUint8() (uint8, error)

func (*Decoder) DecodeValue

func (d *Decoder) DecodeValue(v reflect.Value) error

func (*Decoder) Skip

func (d *Decoder) Skip() error

Skip skips next value.

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(v ...interface{}) error

func (*Encoder) EncodeArrayLen

func (e *Encoder) EncodeArrayLen(l int) error

func (*Encoder) EncodeBool

func (e *Encoder) EncodeBool(value bool) error

func (*Encoder) EncodeBytes

func (e *Encoder) EncodeBytes(v []byte) error

func (*Encoder) EncodeFloat32

func (e *Encoder) EncodeFloat32(n float32) error

func (*Encoder) EncodeFloat64

func (e *Encoder) EncodeFloat64(n float64) error

func (*Encoder) EncodeInt

func (e *Encoder) EncodeInt(v int) error

func (*Encoder) EncodeInt16

func (e *Encoder) EncodeInt16(v int16) error

func (*Encoder) EncodeInt32

func (e *Encoder) EncodeInt32(v int32) error

func (*Encoder) EncodeInt64

func (e *Encoder) EncodeInt64(v int64) error

func (*Encoder) EncodeInt8

func (e *Encoder) EncodeInt8(v int8) error

func (*Encoder) EncodeMapLen

func (e *Encoder) EncodeMapLen(l int) error

func (*Encoder) EncodeNil

func (e *Encoder) EncodeNil() error

func (*Encoder) EncodeSliceLen

func (e *Encoder) EncodeSliceLen(l int) error

Deprecated. Use EncodeArrayLen instead.

func (*Encoder) EncodeString

func (e *Encoder) EncodeString(v string) error

func (*Encoder) EncodeTime

func (e *Encoder) EncodeTime(tm time.Time) error

func (*Encoder) EncodeUint

func (e *Encoder) EncodeUint(v uint) error

func (*Encoder) EncodeUint16

func (e *Encoder) EncodeUint16(v uint16) error

func (*Encoder) EncodeUint32

func (e *Encoder) EncodeUint32(v uint32) error

func (*Encoder) EncodeUint64

func (e *Encoder) EncodeUint64(v uint64) error

func (*Encoder) EncodeUint8

func (e *Encoder) EncodeUint8(v uint8) error

func (*Encoder) EncodeValue

func (e *Encoder) EncodeValue(v reflect.Value) error

type Marshaler

type Marshaler interface {
	MarshalMsgpack() ([]byte, error)
}

Deprecated. Use CustomEncoder.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalMsgpack([]byte) error
}

Deprecated. Use CustomDecoder.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL