• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

codec/H03-Nov-2019-

.travis.ymlH A D03-Nov-2019334

LICENSEH A D03-Nov-20191.1 KiB

README.mdH A D03-Nov-201911.4 KiB

go.modH A D03-Nov-2019111

msgpack.org.mdH A D03-Nov-20191.3 KiB

README.md

1[![Sourcegraph](https://sourcegraph.com/github.com/ugorji/go/-/badge.svg?v=4)](https://sourcegraph.com/github.com/ugorji/go/-/tree/codec?badge)
2[![Build Status](https://travis-ci.org/ugorji/go.svg?branch=master)](https://travis-ci.org/ugorji/go)
3[![codecov](https://codecov.io/gh/ugorji/go/branch/master/graph/badge.svg?v=4)](https://codecov.io/gh/ugorji/go)
4[![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/ugorji/go/codec)
5[![rcard](https://goreportcard.com/badge/github.com/ugorji/go/codec?v=4)](https://goreportcard.com/report/github.com/ugorji/go/codec)
6[![License](http://img.shields.io/badge/license-mit-blue.svg?style=flat-square)](https://raw.githubusercontent.com/ugorji/go/master/LICENSE)
7
8# go-codec
9
10This repository contains the `go-codec` library.
11
12To install:
13
14```
15go get github.com/ugorji/go/codec
16```
17
18# Package Documentation
19
20
21Package codec provides a High Performance, Feature-Rich Idiomatic Go 1.4+
22codec/encoding library for binc, msgpack, cbor, json.
23
24Supported Serialization formats are:
25
26  - msgpack: https://github.com/msgpack/msgpack
27  - binc:    http://github.com/ugorji/binc
28  - cbor:    http://cbor.io http://tools.ietf.org/html/rfc7049
29  - json:    http://json.org http://tools.ietf.org/html/rfc7159
30  - simple:
31
32This package will carefully use 'package unsafe' for performance reasons in
33specific places. You can build without unsafe use by passing the safe or
34appengine tag i.e. 'go install -tags=safe ...'. Note that unsafe is only
35supported for the last 4 go releases e.g. current go release is go 1.12, so
36we support unsafe use only from go 1.9+ . This is because supporting unsafe
37requires knowledge of implementation details.
38
39For detailed usage information, read the primer at
40http://ugorji.net/blog/go-codec-primer .
41
42The idiomatic Go support is as seen in other encoding packages in the
43standard library (ie json, xml, gob, etc).
44
45Rich Feature Set includes:
46
47  - Simple but extremely powerful and feature-rich API
48  - Support for go1.4 and above, while selectively using newer APIs for later releases
49  - Excellent code coverage ( > 90% )
50  - Very High Performance.
51    Our extensive benchmarks show us outperforming Gob, Json, Bson, etc by 2-4X.
52  - Careful selected use of 'unsafe' for targeted performance gains.
53    100% mode exists where 'unsafe' is not used at all.
54  - Lock-free (sans mutex) concurrency for scaling to 100's of cores
55  - In-place updates during decode, with option to zero value in maps and slices prior to decode
56  - Coerce types where appropriate
57    e.g. decode an int in the stream into a float, decode numbers from formatted strings, etc
58  - Corner Cases:
59    Overflows, nil maps/slices, nil values in streams are handled correctly
60  - Standard field renaming via tags
61  - Support for omitting empty fields during an encoding
62  - Encoding from any value and decoding into pointer to any value
63    (struct, slice, map, primitives, pointers, interface{}, etc)
64  - Extensions to support efficient encoding/decoding of any named types
65  - Support encoding.(Binary|Text)(M|Unm)arshaler interfaces
66  - Support IsZero() bool to determine if a value is a zero value.
67    Analogous to time.Time.IsZero() bool.
68  - Decoding without a schema (into a interface{}).
69    Includes Options to configure what specific map or slice type to use
70    when decoding an encoded list or map into a nil interface{}
71  - Mapping a non-interface type to an interface, so we can decode appropriately
72    into any interface type with a correctly configured non-interface value.
73  - Encode a struct as an array, and decode struct from an array in the data stream
74  - Option to encode struct keys as numbers (instead of strings)
75    (to support structured streams with fields encoded as numeric codes)
76  - Comprehensive support for anonymous fields
77  - Fast (no-reflection) encoding/decoding of common maps and slices
78  - Code-generation for faster performance.
79  - Support binary (e.g. messagepack, cbor) and text (e.g. json) formats
80  - Support indefinite-length formats to enable true streaming
81    (for formats which support it e.g. json, cbor)
82  - Support canonical encoding, where a value is ALWAYS encoded as same sequence of bytes.
83    This mostly applies to maps, where iteration order is non-deterministic.
84  - NIL in data stream decoded as zero value
85  - Never silently skip data when decoding.
86    User decides whether to return an error or silently skip data when keys or indexes
87    in the data stream do not map to fields in the struct.
88  - Detect and error when encoding a cyclic reference (instead of stack overflow shutdown)
89  - Encode/Decode from/to chan types (for iterative streaming support)
90  - Drop-in replacement for encoding/json. `json:` key in struct tag supported.
91  - Provides a RPC Server and Client Codec for net/rpc communication protocol.
92  - Handle unique idiosyncrasies of codecs e.g.
93    - For messagepack, configure how ambiguities in handling raw bytes are resolved
94    - For messagepack, provide rpc server/client codec to support
95      msgpack-rpc protocol defined at:
96      https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md
97
98
99## Extension Support
100
101Users can register a function to handle the encoding or decoding of their
102custom types.
103
104There are no restrictions on what the custom type can be. Some examples:
105
106```go
107    type BisSet   []int
108    type BitSet64 uint64
109    type UUID     string
110    type MyStructWithUnexportedFields struct { a int; b bool; c []int; }
111    type GifImage struct { ... }
112```
113
114As an illustration, MyStructWithUnexportedFields would normally be encoded
115as an empty map because it has no exported fields, while UUID would be
116encoded as a string. However, with extension support, you can encode any of
117these however you like.
118
119
120## Custom Encoding and Decoding
121
122This package maintains symmetry in the encoding and decoding halfs. We
123determine how to encode or decode by walking this decision tree
124
125  - is type a codec.Selfer?
126  - is there an extension registered for the type?
127  - is format binary, and is type a encoding.BinaryMarshaler and BinaryUnmarshaler?
128  - is format specifically json, and is type a encoding/json.Marshaler and Unmarshaler?
129  - is format text-based, and type an encoding.TextMarshaler and TextUnmarshaler?
130  - else we use a pair of functions based on the "kind" of the type e.g. map, slice, int64, etc
131
132This symmetry is important to reduce chances of issues happening because the
133encoding and decoding sides are out of sync e.g. decoded via very specific
134encoding.TextUnmarshaler but encoded via kind-specific generalized mode.
135
136Consequently, if a type only defines one-half of the symmetry (e.g. it
137implements UnmarshalJSON() but not MarshalJSON() ), then that type doesn't
138satisfy the check and we will continue walking down the decision tree.
139
140
141## RPC
142
143RPC Client and Server Codecs are implemented, so the codecs can be used with
144the standard net/rpc package.
145
146
147## Usage
148
149The Handle is SAFE for concurrent READ, but NOT SAFE for concurrent
150modification.
151
152The Encoder and Decoder are NOT safe for concurrent use.
153
154Consequently, the usage model is basically:
155
156  - Create and initialize the Handle before any use.
157    Once created, DO NOT modify it.
158  - Multiple Encoders or Decoders can now use the Handle concurrently.
159    They only read information off the Handle (never write).
160  - However, each Encoder or Decoder MUST not be used concurrently
161  - To re-use an Encoder/Decoder, call Reset(...) on it first.
162    This allows you use state maintained on the Encoder/Decoder.
163
164Sample usage model:
165
166```go
167    // create and configure Handle
168    var (
169      bh codec.BincHandle
170      mh codec.MsgpackHandle
171      ch codec.CborHandle
172    )
173
174    mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
175
176    // configure extensions
177    // e.g. for msgpack, define functions and enable Time support for tag 1
178    // mh.SetExt(reflect.TypeOf(time.Time{}), 1, myExt)
179
180    // create and use decoder/encoder
181    var (
182      r io.Reader
183      w io.Writer
184      b []byte
185      h = &bh // or mh to use msgpack
186    )
187
188    dec = codec.NewDecoder(r, h)
189    dec = codec.NewDecoderBytes(b, h)
190    err = dec.Decode(&v)
191
192    enc = codec.NewEncoder(w, h)
193    enc = codec.NewEncoderBytes(&b, h)
194    err = enc.Encode(v)
195
196    //RPC Server
197    go func() {
198        for {
199            conn, err := listener.Accept()
200            rpcCodec := codec.GoRpc.ServerCodec(conn, h)
201            //OR rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h)
202            rpc.ServeCodec(rpcCodec)
203        }
204    }()
205
206    //RPC Communication (client side)
207    conn, err = net.Dial("tcp", "localhost:5555")
208    rpcCodec := codec.GoRpc.ClientCodec(conn, h)
209    //OR rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, h)
210    client := rpc.NewClientWithCodec(rpcCodec)
211```
212
213
214## Running Tests
215
216To run tests, use the following:
217
218```
219    go test
220```
221
222To run the full suite of tests, use the following:
223
224```
225    go test -tags alltests -run Suite
226```
227
228You can run the tag 'safe' to run tests or build in safe mode. e.g.
229
230```
231    go test -tags safe -run Json
232    go test -tags "alltests safe" -run Suite
233```
234
235## Running Benchmarks
236
237```
238    cd bench
239    go test -bench . -benchmem -benchtime 1s
240```
241
242Please see http://github.com/ugorji/go-codec-bench .
243
244
245## Managing Binary Size
246
247This package could add up to 10MB to the size of your binaries.
248
249This is because we include some a auto-generated file:
250`fast-path.generated.go` to help with performance when encoding/decoding
251slices and maps of built in numeric, boolean, string and interface{} types.
252
253You can override this by building (or running tests and benchmarks) with the
254tag: `notfastpath`.
255
256```
257    go install -tags notfastpath
258    go build -tags notfastpath
259    go test -tags notfastpath
260```
261
262Be aware that, at least in our representative microbenchmarks for cbor (for
263example), we see up to 33% increase in decoding and 50% increase in encoding
264speeds. YMMV.
265
266
267## Caveats
268
269Struct fields matching the following are ignored during encoding and
270decoding
271
272  - struct tag value set to -
273  - func, complex numbers, unsafe pointers
274  - unexported and not embedded
275  - unexported and embedded and not struct kind
276  - unexported and embedded pointers (from go1.10)
277
278Every other field in a struct will be encoded/decoded.
279
280Embedded fields are encoded as if they exist in the top-level struct, with
281some caveats. See Encode documentation.
282
283## Exported Package API
284
285```go
286const CborStreamBytes byte = 0x5f ...
287const GenVersion = 10
288var GoRpc goRpc
289var MsgpackSpecRpc msgpackSpecRpc
290func GenHelperDecoder(d *Decoder) (gd genHelperDecoder, dd genHelperDecDriver)
291func GenHelperEncoder(e *Encoder) (ge genHelperEncoder, ee genHelperEncDriver)
292type BasicHandle struct{ ... }
293type BincHandle struct{ ... }
294type BytesExt interface{ ... }
295type CborHandle struct{ ... }
296type DecodeOptions struct{ ... }
297type Decoder struct{ ... }
298    func NewDecoder(r io.Reader, h Handle) *Decoder
299    func NewDecoderBytes(in []byte, h Handle) *Decoder
300type EncodeOptions struct{ ... }
301type Encoder struct{ ... }
302    func NewEncoder(w io.Writer, h Handle) *Encoder
303    func NewEncoderBytes(out *[]byte, h Handle) *Encoder
304type Ext interface{ ... }
305type Handle interface{ ... }
306type InterfaceExt interface{ ... }
307type JsonHandle struct{ ... }
308type MapBySlice interface{ ... }
309type MissingFielder interface{ ... }
310type MsgpackHandle struct{ ... }
311type MsgpackSpecRpcMultiArgs []interface{}
312type RPCOptions struct{ ... }
313type Raw []byte
314type RawExt struct{ ... }
315type Rpc interface{ ... }
316type Selfer interface{ ... }
317type SimpleHandle struct{ ... }
318type TypeInfos struct{ ... }
319    func NewTypeInfos(tags []string) *TypeInfos
320```
321