Documentation ¶
Overview ¶
Package msgpack implements MessagePack encoding and decoding.
Index ¶
- Variables
- type DecodeConvertError
- type Decoder
- func (d *Decoder) Bool() bool
- func (d *Decoder) Bytes() []byte
- func (d *Decoder) BytesNoCopy() []byte
- func (d *Decoder) Decode(v interface{}) (err error)
- func (d *Decoder) Extension() int
- func (d *Decoder) Float() float64
- func (d *Decoder) Int() int64
- func (d *Decoder) Len() int
- func (d *Decoder) SetExtensions(extensions ExtensionMap)
- func (d *Decoder) Skip() error
- func (d *Decoder) String() string
- func (d *Decoder) Type() Type
- func (d *Decoder) Uint() uint64
- func (d *Decoder) Unpack() error
- type Encoder
- func (e *Encoder) Encode(v interface{}) (err error)
- func (e *Encoder) PackArrayLen(n int64) error
- func (e *Encoder) PackBinary(v []byte) error
- func (e *Encoder) PackBool(b bool) error
- func (e *Encoder) PackExtension(kind int, data []byte) error
- func (e *Encoder) PackFloat(f float64) error
- func (e *Encoder) PackInt(v int64) error
- func (e *Encoder) PackMapLen(n int64) error
- func (e *Encoder) PackNil() error
- func (e *Encoder) PackRaw(p []byte) error
- func (e *Encoder) PackString(v string) error
- func (e *Encoder) PackStringBytes(v []byte) error
- func (e *Encoder) PackUint(v uint64) error
- type ExtensionMap
- type Marshaler
- type Type
- type Unmarshaler
Constants ¶
This section is empty.
Variables ¶
var ( // ErrLongStringOrBinary is the long string or binary error. ErrLongStringOrBinary = errors.New("msgpack: long string or binary") // ErrIllegalSize is the illegal array or map size error. ErrIllegalSize = errors.New("msgpack: illegal array or map size") )
var ErrDataSizeTooLarge = errors.New("msgpack: data size too large")
ErrDataSizeTooLarge is the data size too large error.
var ErrInvalidDecodeArg = errors.New("msgpack: argument to Decode must be non-nil pointer, slice or map")
ErrInvalidDecodeArg is the invalid argument error.
Functions ¶
This section is empty.
Types ¶
type DecodeConvertError ¶
type DecodeConvertError struct { // The MessagePack type of the value. SrcType Type // Option value. SrcValue interface{} // Type of the Go value that could not be assigned to. DestType reflect.Type }
DecodeConvertError describes a MessagePack value that was not appropriate for a value of a specific Go type.
func (*DecodeConvertError) Error ¶
func (e *DecodeConvertError) Error() string
Error implements the error interface.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder reads MessagePack objects from an io.Reader.
func NewDecoder ¶
NewDecoder allocates and initializes a new decoder.
func (*Decoder) Bytes ¶
Bytes returns the current String, Binary or Extension value as a slice of bytes.
func (*Decoder) BytesNoCopy ¶
BytesNoCopy returns the current String, Binary or Extension value as a slice of bytes. The underlying array may point to data that will be overwritten by a subsequent call to Unpack.
func (*Decoder) Decode ¶
Decode decodes the next value in the stream to v.
Decode uses the inverse of the encodings that Encoder.Encode uses, allocating maps, slices, and pointers as necessary, with the following additional rules:
To decode into a pointer, Decode first handles the case of a MessagePack nil. In that case, Decode sets the pointer to nil. Otherwise, Decode decodes the stream into the value pointed at by the pointer. If the pointer is nil, Decode allocates a new value for it to point to.
To decode a MessagePack array into a slice, Decode sets the slice length to the length of the MessagePack array or reallocates the slice if there is insufficient capaicity. Slice elments are not cleared before decoding the element.
To decode a MessagePack array into a Go array, Decode decodes the MessagePack array elements into corresponding Go array elements. If the Go array is smaller than the MessagePack array, the additional MessagePack array elements are discarded. If the MessagePack array is smaller than the Go array, the additional Go array elements are set to zero values.
If a MessagePack value is not appropriate for a given target type, or if a MessagePack number overflows the target type, Decode skips that field and completes the decoding as best it can. If no more serious errors are encountered, Decode returns an DecodeConvertError describing the earliest such error.
func (*Decoder) SetExtensions ¶
func (d *Decoder) SetExtensions(extensions ExtensionMap)
SetExtensions specifies functions for converting MessagePack extensions to Go values.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder writes values in MessagePack format.
func NewEncoder ¶
NewEncoder allocates and initializes a new Unpacker.
func (*Encoder) Encode ¶
Encode writes the MessagePack encoding of v to the stream.
Encode traverses the value v recursively. If an encountered value implements the Marshaler interface Encode calls its MarshalMsgPack method to write the value to the stream.
Otherwise, Encode uses the following type-dependent default encodings:
Go Type MessagePack Type bool true or false float32, float64 float64 string string []byte binary slices, arrays array struct, map map
Struct values encode as maps or arrays. If any struct field tag specifies the "array" option, then the struct is encoded as an array. Otherwise, the struct is encoded as a map. Each exported struct field becomes a member of the map unless
- the field's tag is "-", or
- the field is empty and its tag specifies the "omitempty" option.
Anonymous struct fields are marshaled as if their inner exported fields were fields in the outer struct.
The struct field tag "empty" specifies a default value when decoding and the empty value for the "omitempty" option.
Pointer values encode as the value pointed to. A nil pointer encodes as the MessagePack nil value.
Interface values encode as the value contained in the interface. A nil interface value encodes as the MessagePack nil value.
func (*Encoder) PackArrayLen ¶
PackArrayLen write an Array length to the MessagePack stream. The application must write n objects to the stream following this call.
func (*Encoder) PackBinary ¶
PackBinary writes a Binary value to the MessagePack stream.
func (*Encoder) PackExtension ¶
PackExtension writes an extension to the MessagePack stream.
func (*Encoder) PackMapLen ¶
PackMapLen write an Map length to the MessagePack stream. The application must write n key-value pairs to the stream following this call.
func (*Encoder) PackRaw ¶
PackRaw writes bytes directly to the MessagePack stream. It is the application's responsibility to ensure that the bytes are valid.
func (*Encoder) PackString ¶
PackString writes a String value to the MessagePack stream.
func (*Encoder) PackStringBytes ¶
PackStringBytes writes a String value to the MessagePack stream.
type ExtensionMap ¶
ExtensionMap specifies functions for converting MessagePack extensions to Go values.
The key is the MessagePack extension type. The value is a function that converts the extension data to a Go value.
type Marshaler ¶
Marshaler is the interface implemented by objects that can encode themselves to a MessagePack stream.
type Type ¶
type Type int
Type represents the type of value in the MessagePack stream.
list of MessagePack types.
type Unmarshaler ¶
Unmarshaler is the interface implemented by objects that can decode themselves from a MessagePack stream.