vmihailenco/msgpack

repository·v5·Indexed 23 days ago

https://github.com/vmihailenco/msgpack

A high-performance MessagePack encoding and decoding library for Golang. It supports standard primitives, structs, time.Time, and provides advanced features including custom encoding, extensions, struct tag customization, and efficient data extraction via Decoder.Query.

Tokens
5.3K
Snippets
6
Records
68
Agent score
82%

What's inside vmihailenco/msgpack

  1. Quickstart: Marshal and Unmarshal data

    v5

    Use msgpack.Marshal to encode a Go value into MessagePack bytes, and msgpack.Unmarshal to decode MessagePack bytes back into a Go value.

    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
    }
  2. Install msgpack/v5

    v5

    msgpack requires Go modules. First, initialize your Go module, then install the v5 package. Note that you must include v5 in the import path; omitting it is a common mistake.

    go mod init github.com/my/repo
    go get github.com/vmihailenco/msgpack/v5
  3. Automatic App Engine Datastore Type Support

    v5

    The msgpappengine package automatically registers custom encoders and decoders for Google App Engine Datastore types with the msgpack library via its init() function. This allows msgpack.Encoder and msgpack.Decoder to handle these types transparently when they are part of a larger structure.

    Supported types:

    • *ds.Key (via encodeDatastoreKeyValue and decodeDatastoreKeyValue)
    • ds.Cursor (via encodeDatastoreCursorValue and decodeDatastoreCursorValue)
  4. Configure struct tags for msgpack

    v5

    You can control how struct fields are encoded using msgpack tags:

    • Renaming: Use msgpack:"my_field_name" to change the field name.
    • Aliasing: Use msgpack:"alias:another_name" to provide an alias.
    • Omitting empty fields: Use msgpack:",omitempty" to omit individual empty fields, or configure the encoder to omit all empty fields in a struct.
  5. Advanced encoding features

    v5

    msgpack provides several advanced configuration options for encoders and decoders:

    • Custom Encoding/Decoding: Implement CustomEncoder or CustomDecoder interfaces for custom logic.
    • Extensions: Use the RegisterExt mechanism to encode type information.
    • Map Key Sorting: Use Encoder.SetSortMapKeys to ensure deterministic map encoding.
    • Array Encoding: Use Encoder.UseArrayEncodedStructs to encode structs as arrays instead of maps.
    • Custom Struct Tags: Use Encoder.SetCustomStructTag and Decoder.SetCustomStructTag to allow msgpack to act as a drop-in replacement for other tag systems (like json).
    • Queries: Use Decoder.Query for efficient data extraction.
  6. Register custom type extensions with RegisterExt

    v5

    Use RegisterExt to associate a specific MessagePack extension ID (int8) with a type that implements both Marshaler and Unmarshaler interfaces. This allows the library to automatically handle encoding and decoding for that type using its own MarshalMsgpack and UnmarshalMsgpack methods.

    Requirements:

    • extID: A unique int8 identifier for the extension.
    • value: An instance of the type that implements the MarshalerUnmarshaler interface.
  7. Use Decoder for stream decoding

    v5
    For decoding from an io.Reader stream, use NewDecoder. The decoder manages its own buffering. If the provided reader implements io.ByteScanner, the decoder will use it directly to avoid extra buffering.
  8. Encode a map with string keys using EncodeMap

    v5

    Use EncodeMap to encode a map[string]interface{} into MessagePack format. If the map is nil, it encodes a MessagePack nil value. The method iterates through the map, encoding each key as a string and then encoding the corresponding value using the encoder's standard encoding logic.

    func (e *Encoder) EncodeMap(m map[string]interface{}) error
  9. Encode signed integers using the most compact representation

    v5

    Use EncodeInt(n int64) to encode a signed integer using the smallest possible number of bytes (1, 2, 3, 5, or 9 bytes).

    Note: The specific type of the number is lost during encoding, as it will be optimized to the smallest representation.