Documentation ¶
Index ¶
- func Marshal(v interface{}) ([]byte, error)
- func PutDecoder(dec *Decoder)
- func PutEncoder(enc *Encoder)
- func Register(value interface{}, enc encoderFunc, dec decoderFunc)
- func Unmarshal(data []byte, v interface{}) error
- 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) DecodeDouble() (float64, error)
- func (d *Decoder) DecodeDuration() (time.Duration, error)
- func (d *Decoder) DecodeFFIComplex() (complex128, error)
- func (d *Decoder) DecodeFFIInt64() (int64, error)
- func (d *Decoder) DecodeFFIUint64() (uint64, error)
- func (d *Decoder) DecodeInt() (int, error)
- func (d *Decoder) DecodeInt32() (int32, error)
- func (d *Decoder) DecodeInterface() (interface{}, error)
- func (d *Decoder) DecodeInterfaceLoose() (interface{}, error)
- func (d *Decoder) DecodeMap() (map[string]interface{}, error)
- func (d *Decoder) DecodeMapLen() (int, error)
- func (d *Decoder) DecodeMulti(v ...interface{}) error
- func (d *Decoder) DecodeNull() error
- func (d *Decoder) DecodeRaw() (RawMessage, error)
- func (d *Decoder) DecodeSlice() ([]interface{}, error)
- func (d *Decoder) DecodeString() (string, error)
- func (d *Decoder) DecodeTypedMap() (interface{}, error)
- func (d *Decoder) DecodeUint() (uint, error)
- func (d *Decoder) DecodeUint32() (uint32, error)
- func (d *Decoder) DecodeUntypedMap() (map[interface{}]interface{}, error)
- func (d *Decoder) DecodeValue(v reflect.Value) error
- 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) 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) WithDict(dict []string, fn func(*Decoder) error) error
- type Encoder
- func (e *Encoder) Encode(vv ...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) EncodeDouble(n float64) error
- func (e *Encoder) EncodeDuration(d time.Duration) error
- func (e *Encoder) EncodeEmpty() error
- func (e *Encoder) EncodeFFIInt64(n int64) error
- func (e *Encoder) EncodeFFIUint(n uint64) error
- func (e *Encoder) EncodeFFIUint64(n uint64) error
- func (e *Encoder) EncodeInt(n int64) error
- func (e *Encoder) EncodeInt32(n int32) 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) EncodeNoIntern(v interface{}) error
- func (e *Encoder) EncodeNull() error
- func (e *Encoder) EncodeString(v string) error
- func (e *Encoder) EncodeValue(v reflect.Value) error
- func (e *Encoder) EncodeValueNoIntern(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) 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 RawMessage
- type Unmarshaler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal returns the MessagePack encoding of v.
Example ¶
package main import ( "fmt" "github.com/fffonion/ljpack" ) func main() { type Item struct { Foo string } b, err := ljpack.Marshal(&Item{Foo: "bar"}) if err != nil { panic(err) } var item Item err = ljpack.Unmarshal(b, &item) if err != nil { panic(err) } fmt.Println(item.Foo) }
Output: bar
Example (AsArray) ¶
package main import ( "bytes" "fmt" "github.com/fffonion/ljpack" ) func main() { type Item struct { _ljpack struct{} `ljpack:",as_array"` Foo string Bar string } var buf bytes.Buffer enc := ljpack.NewEncoder(&buf) err := enc.Encode(&Item{Foo: "foo", Bar: "bar"}) if err != nil { panic(err) } dec := ljpack.NewDecoder(&buf) v, err := dec.DecodeInterface() if err != nil { panic(err) } fmt.Println(v) }
Output: [foo bar]
Example (EscapedNames) ¶
package main import ( "fmt" "github.com/fffonion/ljpack" ) func main() { og := map[string]interface{}{ "something:special": uint(123), "hello, world": "hello!", } raw, err := ljpack.Marshal(og) if err != nil { panic(err) } type Item struct { SomethingSpecial uint `ljpack:"'something:special'"` HelloWorld string `ljpack:"'hello, world'"` } var item Item if err := ljpack.Unmarshal(raw, &item); err != nil { panic(err) } fmt.Printf("%#v\n", item) }
Output: ljpack_test.Item{SomethingSpecial:0x7b, HelloWorld:"hello!"}
Example (Ignore_simple_zero_structs_when_tagged_with_omitempty) ¶
package main import ( "fmt" "github.com/fffonion/ljpack" ) type NullInt struct { Valid bool Int int } func (i *NullInt) Set(j int) { i.Int = j i.Valid = true } func (i NullInt) IsZero() bool { return !i.Valid } func (i NullInt) MarshalMsgpack() ([]byte, error) { return ljpack.Marshal(i.Int) } func (i *NullInt) UnmarshalMsgpack(b []byte) error { if err := ljpack.Unmarshal(b, &i.Int); err != nil { return err } i.Valid = true return nil } type Secretive struct { Visible bool hidden bool } type T struct { I NullInt `ljpack:",omitempty"` J NullInt S Secretive `ljpack:",omitempty"` } func main() { var t1 T raw, err := ljpack.Marshal(t1) if err != nil { panic(err) } var t2 T if err = ljpack.Unmarshal(raw, &t2); err != nil { panic(err) } fmt.Printf("%#v\n", t2) t2.I.Set(42) t2.S.hidden = true // won't be included because it is a hidden field raw, err = ljpack.Marshal(t2) if err != nil { panic(err) } var t3 T if err = ljpack.Unmarshal(raw, &t3); err != nil { panic(err) } fmt.Printf("%#v\n", t3) }
Output: ljpack_test.T{I:ljpack_test.NullInt{Valid:false, Int:0}, J:ljpack_test.NullInt{Valid:true, Int:0}, S:ljpack_test.Secretive{Visible:false, hidden:false}} ljpack_test.T{I:ljpack_test.NullInt{Valid:true, Int:42}, J:ljpack_test.NullInt{Valid:true, Int:0}, S:ljpack_test.Secretive{Visible:false, hidden:false}}
Example (MapStringInterface) ¶
package main import ( "fmt" "github.com/fffonion/ljpack" ) func main() { in := map[string]interface{}{"foo": 1, "hello": "world"} b, err := ljpack.Marshal(in) if err != nil { panic(err) } var out map[string]interface{} err = ljpack.Unmarshal(b, &out) if err != nil { panic(err) } fmt.Println("foo =", out["foo"]) fmt.Println("hello =", out["hello"]) }
Output: foo = 1 hello = world
Example (OmitEmpty) ¶
package main import ( "fmt" "github.com/fffonion/ljpack" ) func main() { type Item struct { Foo string Bar string } item := &Item{ Foo: "hello", } b, err := ljpack.Marshal(item) if err != nil { panic(err) } fmt.Printf("item: %q\n", b) type ItemOmitEmpty struct { _ljpack struct{} `ljpack:",omitempty"` Foo string Bar string } itemOmitEmpty := &ItemOmitEmpty{ Foo: "hello", } b, err = ljpack.Marshal(itemOmitEmpty) if err != nil { panic(err) } fmt.Printf("item2: %q\n", b) }
Output: item: "\x82\xa3Foo\xa5hello\xa3Bar\xa0" item2: "\x81\xa3Foo\xa5hello"
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.
Types ¶
type CustomDecoder ¶
type CustomEncoder ¶
Example ¶
package main import ( "fmt" "github.com/fffonion/ljpack" ) type customStruct struct { S string N int } var _ ljpack.CustomEncoder = (*customStruct)(nil) var _ ljpack.CustomDecoder = (*customStruct)(nil) func (s *customStruct) EncodeLJpack(enc *ljpack.Encoder) error { return enc.EncodeMulti(s.S, s.N) } func (s *customStruct) DecodeLJpack(dec *ljpack.Decoder) error { return dec.DecodeMulti(&s.S, &s.N) } func main() { b, err := ljpack.Marshal(&customStruct{S: "hello", N: 42}) if err != nil { panic(err) } var v customStruct err = ljpack.Unmarshal(b, &v) if err != nil { panic(err) } fmt.Printf("%#v", v) }
Output: ljpack_test.customStruct{S:"hello", N:42}
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder reads and decodes LJPack 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 ljpack 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) DecodeDouble ¶
DecodeDouble decodes ljpack float32/64 into Go float64.
func (*Decoder) DecodeFFIComplex ¶
func (d *Decoder) DecodeFFIComplex() (complex128, error)
func (*Decoder) DecodeFFIInt64 ¶
DecodeFFIInt64 decodes ljpack int8/16/32/64 and uint8/16/32/64 into Go int64.
func (*Decoder) DecodeFFIUint64 ¶
DecodeFFIUint64 decodes ljpack int8/16/32/64 and uint8/16/32/64 into Go uint64.
func (*Decoder) DecodeInt32 ¶
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) DecodeNull ¶
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) DecodeUint32 ¶
func (*Decoder) DecodeUntypedMap ¶
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 LJPack code without advancing the reader. Subpackage ljpack/codes defines the list of available ljpcode.
func (*Decoder) Query ¶
Query extracts data specified by the query from the ljpack stream skipping any other data. Query consists of map keys and array indexes separated with dot, e.g. key1.0.key2.
Example ¶
package main import ( "bytes" "fmt" "github.com/fffonion/ljpack" ) func main() { b, err := ljpack.Marshal([]map[string]interface{}{ {"id": 1, "attrs": map[string]interface{}{"phone": 12345}}, {"id": 2, "attrs": map[string]interface{}{"phone": 54321}}, }) if err != nil { panic(err) } dec := ljpack.NewDecoder(bytes.NewBuffer(b)) values, err := dec.Query("*.attrs.phone") if err != nil { panic(err) } fmt.Println("phones are", values) dec.Reset(bytes.NewBuffer(b)) values, err = dec.Query("1.attrs.phone") if err != nil { panic(err) } fmt.Println("2nd phone is", values[0]) }
Output: phones are [12345 54321] 2nd phone is 54321
func (*Decoder) Reset ¶
Reset discards any buffered data, resets all state, and switches the buffered reader to read from r.
func (*Decoder) SetCustomStructTag ¶
SetCustomStructTag causes the decoder to use the supplied tag as a fallback option if there is no ljpack tag.
func (*Decoder) SetMapDecoder ¶
Example ¶
package main import ( "bytes" "fmt" "github.com/fffonion/ljpack" ) func main() { buf := new(bytes.Buffer) enc := ljpack.NewEncoder(buf) in := map[string]string{"hello": "world"} err := enc.Encode(in) if err != nil { panic(err) } dec := ljpack.NewDecoder(buf) // Causes decoder to produce map[string]string instead of map[string]interface{}. dec.SetMapDecoder(func(d *ljpack.Decoder) (interface{}, error) { n, err := d.DecodeMapLen() if err != nil { return nil, err } m := make(map[string]string, n) for i := 0; i < n; i++ { mk, err := d.DecodeString() if err != nil { return nil, err } mv, err := d.DecodeString() if err != nil { return nil, err } m[mk] = mv } return m, nil }) out, err := dec.DecodeInterface() if err != nil { panic(err) } fmt.Printf("%#v", out) }
Output: map[string]string{"hello":"world"}
func (*Decoder) UseInternedStrings ¶
UseInternedStrings enables support for decoding interned strings.
func (*Decoder) UseLooseInterfaceDecoding ¶
UseLooseInterfaceDecoding causes decoder to use DecodeInterfaceLoose to decode ljpack value into Go interface{}.
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) EncodeDouble ¶
func (*Encoder) EncodeEmpty ¶
func (*Encoder) EncodeFFIInt64 ¶
EncodeFFIInt64 encodes an int64 in 9 bytes preserving type of the number.
func (*Encoder) EncodeFFIUint ¶
EncodeUnsignedNumber encodes an uint64 in 1, 2, 3, 5, or 9 bytes. Type of the number is lost during encoding.
func (*Encoder) EncodeFFIUint64 ¶
EncodeFFIUint64 encodes an uint16 in 9 bytes preserving type of the number.
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) EncodeInt32 ¶
EncodeInt32 encodes an int32 in 5 bytes preserving type of the number.
func (*Encoder) EncodeMapLen ¶
func (*Encoder) EncodeMapSorted ¶
func (*Encoder) EncodeMulti ¶
func (*Encoder) EncodeNoIntern ¶
func (*Encoder) EncodeNull ¶
func (*Encoder) EncodeString ¶
func (*Encoder) Reset ¶
Reset discards any buffered data, resets all state, and switches the writer to write to w.
func (*Encoder) SetCustomStructTag ¶
SetCustomStructTag causes the Encoder to use a custom struct tag as fallback option if there is no ljpack 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]interface{}
func (*Encoder) UseArrayEncodedStructs ¶
UseArrayEncodedStructs causes the Encoder to encode Go structs as ljpack arrays.
Example ¶
package main import ( "bytes" "fmt" "github.com/fffonion/ljpack" ) func main() { type Item struct { Foo string Bar string } var buf bytes.Buffer enc := ljpack.NewEncoder(&buf) enc.UseArrayEncodedStructs(true) err := enc.Encode(&Item{Foo: "foo", Bar: "bar"}) if err != nil { panic(err) } dec := ljpack.NewDecoder(&buf) v, err := dec.DecodeInterface() if err != nil { panic(err) } fmt.Println(v) }
Output: [foo bar]
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 ljpack int8 saving 7 bytes.
func (*Encoder) UseInternedStrings ¶
UseInternedStrings causes the Encoder to intern strings.
type RawMessage ¶
type RawMessage []byte
func (*RawMessage) DecodeLJpack ¶
func (m *RawMessage) DecodeLJpack(dec *Decoder) error
func (RawMessage) EncodeLJpack ¶
func (m RawMessage) EncodeLJpack(enc *Encoder) error