msgpack

package
v0.807.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package msgpack decodes a MessagePack-encoded value to a structured tree — the compact binary serialization (https://msgpack.org) used by Redis internals, msgpack-RPC, many web/API backends, mobile sync protocols, and game-server traffic. It is the binary-serialization sibling of cbor_decode: an operator pastes the hex of a captured msgpack blob (from a packet dump, a cache value, a stored token) and gets the decoded structure without writing a throwaway script. Pure offline transform; no network or device.

Wrap-vs-native judgement

Native. MessagePack is a fully public, byte-oriented format (github.com/msgpack/msgpack spec.md) — a one-byte type tag selecting a fixed family (fixint / fixstr / fixarray / fixmap, nil/bool, bin, ext, the big-endian uint/int/float widths, and the str/array/map length-prefixed forms). It is a recursive-descent walk over a byte cursor; there is nothing to wrap, and adding github.com/vmihailenco/msgpack as a runtime dependency to decode untrusted bytes is unwarranted. Consistent with internal/cbordecode and internal/protobufdecode owning their parse in-tree.

Verifiable / no confidently-wrong output

Strongest verification class — every format family is gated byte-for-byte against vectors produced by the reference `msgpack` Python library (nil/bool, fixint / negative fixint, uint8..uint64, int8..int64, float32/float64, fixstr / str8, bin8, fixarray, fixmap, fixext4, and a nested map/array). A truncated or malformed blob is rejected with an error (never a partial/guessed decode), the reserved 0xc1 tag is rejected, and any trailing bytes after the top-level value are surfaced as trailing_bytes_hex rather than ignored.

Covered / deferred

Covered: all MessagePack core types, plus the Timestamp extension (ext type -1) decoded to RFC 3339 across all three wire layouts — 32-bit (uint32 seconds), 64-bit (30-bit nanoseconds + 34-bit seconds) and 96-bit (uint32 nanoseconds + signed int64 seconds, so pre-epoch times decode correctly). A non-standard Timestamp length or an out-of-range nanoseconds field is left raw with a note rather than decoded into a confidently-wrong time. Other extension types are surfaced as raw (extension type + data hex). Invalid-UTF-8 str payloads are surfaced as hex with a note instead of an invalid string.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MapEntry

type MapEntry struct {
	Key   *Value `json:"key"`
	Value *Value `json:"value"`
}

MapEntry is one key/value pair of a decoded map (order-preserving).

type Result

type Result struct {
	Value         *Value `json:"value"`
	TotalBytes    int    `json:"total_bytes"`
	TrailingHex   string `json:"trailing_bytes_hex,omitempty"`
	TrailingCount int    `json:"trailing_bytes,omitempty"`
}

Result is the decoded top-level value plus framing metadata.

func Decode

func Decode(hexBlob string) (*Result, error)

Decode parses the hex of a MessagePack blob (separators and an optional 0x prefix tolerated) into a single top-level value.

func DecodeBytes

func DecodeBytes(b []byte) (*Result, error)

DecodeBytes parses a MessagePack blob from raw bytes.

type Value

type Value struct {
	Type   string `json:"type"`
	Format string `json:"format"`

	Bool  *bool    `json:"bool,omitempty"`
	Int   *int64   `json:"int,omitempty"`
	Uint  *uint64  `json:"uint,omitempty"`
	Float *float64 `json:"float,omitempty"`
	Str   *string  `json:"str,omitempty"`
	// BytesHex carries a bin payload, or a str payload that is not valid UTF-8.
	BytesHex string `json:"bytes_hex,omitempty"`
	Note     string `json:"note,omitempty"`

	Array []*Value    `json:"array,omitempty"`
	Map   []*MapEntry `json:"map,omitempty"`

	// Ext fields (msgpack extension types).
	ExtType *int8  `json:"ext_type,omitempty"`
	ExtData string `json:"ext_data_hex,omitempty"`

	// Timestamp fields, set when an ext type -1 carries a well-formed
	// MessagePack Timestamp (Type becomes "timestamp").
	Timestamp        string  `json:"timestamp,omitempty"` // RFC 3339 (UTC)
	TimestampUnixSec *int64  `json:"timestamp_unix_sec,omitempty"`
	TimestampNanos   *uint32 `json:"timestamp_nanos,omitempty"`
}

Value is one decoded MessagePack value.

Jump to

Keyboard shortcuts

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