README.md
1# Codec
2
3High Performance, Feature-Rich Idiomatic Go codec/encoding library for
4binc, msgpack, cbor, json.
5
6Supported Serialization formats are:
7
8 - msgpack: https://github.com/msgpack/msgpack
9 - binc: http://github.com/ugorji/binc
10 - cbor: http://cbor.io http://tools.ietf.org/html/rfc7049
11 - json: http://json.org http://tools.ietf.org/html/rfc7159
12 - simple:
13
14To install:
15
16 go get github.com/ugorji/go/codec
17
18This package will carefully use 'unsafe' for performance reasons in specific places.
19You can build without unsafe use by passing the safe or appengine tag
20i.e. 'go install -tags=safe ...'. Note that unsafe is only supported for the last 3
21go sdk versions e.g. current go release is go 1.9, so we support unsafe use only from
22go 1.7+ . This is because supporting unsafe requires knowledge of implementation details.
23
24Online documentation: http://godoc.org/github.com/ugorji/go/codec
25Detailed Usage/How-to Primer: http://ugorji.net/blog/go-codec-primer
26
27The idiomatic Go support is as seen in other encoding packages in
28the standard library (ie json, xml, gob, etc).
29
30Rich Feature Set includes:
31
32 - Simple but extremely powerful and feature-rich API
33 - Support for go1.4 and above, while selectively using newer APIs for later releases
34 - Excellent code coverage ( > 90% )
35 - Very High Performance.
36 Our extensive benchmarks show us outperforming Gob, Json, Bson, etc by 2-4X.
37 - Careful selected use of 'unsafe' for targeted performance gains.
38 100% mode exists where 'unsafe' is not used at all.
39 - Lock-free (sans mutex) concurrency for scaling to 100's of cores
40 - In-place updates during decode, with option to zero the value in maps and slices prior to decode
41 - Coerce types where appropriate
42 e.g. decode an int in the stream into a float, decode numbers from formatted strings, etc
43 - Corner Cases:
44 Overflows, nil maps/slices, nil values in streams are handled correctly
45 - Standard field renaming via tags
46 - Support for omitting empty fields during an encoding
47 - Encoding from any value and decoding into pointer to any value
48 (struct, slice, map, primitives, pointers, interface{}, etc)
49 - Extensions to support efficient encoding/decoding of any named types
50 - Support encoding.(Binary|Text)(M|Unm)arshaler interfaces
51 - Support IsZero() bool to determine if a value is a zero value.
52 Analogous to time.Time.IsZero() bool.
53 - Decoding without a schema (into a interface{}).
54 Includes Options to configure what specific map or slice type to use
55 when decoding an encoded list or map into a nil interface{}
56 - Mapping a non-interface type to an interface, so we can decode appropriately
57 into any interface type with a correctly configured non-interface value.
58 - Encode a struct as an array, and decode struct from an array in the data stream
59 - Option to encode struct keys as numbers (instead of strings)
60 (to support structured streams with fields encoded as numeric codes)
61 - Comprehensive support for anonymous fields
62 - Fast (no-reflection) encoding/decoding of common maps and slices
63 - Code-generation for faster performance.
64 - Support binary (e.g. messagepack, cbor) and text (e.g. json) formats
65 - Support indefinite-length formats to enable true streaming
66 (for formats which support it e.g. json, cbor)
67 - Support canonical encoding, where a value is ALWAYS encoded as same sequence of bytes.
68 This mostly applies to maps, where iteration order is non-deterministic.
69 - NIL in data stream decoded as zero value
70 - Never silently skip data when decoding.
71 User decides whether to return an error or silently skip data when keys or indexes
72 in the data stream do not map to fields in the struct.
73 - Encode/Decode from/to chan types (for iterative streaming support)
74 - Drop-in replacement for encoding/json. `json:` key in struct tag supported.
75 - Provides a RPC Server and Client Codec for net/rpc communication protocol.
76 - Handle unique idiosyncrasies of codecs e.g.
77 - For messagepack, configure how ambiguities in handling raw bytes are resolved
78 - For messagepack, provide rpc server/client codec to support
79 msgpack-rpc protocol defined at:
80 https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md
81
82## Extension Support
83
84Users can register a function to handle the encoding or decoding of
85their custom types.
86
87There are no restrictions on what the custom type can be. Some examples:
88
89 type BisSet []int
90 type BitSet64 uint64
91 type UUID string
92 type MyStructWithUnexportedFields struct { a int; b bool; c []int; }
93 type GifImage struct { ... }
94
95As an illustration, MyStructWithUnexportedFields would normally be
96encoded as an empty map because it has no exported fields, while UUID
97would be encoded as a string. However, with extension support, you can
98encode any of these however you like.
99
100## Custom Encoding and Decoding
101
102This package maintains symmetry in the encoding and decoding halfs.
103We determine how to encode or decode by walking this decision tree
104
105 - is type a codec.Selfer?
106 - is there an extension registered for the type?
107 - is format binary, and is type a encoding.BinaryMarshaler and BinaryUnmarshaler?
108 - is format specifically json, and is type a encoding/json.Marshaler and Unmarshaler?
109 - is format text-based, and type an encoding.TextMarshaler?
110 - else we use a pair of functions based on the "kind" of the type e.g. map, slice, int64, etc
111
112This symmetry is important to reduce chances of issues happening because the
113encoding and decoding sides are out of sync e.g. decoded via very specific
114encoding.TextUnmarshaler but encoded via kind-specific generalized mode.
115
116Consequently, if a type only defines one-half of the symmetry
117(e.g. it implements UnmarshalJSON() but not MarshalJSON() ),
118then that type doesn't satisfy the check and we will continue walking down the
119decision tree.
120
121## RPC
122
123RPC Client and Server Codecs are implemented, so the codecs can be used
124with the standard net/rpc package.
125
126## Usage
127
128Typical usage model:
129
130 // create and configure Handle
131 var (
132 bh codec.BincHandle
133 mh codec.MsgpackHandle
134 ch codec.CborHandle
135 )
136
137 mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
138
139 // configure extensions
140 // e.g. for msgpack, define functions and enable Time support for tag 1
141 // mh.SetExt(reflect.TypeOf(time.Time{}), 1, myExt)
142
143 // create and use decoder/encoder
144 var (
145 r io.Reader
146 w io.Writer
147 b []byte
148 h = &bh // or mh to use msgpack
149 )
150
151 dec = codec.NewDecoder(r, h)
152 dec = codec.NewDecoderBytes(b, h)
153 err = dec.Decode(&v)
154
155 enc = codec.NewEncoder(w, h)
156 enc = codec.NewEncoderBytes(&b, h)
157 err = enc.Encode(v)
158
159 //RPC Server
160 go func() {
161 for {
162 conn, err := listener.Accept()
163 rpcCodec := codec.GoRpc.ServerCodec(conn, h)
164 //OR rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h)
165 rpc.ServeCodec(rpcCodec)
166 }
167 }()
168
169 //RPC Communication (client side)
170 conn, err = net.Dial("tcp", "localhost:5555")
171 rpcCodec := codec.GoRpc.ClientCodec(conn, h)
172 //OR rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, h)
173 client := rpc.NewClientWithCodec(rpcCodec)
174
175## Running Tests
176
177To run tests, use the following:
178
179 go test
180
181To run the full suite of tests, use the following:
182
183 go test -tags alltests -run Suite
184
185You can run the tag 'safe' to run tests or build in safe mode. e.g.
186
187 go test -tags safe -run Json
188 go test -tags "alltests safe" -run Suite
189
190## Running Benchmarks
191
192Please see http://github.com/ugorji/go-codec-bench .
193
194## Caveats
195
196Struct fields matching the following are ignored during encoding and decoding
197
198 - struct tag value set to -
199 - func, complex numbers, unsafe pointers
200 - unexported and not embedded
201 - unexported and embedded and not struct kind
202 - unexported and embedded pointers (from go1.10)
203
204Every other field in a struct will be encoded/decoded.
205
206Embedded fields are encoded as if they exist in the top-level struct,
207with some caveats. See Encode documentation.
208