msgpack

package module
v2.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2025 License: MIT Imports: 9 Imported by: 101

README

MessagePack for Golang

Go Reference test Go Report Card codecov FOSSA Status

📣 Announcement: time.Time decoding defaults to UTC in v3

Starting with v3.0.0, when decoding MessagePack Timestamp into Go’s time.Time, the default Location will be UTC (previously Local). The instant is unchanged. To keep the old behavior, use SetDecodedTimeAsLocal().

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)
    }
}

📣 Announcement: time.Time decoding defaults to UTC in v3

TL;DR: Starting with v3.0.0, when decoding MessagePack Timestamp into Go’s time.Time, the default Location will be UTC (previously Local). The instant is unchanged—only the display/location changes. This avoids host-dependent differences and aligns with common distributed systems practice.

What is changing?
  • Before (v2.x): Decoded time.Time defaults to Local.
  • After (v3.0.0): Decoded time.Time defaults to UTC.

MessagePack’s Timestamp encodes an instant (epoch seconds + nanoseconds) and does not carry timezone info. Your data’s point in time is the same; only time.Time.Location() differs.

Why?
  • Eliminate environment-dependent behavior (e.g., different hosts showing different local zones).
  • Make “UTC by default” the safe, predictable baseline for logs, APIs, and distributed apps.
Who is affected?
  • Apps that display local time without explicitly converting from UTC.
  • If your code already normalizes to UTC or explicitly sets a location, you’re likely unaffected.
Keep the old behavior (Local)

If you want the v2 behavior on v3:

msgpack.SetDecodedTimeAsLocal()

Or convert after the fact:

var t time.Time
_ = msgpack.Unmarshal(data, &t)
t = t.In(time.Local)
Preview the new behavior on v2 (optional)

You can opt into UTC today on v2.x:

msgpack.SetDecodedTimeAsUTC()

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 SetDecodedTimeAsLocal added in v2.4.0

func SetDecodedTimeAsLocal()

SetDecodedTimeAsLocal sets decoded time.Time values to local timezone.

func SetDecodedTimeAsUTC added in v2.4.0

func SetDecodedTimeAsUTC()

SetDecodedTimeAsUTC sets decoded time.Time values to UTC timezone.

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