Documentation ¶
Index ¶
- Variables
- func BytesToString(b []byte) string
- func IsBin(c byte) bool
- func IsExt(c byte) bool
- func IsFixedArray(c byte) bool
- func IsFixedExt(c byte) bool
- func IsFixedMap(c byte) bool
- func IsFixedNum(c byte) bool
- func IsFixedString(c byte) bool
- func IsString(c byte) bool
- func Marshal(v interface{}) ([]byte, error)
- func PutDecoder(dec *Decoder)
- func PutEncoder(enc *Encoder)
- func Register(value interface{}, enc encoderFunc, dec decoderFunc)
- func RegisterExt(extID int8, value MarshalerUnmarshaler)
- func RegisterExtDecoder(extID int8, value interface{}, ...)
- func RegisterExtEncoder(extID int8, value interface{}, ...)
- func StringToBytes(s string) []byte
- func StructToAnyStruct(obj any) (any, error)
- func Unmarshal(data []byte, v interface{}) error
- func UnregisterExt(extID int8)
- func Version() string
- type CustomDecoder
- type CustomEncoder
- type Decoder
- func (d *Decoder) Buffered() io.Reader
- func (d *Decoder) Decode(v interface{}) error
- func (d *Decoder) DecodeArrayLen() (int, error)
- func (d *Decoder) DecodeBool() (bool, error)
- func (d *Decoder) DecodeBytes() ([]byte, error)
- func (d *Decoder) DecodeBytesLen() (int, error)
- func (d *Decoder) DecodeDuration() (time.Duration, error)
- func (d *Decoder) DecodeExtHeader() (extID int8, extLen int, err error)
- func (d *Decoder) DecodeFloat32() (float32, error)
- func (d *Decoder) DecodeFloat64() (float64, error)
- func (d *Decoder) DecodeInt() (int, error)
- func (d *Decoder) DecodeInt16() (int16, error)
- func (d *Decoder) DecodeInt32() (int32, error)
- func (d *Decoder) DecodeInt64() (int64, error)
- func (d *Decoder) DecodeInt8() (int8, error)
- func (d *Decoder) DecodeInterface() (interface{}, error)
- func (d *Decoder) DecodeInterfaceLoose() (interface{}, error)
- func (d *Decoder) DecodeMap() (map[any]any, error)
- func (d *Decoder) DecodeMapLen() (int, error)
- func (d *Decoder) DecodeMulti(v ...interface{}) error
- func (d *Decoder) DecodeNil() error
- func (d *Decoder) DecodeRaw() (RawMessage, error)
- func (d *Decoder) DecodeSlice() ([]interface{}, error)
- func (d *Decoder) DecodeString() (string, error)
- func (d *Decoder) DecodeStringKeyMap() (map[string]any, error)
- func (d *Decoder) DecodeTime() (time.Time, error)
- func (d *Decoder) DecodeTypedMap() (interface{}, error)
- func (d *Decoder) DecodeUint() (uint, error)
- func (d *Decoder) DecodeUint16() (uint16, error)
- func (d *Decoder) DecodeUint32() (uint32, error)
- func (d *Decoder) DecodeUint64() (uint64, error)
- func (d *Decoder) DecodeUint8() (uint8, error)
- func (d *Decoder) DecodeUntypedMap() (map[interface{}]interface{}, error)
- func (d *Decoder) DecodeValue(v reflect.Value) error
- func (d *Decoder) DisableAllocLimit(on bool)
- func (d *Decoder) DisallowUnknownFields(on bool)
- func (d *Decoder) PeekCode() (byte, error)
- func (d *Decoder) Query(query string) ([]interface{}, error)
- func (d *Decoder) ReadFull(buf []byte) error
- func (d *Decoder) Reset(r io.Reader)
- func (d *Decoder) ResetDict(r io.Reader, dict []string)
- func (d *Decoder) ResetReader(r io.Reader)
- func (d *Decoder) SetCustomStructTag(tag string)
- func (d *Decoder) SetMapDecoder(fn func(*Decoder) (interface{}, error))
- func (d *Decoder) Skip() error
- func (d *Decoder) UseInternedStrings(on bool)
- func (d *Decoder) UseLooseInterfaceDecoding(on bool)
- func (d *Decoder) UsePreallocateValues(on bool)
- func (d *Decoder) WithDict(dict []string, fn func(*Decoder) error) error
- type Encoder
- func (e *Encoder) Encode(v interface{}) error
- func (e *Encoder) EncodeArrayLen(l int) error
- func (e *Encoder) EncodeBool(value bool) error
- func (e *Encoder) EncodeBytes(v []byte) error
- func (e *Encoder) EncodeBytesLen(l int) error
- func (e *Encoder) EncodeDuration(d time.Duration) error
- func (e *Encoder) EncodeExtHeader(extID int8, extLen int) error
- func (e *Encoder) EncodeFloat32(n float32) error
- func (e *Encoder) EncodeFloat64(n float64) error
- func (e *Encoder) EncodeInt(n int64) error
- func (e *Encoder) EncodeInt16(n int16) error
- func (e *Encoder) EncodeInt32(n int32) error
- func (e *Encoder) EncodeInt64(n int64) error
- func (e *Encoder) EncodeInt8(n int8) error
- func (e *Encoder) EncodeMap(m map[string]interface{}) error
- func (e *Encoder) EncodeMapLen(l int) error
- func (e *Encoder) EncodeMapSorted(m map[string]interface{}) error
- func (e *Encoder) EncodeMulti(v ...interface{}) error
- func (e *Encoder) EncodeNil() error
- func (e *Encoder) EncodeString(v string) error
- func (e *Encoder) EncodeTime(tm time.Time) error
- func (e *Encoder) EncodeUint(n uint64) error
- func (e *Encoder) EncodeUint16(n uint16) error
- func (e *Encoder) EncodeUint32(n uint32) error
- func (e *Encoder) EncodeUint64(n uint64) error
- func (e *Encoder) EncodeUint8(n uint8) error
- func (e *Encoder) EncodeValue(v reflect.Value) error
- func (e *Encoder) Reset(w io.Writer)
- func (e *Encoder) ResetDict(w io.Writer, dict map[string]int)
- func (e *Encoder) ResetWriter(w io.Writer)
- func (e *Encoder) SetCustomStructTag(tag string)
- func (e *Encoder) SetOmitEmpty(on bool)
- func (e *Encoder) SetSortMapKeys(on bool) *Encoder
- func (e *Encoder) UseArrayEncodedStructs(on bool)
- func (e *Encoder) UseCompactFloats(on bool)
- func (e *Encoder) UseCompactInts(on bool)
- func (e *Encoder) UseInternedStrings(on bool)
- func (e *Encoder) WithDict(dict map[string]int, fn func(*Encoder) error) error
- func (e *Encoder) Writer() io.Writer
- type Marshaler
- type MarshalerUnmarshaler
- type Parser
- type RawMessage
- type Tag
- type Unmarshaler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( PosFixedNumHigh byte = 0x7f NegFixedNumLow byte = 0xe0 Nil byte = 0xc0 False byte = 0xc2 True byte = 0xc3 Float byte = 0xca Double byte = 0xcb Uint8 byte = 0xcc Uint16 byte = 0xcd Uint32 byte = 0xce Uint64 byte = 0xcf Int8 byte = 0xd0 Int16 byte = 0xd1 Int32 byte = 0xd2 Int64 byte = 0xd3 FixedStrLow byte = 0xa0 FixedStrHigh byte = 0xbf FixedStrMask byte = 0x1f Str8 byte = 0xd9 Str16 byte = 0xda Str32 byte = 0xdb Bin8 byte = 0xc4 Bin16 byte = 0xc5 Bin32 byte = 0xc6 FixedArrayLow byte = 0x90 FixedArrayHigh byte = 0x9f FixedArrayMask byte = 0xf Array16 byte = 0xdc Array32 byte = 0xdd FixedMapLow byte = 0x80 FixedMapHigh byte = 0x8f FixedMapMask byte = 0xf Map16 byte = 0xde Map32 byte = 0xdf FixExt1 byte = 0xd4 FixExt2 byte = 0xd5 FixExt4 byte = 0xd6 FixExt8 byte = 0xd7 FixExt16 byte = 0xd8 Ext8 byte = 0xc7 Ext16 byte = 0xc8 Ext32 byte = 0xc9 )
Functions ¶
func BytesToString ¶
func IsFixedArray ¶
func IsFixedExt ¶
func IsFixedMap ¶
func IsFixedNum ¶
func IsFixedString ¶
func Marshal ¶
Marshal returns the MessagePack encoding of v.
Example ¶
node := &DataNode{ Base: Base{ StrID: "Node1", ID: 1, }, V1: "V1", V2: 2, V3: map[int]int{ 3: 4, }, V4: true, } bytes, err := Marshal(node) if err != nil { panic(err) } var showObj any err = Unmarshal(bytes, &showObj) if err != nil { panic(err) } fmt.Println(showObj)
Output: [1 <nil> Node1 V1 2 map[3:4] true]
func PutDecoder ¶
func PutDecoder(dec *Decoder)
func PutEncoder ¶
func PutEncoder(enc *Encoder)
func Register ¶
func Register(value interface{}, enc encoderFunc, dec decoderFunc)
Register registers encoder and decoder functions for a value. This is low level API and in most cases you should prefer implementing CustomEncoder/CustomDecoder or Marshaler/Unmarshaler interfaces.
func RegisterExt ¶
func RegisterExt(extID int8, value MarshalerUnmarshaler)
func RegisterExtDecoder ¶
func RegisterExtEncoder ¶
func StringToBytes ¶
func StructToAnyStruct ¶
func Unmarshal ¶
Unmarshal decodes the MessagePack-encoded data and stores the result in the value pointed to by v.
Example ¶
node := &DataNode{ Base: Base{ ID: 1, StrID: "Node1", }, V1: "V1", V2: 2, V3: map[int]int{ 3: 4, }, V4: true, } bytes, err := Marshal(node) if err != nil { panic(err) } simpleNode := &SimpleDataNode{} err = Unmarshal(bytes, simpleNode) if err != nil { panic(err) } fmt.Println(simpleNode.ID, simpleNode.StrID, simpleNode.Value2, simpleNode.Value3) bytes, err = Marshal(simpleNode) if err != nil { panic(err) } node = &DataNode{} err = Unmarshal(bytes, node) fmt.Println(node.ID, node.StrID, node.V1, node.V2, node.V3, node.V4) var showObj any err = Unmarshal(bytes, &showObj) if err != nil { panic(err) } fmt.Println(showObj)
Output: 1 Node1 2 map[3:4] 1 Node1 2 map[3:4] false [1 <nil> Node1 <nil> 2 map[3:4]]
func UnregisterExt ¶
func UnregisterExt(extID int8)
Types ¶
type CustomDecoder ¶
type CustomEncoder ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder reads and decodes MessagePack values from an input stream.
func GetDecoder ¶
func GetDecoder() *Decoder
func NewDecoder ¶
NewDecoder returns a new decoder that reads from r.
The decoder introduces its own buffering and may read data from r beyond the requested msgpack values. Buffering can be disabled by passing a reader that implements io.ByteScanner interface.
func (*Decoder) Buffered ¶
Buffered returns a reader of the data remaining in the Decoder's buffer. The reader is valid until the next call to Decode.
func (*Decoder) DecodeArrayLen ¶
DecodeArrayLen decodes array length. Length is -1 when array is nil.
func (*Decoder) DecodeBool ¶
func (*Decoder) DecodeBytes ¶
func (*Decoder) DecodeBytesLen ¶
func (*Decoder) DecodeExtHeader ¶
func (*Decoder) DecodeFloat32 ¶
func (*Decoder) DecodeFloat64 ¶
DecodeFloat64 decodes msgpack float32/64 into Go float64.
func (*Decoder) DecodeInt16 ¶
func (*Decoder) DecodeInt32 ¶
func (*Decoder) DecodeInt64 ¶
DecodeInt64 decodes msgpack int8/16/32/64 and uint8/16/32/64 into Go int64.
func (*Decoder) DecodeInt8 ¶
func (*Decoder) DecodeInterface ¶
DecodeInterface decodes value into interface. It returns following types:
- nil,
- bool,
- int8, int16, int32, int64,
- uint8, uint16, uint32, uint64,
- float32 and float64,
- string,
- []byte,
- slices of any of the above,
- maps of any of the above.
DecodeInterface should be used only when you don't know the type of value you are decoding. For example, if you are decoding number it is better to use DecodeInt64 for negative numbers and DecodeUint64 for positive numbers.
func (*Decoder) DecodeInterfaceLoose ¶
DecodeInterfaceLoose is like DecodeInterface except that:
- int8, int16, and int32 are converted to int64,
- uint8, uint16, and uint32 are converted to uint64,
- float32 is converted to float64.
- []byte is converted to string.
func (*Decoder) DecodeMapLen ¶
DecodeMapLen decodes map length. Length is -1 when map is nil.
func (*Decoder) DecodeMulti ¶
func (*Decoder) DecodeRaw ¶
func (d *Decoder) DecodeRaw() (RawMessage, error)
func (*Decoder) DecodeSlice ¶
func (*Decoder) DecodeString ¶
func (*Decoder) DecodeTypedMap ¶
DecodeTypedMap decodes a typed map. Typed map is a map that has a fixed type for keys and values. Key and value types may be different.
func (*Decoder) DecodeUint ¶
func (*Decoder) DecodeUint16 ¶
func (*Decoder) DecodeUint32 ¶
func (*Decoder) DecodeUint64 ¶
DecodeUint64 decodes msgpack int8/16/32/64 and uint8/16/32/64 into Go uint64.
func (*Decoder) DecodeUint8 ¶
func (*Decoder) DecodeUntypedMap ¶
func (*Decoder) DisableAllocLimit ¶
DisableAllocLimit enables fully allocating slices/maps when the size is known
func (*Decoder) DisallowUnknownFields ¶
DisallowUnknownFields causes the Decoder to return an error when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.
func (*Decoder) PeekCode ¶
PeekCode returns the next MessagePack code without advancing the reader. Subpackage msgpack/codes defines the list of available msgpcode.
func (*Decoder) Query ¶
Query extracts data specified by the query from the msgpack stream skipping any other data. Query consists of map keys and array indexes separated with dot, e.g. key1.0.key2.
func (*Decoder) Reset ¶
Reset discards any buffered data, resets all state, and switches the buffered reader to read from r.
func (*Decoder) ResetReader ¶
func (*Decoder) SetCustomStructTag ¶
SetCustomStructTag causes the decoder to use the supplied tag as a fallback option if there is no msgpack tag.
func (*Decoder) SetMapDecoder ¶
func (*Decoder) UseInternedStrings ¶
UseInternedStrings enables support for decoding interned strings.
func (*Decoder) UseLooseInterfaceDecoding ¶
UseLooseInterfaceDecoding causes decoder to use DecodeInterfaceLoose to decode msgpack value into Go interface{}.
func (*Decoder) UsePreallocateValues ¶
UsePreallocateValues enables preallocating values in chunks
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
func GetEncoder ¶
func GetEncoder() *Encoder
func NewEncoder ¶
NewEncoder returns a new encoder that writes to w.
func (*Encoder) EncodeArrayLen ¶
func (*Encoder) EncodeBool ¶
func (*Encoder) EncodeBytes ¶
func (*Encoder) EncodeBytesLen ¶
func (*Encoder) EncodeFloat32 ¶
func (*Encoder) EncodeFloat64 ¶
func (*Encoder) EncodeInt ¶
EncodeNumber encodes an int64 in 1, 2, 3, 5, or 9 bytes. Type of the number is lost during encoding.
func (*Encoder) EncodeInt16 ¶
EncodeInt16 encodes an int16 in 3 bytes preserving type of the number.
func (*Encoder) EncodeInt32 ¶
EncodeInt32 encodes an int32 in 5 bytes preserving type of the number.
func (*Encoder) EncodeInt64 ¶
EncodeInt64 encodes an int64 in 9 bytes preserving type of the number.
func (*Encoder) EncodeInt8 ¶
EncodeInt8 encodes an int8 in 2 bytes preserving type of the number.
func (*Encoder) EncodeMapLen ¶
func (*Encoder) EncodeMapSorted ¶
func (*Encoder) EncodeMulti ¶
func (*Encoder) EncodeString ¶
func (*Encoder) EncodeUint ¶
EncodeUnsignedNumber encodes an uint64 in 1, 2, 3, 5, or 9 bytes. Type of the number is lost during encoding.
func (*Encoder) EncodeUint16 ¶
EncodeUint16 encodes an uint16 in 3 bytes preserving type of the number.
func (*Encoder) EncodeUint32 ¶
EncodeUint32 encodes an uint16 in 5 bytes preserving type of the number.
func (*Encoder) EncodeUint64 ¶
EncodeUint64 encodes an uint16 in 9 bytes preserving type of the number.
func (*Encoder) EncodeUint8 ¶
EncodeUint8 encodes an uint8 in 2 bytes preserving type of the number.
func (*Encoder) Reset ¶
Reset discards any buffered data, resets all state, and switches the writer to write to w.
func (*Encoder) ResetWriter ¶
func (*Encoder) SetCustomStructTag ¶
SetCustomStructTag causes the Encoder to use a custom struct tag as fallback option if there is no msgpack tag.
func (*Encoder) SetOmitEmpty ¶
SetOmitEmpty causes the Encoder to omit empty values by default.
func (*Encoder) SetSortMapKeys ¶
SetSortMapKeys causes the Encoder to encode map keys in increasing order. Supported map types are:
- map[string]string
- map[string]bool
- map[string]interface{}
func (*Encoder) UseArrayEncodedStructs ¶
UseArrayEncodedStructs causes the Encoder to encode Go structs as msgpack arrays.
func (*Encoder) UseCompactFloats ¶
UseCompactFloats causes the Encoder to chose a compact integer encoding for floats that can be represented as integers.
func (*Encoder) UseCompactInts ¶
UseCompactEncoding causes the Encoder to chose the most compact encoding. For example, it allows to encode small Go int64 as msgpack int8 saving 7 bytes.
func (*Encoder) UseInternedStrings ¶
UseInternedStrings causes the Encoder to intern strings.
type MarshalerUnmarshaler ¶
type MarshalerUnmarshaler interface { Marshaler Unmarshaler }
type RawMessage ¶
type RawMessage []byte
func (*RawMessage) DecodeMsgpack ¶
func (m *RawMessage) DecodeMsgpack(dec *Decoder) error
func (RawMessage) EncodeMsgpack ¶
func (m RawMessage) EncodeMsgpack(enc *Encoder) error