Documentation ¶
Index ¶
- Constants
- func Marshal(v interface{}) ([]byte, error)
- func MarshalStringKey(v interface{}) ([]byte, error)
- func Unmarshal(b []byte, v interface{}) error
- func UnmarshalStringKey(b []byte, v interface{}) error
- type ArrayHeader
- type ArrayLengthTolerance
- type Decoder
- func (d *Decoder) Decode(v interface{}) (e error)
- func (d *Decoder) DecodeArrayHeader(format Format) (ArrayHeader, error)
- func (d *Decoder) DecodeExtHeader(format Format) (ExtHeader, error)
- func (d *Decoder) DecodeFloat32(format Format) (float32, error)
- func (d *Decoder) DecodeFloat64(format Format) (float64, error)
- func (d *Decoder) DecodeFormat() (Format, error)
- func (d *Decoder) DecodeInt64(format Format) (int64, error)
- func (d *Decoder) DecodeMapHeader(format Format) (MapHeader, error)
- func (d *Decoder) DecodeString(format Format) (string, error)
- func (d *Decoder) DecodeTime(header ExtHeader) (time.Time, error)
- func (d *Decoder) DecodeUint64(format Format) (uint64, error)
- func (d *Decoder) Reset(r io.Reader)
- func (d *Decoder) ResetBytes(in []byte)
- func (d *Decoder) SetArrayLengthTolerance(tolerance ArrayLengthTolerance) *Decoder
- func (d *Decoder) SetStructKeyType(t StructKeyType) *Decoder
- func (d *Decoder) SetStructTagName(tagName string) *Decoder
- func (d *Decoder) SetTimeZone(loc *time.Location) *Decoder
- func (d *Decoder) SkipObject(format Format) error
- type Encoder
- func (e *Encoder) Encode(v interface{}) error
- func (e *Encoder) EncodeArrayHeader(length uint32) error
- func (e *Encoder) EncodeBin(v []byte) error
- func (e *Encoder) EncodeBool(v bool) error
- func (e *Encoder) EncodeFloat32(v float32) error
- func (e *Encoder) EncodeFloat64(v float64) error
- func (e *Encoder) EncodeInt64(v int64) error
- func (e *Encoder) EncodeMapHeader(length uint32) error
- func (e *Encoder) EncodeNil() error
- func (e *Encoder) EncodeString(v string) error
- func (e *Encoder) EncodeTime(v time.Time) error
- func (e *Encoder) EncodeUint64(v uint64) error
- func (e *Encoder) Reset(w io.Writer)
- func (e *Encoder) SetStructKeyType(t StructKeyType) *Encoder
- func (e *Encoder) SetStructTagName(tagName string) *Encoder
- type ExtHeader
- type Format
- type FormatName
- type MapHeader
- type Marshaler
- type StructKeyType
- type Unmarshaler
Examples ¶
Constants ¶
const TimestampTypeCode = 0xFF
TimestampTypeCode is ext type code of Timestamp.
Variables ¶
This section is empty.
Functions ¶
func MarshalStringKey ¶
MarshalStringKey is encode any data to message pack.
func UnmarshalStringKey ¶
UnmarshalStringKey is decode message pack data to any data.
Types ¶
type ArrayHeader ¶
ArrayHeader is header of array data.
type ArrayLengthTolerance ¶
type ArrayLengthTolerance int
ArrayLengthTolerance is tolerance of array length when decode time.
const ( // ArrayLengthToleranceLessThanOrEqual is allowed if the array length of message pack is the same or less than or equal to the length of go. ArrayLengthToleranceLessThanOrEqual ArrayLengthTolerance = iota // ArrayLengthToleranceEqualOnly is allowed if the array length of message pack is the only equal to the length of go. ArrayLengthToleranceEqualOnly // ArrayLengthToleranceRounding is allowed always. // If the array length of is the same or greater than to the length of go, overloaded data is discarded. ArrayLengthToleranceRounding )
func (ArrayLengthTolerance) String ¶
func (i ArrayLengthTolerance) String() string
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder is message pack decoder.
func NewDecoderBytes ¶
NewDecoderBytes is create decoder instance.
func (*Decoder) Decode ¶
Decode is decode data from message pack.
Example (Streaming) ¶
package main import ( "fmt" "log" "go.nanasi880.dev/x/encoding/msgpack" ) func main() { examples := make([]int, 0) for i := 0; i < 10; i++ { examples = append(examples, i) } data, err := msgpack.Marshal(examples) if err != nil { log.Fatal(err) } ch := make(chan int) go func() { defer close(ch) dec := msgpack.NewDecoderBytes(data) format, err := dec.DecodeFormat() if err != nil { log.Fatal(err) } header, err := dec.DecodeArrayHeader(format) if err != nil { log.Fatal(err) } for i := uint32(0); i < header.Length; i++ { var value int err = dec.Decode(&value) if err != nil { log.Fatal(err) } ch <- value } }() for value := range ch { fmt.Println(value) } }
Output: 0 1 2 3 4 5 6 7 8 9
func (*Decoder) DecodeArrayHeader ¶
func (d *Decoder) DecodeArrayHeader(format Format) (ArrayHeader, error)
DecodeArrayHeader is decode array header from message pack.
func (*Decoder) DecodeExtHeader ¶
DecodeExtHeader is decode ext header from message pack.
func (*Decoder) DecodeFloat32 ¶
DecodeFloat32 is decode float type as float32 from message pack.
func (*Decoder) DecodeFloat64 ¶
DecodeFloat64 is decode float type as float64 from message pack.
func (*Decoder) DecodeFormat ¶
DecodeFormat is decode format from message pack.
func (*Decoder) DecodeInt64 ¶
DecodeInt64 is decode integer type as int64 from message pack.
func (*Decoder) DecodeMapHeader ¶
DecodeMapHeader is decode map header from message pack.
func (*Decoder) DecodeString ¶
DecodeString is decode string type or bin type as string from message pack.
func (*Decoder) DecodeTime ¶
DecodeTime is decode time from message pack.
func (*Decoder) DecodeUint64 ¶
DecodeUint64 is decode integer type as uint64 from message pack.
func (*Decoder) ResetBytes ¶
ResetBytes is reset decoder. However, the work buffer will not be reset.
func (*Decoder) SetArrayLengthTolerance ¶
func (d *Decoder) SetArrayLengthTolerance(tolerance ArrayLengthTolerance) *Decoder
SetArrayLengthTolerance is set ArrayLengthTolerance to Decoder.
func (*Decoder) SetStructKeyType ¶
func (d *Decoder) SetStructKeyType(t StructKeyType) *Decoder
SetStructKeyType is set StructKeyType to Decoder.
func (*Decoder) SetStructTagName ¶
SetStructTagName is set struct tag name to Decoder. If tagName is empty, use `msgpack` tag.
func (*Decoder) SetTimeZone ¶
SetTimeZone is set time zone to Decoder. The decoder will set this time zone to the time when decoding. If loc is nil, use the UTC time.
func (*Decoder) SkipObject ¶
SkipObject is seek current message pack object.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder is message pack encoder.
func (*Encoder) EncodeArrayHeader ¶
EncodeArrayHeader is encode array header as message pack.
func (*Encoder) EncodeBool ¶
EncodeBool is encode bool as message pack.
func (*Encoder) EncodeFloat32 ¶
EncodeFloat32 is encode float32 as message pack.
func (*Encoder) EncodeFloat64 ¶
EncodeFloat64 is encode float64 as message pack.
func (*Encoder) EncodeInt64 ¶
EncodeInt64 is encode int64 as message pack.
func (*Encoder) EncodeMapHeader ¶
EncodeMapHeader is encode map header as message pack.
func (*Encoder) EncodeString ¶
EncodeString is encode string as message pack.
func (*Encoder) EncodeTime ¶
EncodeTime is encode time.Time as message pack.
func (*Encoder) EncodeUint64 ¶
EncodeUint64 is encode uint64 as message pack.
func (*Encoder) SetStructKeyType ¶
func (e *Encoder) SetStructKeyType(t StructKeyType) *Encoder
SetStructKeyType is set StructKeyType to Encoder.
func (*Encoder) SetStructTagName ¶
SetStructTagName is set struct tag name to Encoder. If tagName is empty, use `msgpack` tag.
type FormatName ¶
type FormatName byte
const ( PositiveFixInt FormatName = 0 FixMap FormatName = 0x80 FixArray FormatName = 0x90 FixStr FormatName = 0xa0 Nil FormatName = 0xc0 Unused FormatName = 0xc1 False FormatName = 0xc2 True FormatName = 0xc3 Bin8 FormatName = 0xc4 Bin16 FormatName = 0xc5 Bin32 FormatName = 0xc6 Ext8 FormatName = 0xc7 Ext16 FormatName = 0xc8 Ext32 FormatName = 0xc9 Float32 FormatName = 0xca Float64 FormatName = 0xcb Uint8 FormatName = 0xcc Uint16 FormatName = 0xcd Uint32 FormatName = 0xce Uint64 FormatName = 0xcf Int8 FormatName = 0xd0 Int16 FormatName = 0xd1 Int32 FormatName = 0xd2 Int64 FormatName = 0xd3 FixExt1 FormatName = 0xd4 FixExt2 FormatName = 0xd5 FixExt4 FormatName = 0xd6 FixExt8 FormatName = 0xd7 FixExt16 FormatName = 0xd8 Str8 FormatName = 0xd9 Str16 FormatName = 0xda Str32 FormatName = 0xdb Array16 FormatName = 0xdc Array32 FormatName = 0xdd Map16 FormatName = 0xde Map32 FormatName = 0xdf NegativeFixInt FormatName = 0xe0 )
func (FormatName) Byte ¶
func (i FormatName) Byte() byte
func (FormatName) String ¶
func (i FormatName) String() string
type Marshaler ¶
Marshaler is the interface implemented by types that can marshal themselves into valid message pack.
type StructKeyType ¶
type StructKeyType byte
StructKeyType is determines how to serialize/deserialize the structure when it is encode/decode.
const ( StructKeyTypeInt StructKeyType = iota // StructKeyTypeInt is struct serialize/deserialize as int key array format. StructKeyTypeString // StructKeyTypeString is struct serialize/deserialize as string key map format. )
func (StructKeyType) String ¶
func (i StructKeyType) String() string
type Unmarshaler ¶
Unmarshaler is the interface implemented by types that can unmarshal themselves into valid message pack.