msgpack

package module
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2025 License: MIT Imports: 8 Imported by: 98

README

MessagePack for Golang

Go Reference test Go Report Card codecov FOSSA Status

Features

  • Supported types : primitive / array / slice / struct / map / interface{} and time.Time
  • Renaming fields via msgpack:"field_name"
  • Omitting fields via msgpack:"-"
  • Omitting empty fields via msgpack:"field_name,omitempty"
  • Supports extend encoder / decoder (example)
  • Can also Encoding / Decoding struct as array

Installation

Current version is msgpack/v2.

go get -u github.com/shamaton/msgpack/v2

Quick Start

package main

import (
  "github.com/shamaton/msgpack/v2"
  "net/http"
)

type Struct struct {
	String string
}

// simple
func main() {
	v := Struct{String: "msgpack"}

	d, err := msgpack.Marshal(v)
	if err != nil {
		panic(err)
	}
	r := Struct{}
	if err =  msgpack.Unmarshal(d, &r); err != nil {
		panic(err)
	}
}

// streaming
func handle(w http.ResponseWriter, r *http.Request) {
	var body Struct
	if err := msgpack.UnmarshalRead(r, &body); err != nil {
		panic(err)
    }
	if err := msgpack.MarshalWrite(w, body); err != nil {
		panic(err)
    }
}

Benchmark

This result made from shamaton/msgpack_bench

msgpack_bench

License

This library is under the MIT License.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Error = def.ErrMsgpack

Error is used in all msgpack error as the based error.

View Source
var StructAsArray = false

StructAsArray is encoding option. If this option sets true, default encoding sets to array-format.

Functions

func AddExtCoder

func AddExtCoder(e ext.Encoder, d ext.Decoder) error

AddExtCoder adds encoders for extension types.

Example
err := msgpack.AddExtCoder(&IPNetEncoder{}, &IPNetDecoder{})
if err != nil {
	panic(err)
}

v1 := net.IPNet{IP: net.IP{127, 0, 0, 1}, Mask: net.IPMask{255, 255, 255, 0}}
r1, err := msgpack.Marshal(v1)
if err != nil {
	panic(err)
}
fmt.Printf("encode: % 02x\n", r1)

var v2 net.IPNet
err = msgpack.Unmarshal(r1, &v2)
if err != nil {
	panic(err)
}
fmt.Println("decode:", v2)
Output:

encode: c7 0c 32 31 32 37 2e 30 2e 30 2e 31 2f 32 34
decode: {127.0.0.0 ffffff00}

func AddExtStreamCoder added in v2.2.0

func AddExtStreamCoder(e ext.StreamEncoder, d ext.StreamDecoder) error

AddExtStreamCoder adds stream encoders for extension types.

Example
err := msgpack.AddExtStreamCoder(&IPNetStreamEncoder{}, &IPNetStreamDecoder{})
if err != nil {
	panic(err)
}

v1 := net.IPNet{IP: net.IP{127, 0, 0, 1}, Mask: net.IPMask{255, 255, 255, 0}}

buf := bytes.Buffer{}
err = msgpack.MarshalWrite(&buf, v1)
if err != nil {
	panic(err)
}
fmt.Printf("encode: % 02x\n", buf.Bytes())

var v2 net.IPNet
err = msgpack.UnmarshalRead(&buf, &v2)
if err != nil {
	panic(err)
}
fmt.Println("decode:", v2)
Output:

encode: c7 0c 32 31 32 37 2e 30 2e 30 2e 31 2f 32 34
decode: {127.0.0.0 ffffff00}

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal returns the MessagePack-encoded byte array of v.

func MarshalAsArray

func MarshalAsArray(v interface{}) ([]byte, error)

MarshalAsArray encodes data as array format. This is the same thing that StructAsArray sets true.

func MarshalAsMap

func MarshalAsMap(v interface{}) ([]byte, error)

MarshalAsMap encodes data as map format. This is the same thing that StructAsArray sets false.

func MarshalWrite added in v2.2.0

func MarshalWrite(w io.Writer, v interface{}) error

MarshalWrite writes MessagePack-encoded byte array of v to writer.

func MarshalWriteAsArray added in v2.2.0

func MarshalWriteAsArray(w io.Writer, v interface{}) error

MarshalWriteAsArray writes array format encoded data to writer. This is the same thing that StructAsArray sets true.

func MarshalWriteAsMap added in v2.2.0

func MarshalWriteAsMap(w io.Writer, v interface{}) error

MarshalWriteAsMap writes map format encoded data to writer. This is the same thing that StructAsArray sets false.

func RemoveExtCoder

func RemoveExtCoder(e ext.Encoder, d ext.Decoder) error

RemoveExtCoder removes encoders for extension types.

func RemoveExtStreamCoder added in v2.2.0

func RemoveExtStreamCoder(e ext.StreamEncoder, d ext.StreamDecoder) error

RemoveExtStreamCoder removes stream encoders for extension types.

func SetComplexTypeCode

func SetComplexTypeCode(code int8)

SetComplexTypeCode sets def.complexTypeCode

func Unmarshal

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

Unmarshal analyzes the MessagePack-encoded data and stores the result into the pointer of v.

func UnmarshalAsArray

func UnmarshalAsArray(data []byte, v interface{}) error

UnmarshalAsArray decodes data that is encoded as array format. This is the same thing that StructAsArray sets true.

func UnmarshalAsMap

func UnmarshalAsMap(data []byte, v interface{}) error

UnmarshalAsMap decodes data that is encoded as map format. This is the same thing that StructAsArray sets false.

func UnmarshalRead added in v2.2.0

func UnmarshalRead(r io.Reader, v interface{}) error

UnmarshalRead reads the MessagePack-encoded data from reader and stores the result into the pointer of v.

func UnmarshalReadAsArray added in v2.2.0

func UnmarshalReadAsArray(r io.Reader, v interface{}) error

UnmarshalReadAsArray decodes from stream. stream data expects array format. This is the same thing that StructAsArray sets true.

func UnmarshalReadAsMap added in v2.2.0

func UnmarshalReadAsMap(r io.Reader, v interface{}) error

UnmarshalReadAsMap decodes from stream. stream data expects map format. This is the same thing that StructAsArray sets false.

Types

This section is empty.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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