msgpack

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2022 License: BSD-3-Clause Imports: 10 Imported by: 1

README

Msgpack implementation for Golang

Supports:

  • Primitives, arrays, maps, structs and interface{}.
  • time.Time.
  • Appengine *datastore.Key and datastore.Cursor.
  • Extensions for user defined types.
  • Tags.

API docs: http://godoc.org/github.com/vmihailenco/msgpack

Installation

Install:

go get github.com/vmihailenco/msgpack

Usage

Examples:

func ExampleEncode() {
    b, err := msgpack.Marshal(true)
    fmt.Printf("%v %#v\n", err, b)
    // Output: <nil> []byte{0xc3}
}

func ExampleDecode() {
    var out bool
    err := msgpack.Unmarshal([]byte{0xc3}, &out)
    fmt.Println(err, out)
    // Output: <nil> true
}

func ExampleMapStringInterface() {
    in := map[string]interface{}{"foo": 1, "hello": "world"}
    b, err := msgpack.Marshal(in)
    _ = err

    var out map[string]interface{}
    err = msgpack.Unmarshal(b, &out)
    fmt.Printf("%v %#v\n", err, out)
    // Output: <nil> map[string]interface {}{"foo":1, "hello":"world"}
}

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

Extensions

Look at appengine.go for example.

Documentation

Overview

Example (MapStringInterface)
package main

import (
	"fmt"

	"git.bit5.ru/backend/msgpack"
)

func main() {
	in := map[string]interface{}{"foo": 1, "hello": "world"}
	b, err := msgpack.Marshal(in)
	_ = err

	var out map[string]interface{}
	err = msgpack.Unmarshal(b, &out)
	fmt.Printf("%v %#v\n", err, out)
}
Output:

<nil> map[string]interface {}{"foo":1, "hello":"world"}
Example (RecursiveMapStringInterface)
package main

import (
	"bytes"
	"fmt"

	"git.bit5.ru/backend/msgpack"
)

func main() {
	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
package main

import (
	"fmt"

	"git.bit5.ru/backend/msgpack"
)

func main() {
	b, err := msgpack.Marshal(true)
	fmt.Printf("%v %#v\n", err, b)
}
Output:

<nil> []byte{0xc3}

func Register

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

func Unmarshal

func Unmarshal(data []byte, v ...interface{}) error
Example
package main

import (
	"fmt"

	"git.bit5.ru/backend/msgpack"
)

func main() {
	var out bool
	err := msgpack.Unmarshal([]byte{0xc3}, &out)
	fmt.Println(err, out)
}
Output:

<nil> true

Types

type Coder

type Coder interface {
	// contains filtered or unexported methods
}

type Decoder

type Decoder struct {
	R             bufReader
	DecodeMapFunc func(*Decoder) (interface{}, error)
}

func NewDecoder

func NewDecoder(rd 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) 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

type Encoder

type Encoder struct {
	W writer
}

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(v ...interface{}) 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(value float32) error

func (*Encoder) EncodeFloat64

func (e *Encoder) EncodeFloat64(value 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) EncodeNil

func (e *Encoder) EncodeNil() error

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL