msgpack

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2024 License: MIT Imports: 8 Imported by: 34

README ¶

MessagePack for Golang

Go Reference test Go Report Card codecov FOSSA Status

📣 Notice

If your application serializes only primitive types, array, map and struct, code generation is also recommended. You can get the fastest performance with msgpackgen.

Features

  • Supported types : primitive / array / slice / struct / map / interface{} and time.Time
  • Renaming fields via msgpack:"field_name"
  • Omitting fields via msgpack:"-"
  • Supports extend encoder / decoder
  • 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 ¶

Constants ¶

This section is empty.

Variables ¶

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.

func AddExtStreamCoder ¶ added in v2.2.0

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

AddExtStreamCoder adds stream encoders for extension types.

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