Convert Figma logo to code with AI

vmihailenco logomsgpack

msgpack.org[Go] MessagePack encoding for Golang

2,500
242
2,500
42

Top Related Projects

13,789

A high-performance 100% compatible drop-in replacement of "encoding/json"

2,979

faster JSON serialization for Go

Fast JSON serializer for golang.

1,853

A Go code generator for MessagePack / msgpack.org[Go]

Quick Overview

vmihailenco/msgpack is a Go implementation of the MessagePack serialization format. It provides efficient and compact data serialization, similar to JSON but with a smaller size and faster processing. This library offers both encoding and decoding capabilities for Go data structures.

Pros

  • High performance and low memory usage
  • Supports custom encoders and decoders for complex types
  • Compatible with the MessagePack specification
  • Actively maintained and well-documented

Cons

  • Limited support for some Go-specific types (e.g., complex numbers)
  • May require additional configuration for certain use cases
  • Learning curve for developers unfamiliar with MessagePack

Code Examples

Encoding a struct to MessagePack:

type Person struct {
    Name string
    Age  int
}

person := Person{Name: "Alice", Age: 30}
b, err := msgpack.Marshal(person)
if err != nil {
    panic(err)
}
fmt.Printf("%x", b)

Decoding MessagePack data to a struct:

var decodedPerson Person
err := msgpack.Unmarshal(b, &decodedPerson)
if err != nil {
    panic(err)
}
fmt.Printf("%+v", decodedPerson)

Using custom encoder/decoder:

type CustomTime time.Time

func (t CustomTime) EncodeMsgpack(enc *msgpack.Encoder) error {
    return enc.EncodeTime(time.Time(t))
}

func (t *CustomTime) DecodeMsgpack(dec *msgpack.Decoder) error {
    tm, err := dec.DecodeTime()
    if err != nil {
        return err
    }
    *t = CustomTime(tm)
    return nil
}

Getting Started

To use vmihailenco/msgpack in your Go project:

  1. Install the package:

    go get github.com/vmihailenco/msgpack/v5
    
  2. Import the package in your Go code:

    import "github.com/vmihailenco/msgpack/v5"
    
  3. Use the Marshal and Unmarshal functions for basic encoding and decoding:

    data := map[string]interface{}{"key": "value", "number": 42}
    encoded, err := msgpack.Marshal(data)
    // Handle error and use encoded data
    
    var decoded map[string]interface{}
    err = msgpack.Unmarshal(encoded, &decoded)
    // Handle error and use decoded data
    

For more advanced usage, refer to the library's documentation and examples on GitHub.

Competitor Comparisons

13,789

A high-performance 100% compatible drop-in replacement of "encoding/json"

Pros of json-iterator/go

  • Faster JSON encoding/decoding compared to the standard library
  • More flexible API with support for custom marshalers and unmarshalers
  • Better performance for large JSON payloads

Cons of json-iterator/go

  • Limited to JSON format, while msgpack supports MessagePack format
  • Slightly more complex API compared to the standard library
  • May require additional configuration for optimal performance

Code Comparison

json-iterator/go:

import jsoniter "github.com/json-iterator/go"

var json = jsoniter.ConfigCompatibleWithStandardLibrary
data, err := json.Marshal(someStruct)

msgpack:

import "github.com/vmihailenco/msgpack/v5"

data, err := msgpack.Marshal(someStruct)

Both libraries offer similar ease of use for basic marshaling and unmarshaling operations. However, json-iterator/go provides more configuration options and performance optimizations, while msgpack focuses on the MessagePack format, which is more compact and efficient for binary data.

json-iterator/go is ideal for projects requiring high-performance JSON processing, while msgpack is better suited for applications that need to work with the MessagePack format or require more compact data serialization.

2,979

faster JSON serialization for Go

Pros of ffjson

  • Generates Go code for faster JSON marshaling and unmarshaling
  • Optimized for performance, especially for large structs
  • Supports custom tags for field naming and omission

Cons of ffjson

  • Limited to JSON serialization only
  • Requires code generation step, which can complicate build processes
  • May increase binary size due to generated code

Code Comparison

ffjson:

