Go to file
Pavel Merzlyakov f8719a06fa initial commit 2022-10-01 21:05:46 +03:00
LICENSE initial commit 2022-10-01 21:05:46 +03:00
README.md initial commit 2022-10-01 21:05:46 +03:00
appengine.go initial commit 2022-10-01 21:05:46 +03:00
codes.go initial commit 2022-10-01 21:05:46 +03:00
custom.go initial commit 2022-10-01 21:05:46 +03:00
decode.go initial commit 2022-10-01 21:05:46 +03:00
encode.go initial commit 2022-10-01 21:05:46 +03:00
example_test.go initial commit 2022-10-01 21:05:46 +03:00
go.mod initial commit 2022-10-01 21:05:46 +03:00
go.sum initial commit 2022-10-01 21:05:46 +03:00
map.go initial commit 2022-10-01 21:05:46 +03:00
msgpack.go initial commit 2022-10-01 21:05:46 +03:00
msgpack_test.go initial commit 2022-10-01 21:05:46 +03:00
slice.go initial commit 2022-10-01 21:05:46 +03:00
time.go initial commit 2022-10-01 21:05:46 +03:00
typeinfo.go initial commit 2022-10-01 21:05:46 +03:00

README.md

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.