//go:generate ffjson $GOFILE
type Person struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

msgpack:

type Person struct {
    Name string `msgpack:"name"`
    Age  int    `msgpack:"age"`
}

Key Differences

  • msgpack supports multiple serialization formats (MessagePack, JSON, etc.), while ffjson is JSON-specific
  • ffjson requires code generation for optimal performance, msgpack works without additional steps
  • msgpack offers a more compact binary format, potentially reducing data size
  • ffjson may provide better performance for large, complex structs in JSON format

Use Cases

  • Choose ffjson for high-performance JSON serialization in Go, especially with large data structures
  • Opt for msgpack when working with multiple serialization formats or prioritizing data compactness

Both libraries offer efficient serialization options, with ffjson excelling in JSON-specific scenarios and msgpack providing more flexibility across formats.

Fast JSON serializer for golang.

Pros of easyjson

  • Generates Go code for faster JSON marshaling/unmarshaling
  • Optimized for performance, especially with large data structures
  • Supports custom MarshalJSON/UnmarshalJSON methods

Cons of easyjson

  • Requires code generation step, which can complicate build process
  • Limited to JSON serialization only
  • May increase binary size due to generated code

Code Comparison

easyjson:

//go:generate easyjson -all $GOFILE
type Person struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

msgpack:

type Person struct {
    Name string `msgpack:"name"`
    Age  int    `msgpack:"age"`
}

Key Differences

  • easyjson focuses on JSON serialization, while msgpack supports MessagePack format
  • easyjson requires code generation, msgpack works with standard Go structs
  • easyjson may offer better performance for JSON, but msgpack provides a more compact binary format
  • msgpack has broader language support, while easyjson is Go-specific
  • easyjson provides more fine-grained control over JSON serialization, while msgpack offers a simpler API

Both libraries aim to improve serialization performance in Go, but they take different approaches and target different use cases. The choice between them depends on specific project requirements, such as serialization format, performance needs, and development workflow preferences.

1,853

A Go code generator for MessagePack / msgpack.org[Go]

Pros of msgp

  • Generates code for faster serialization and deserialization
  • Optimized for performance, especially with large data sets
  • Supports custom types and interfaces

Cons of msgp

  • Requires code generation step, which can complicate build processes
  • Less flexible for dynamic data structures
  • May produce larger binary sizes due to generated code

Code Comparison

msgp:

//go:generate msgp
type Person struct {
    Name string `msg:"name"`
    Age  int    `msg:"age"`
}

msgpack:

type Person struct {
    Name string `msgpack:"name"`
    Age  int    `msgpack:"age"`
}

msgp requires a code generation step, while msgpack uses runtime reflection. msgp's generated code typically results in faster serialization and deserialization, but at the cost of additional build complexity and potentially larger binary sizes.

msgpack offers more flexibility for dynamic data structures and is easier to integrate into existing projects without additional build steps. However, it may be slower for large data sets or high-performance applications.

Both libraries support custom types and interfaces, but msgp's approach often leads to better performance due to its generated code. The choice between the two depends on the specific requirements of your project, balancing factors like performance, flexibility, and ease of integration.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

MessagePack encoding for Golang

Build Status PkgGoDev Documentation Chat

msgpack is brought to you by :star: uptrace/uptrace. Uptrace is an open source APM and blazingly fast distributed tracing tool powered by OpenTelemetry and ClickHouse. Give it a star as well!

Resources

Features

Installation

msgpack supports 2 last Go versions and requires support for Go modules. So make sure to initialize a Go module:

go mod init github.com/my/repo

And then install msgpack/v5 (note v5 in the import; omitting it is a popular mistake):

go get github.com/vmihailenco/msgpack/v5

Quickstart

import "github.com/vmihailenco/msgpack/v5"

func ExampleMarshal() {
    type Item struct {
        Foo string
    }

    b, err := msgpack.Marshal(&Item{Foo: "bar"})
    if err != nil {
        panic(err)
    }

    var item Item
    err = msgpack.Unmarshal(b, &item)
    if err != nil {
        panic(err)
    }
    fmt.Println(item.Foo)
    // Output: bar
}

See also

Contributors

Thanks to all the people who already contributed!