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

..08-Nov-2019-

0doc.goH A D08-Nov-20198.7 KiB2281

LICENSEH A D08-Nov-20191.1 KiB2318

README.mdH A D08-Nov-20197.9 KiB208161

binc.goH A D08-Nov-201928.2 KiB1,205973

build.shH A D08-Nov-20196.5 KiB226185

cbor.goH A D08-Nov-201917.5 KiB770652

codecgen.goH A D08-Nov-2019283 144

decode.goH A D08-Nov-201974.2 KiB3,0961,995

encode.goH A D08-Nov-201943.1 KiB1,7621,103

fast-path.generated.goH A D08-Nov-2019807.6 KiB35,07334,443

fast-path.go.tmplH A D08-Nov-201916.1 KiB555510

fast-path.not.goH A D08-Nov-20191.6 KiB4825

gen-dec-array.go.tmplH A D08-Nov-20192.8 KiB7978

gen-dec-map.go.tmplH A D08-Nov-20192.1 KiB4342

gen-enc-chan.go.tmplH A D08-Nov-2019647 2827

gen-helper.generated.goH A D08-Nov-201911.5 KiB352197

gen-helper.go.tmplH A D08-Nov-201911.5 KiB318297

gen.generated.goH A D08-Nov-20195.9 KiB165151

gen.goH A D08-Nov-201964.1 KiB2,1501,695

go.modH A D08-Nov-201971 42

goversion_arrayof_gte_go15.goH A D08-Nov-2019338 156

goversion_arrayof_lt_go15.goH A D08-Nov-2019367 156

goversion_makemap_gte_go19.goH A D08-Nov-2019358 168

goversion_makemap_lt_go19.goH A D08-Nov-2019288 135

goversion_unexportedembeddedptr_gte_go110.goH A D08-Nov-2019223 92

goversion_unexportedembeddedptr_lt_go110.goH A D08-Nov-2019223 92

goversion_unsupported_lt_go14.goH A D08-Nov-2019503 184

goversion_vendor_eq_go15.goH A D08-Nov-2019260 113

goversion_vendor_eq_go16.goH A D08-Nov-2019260 113

goversion_vendor_gte_go17.goH A D08-Nov-2019206 92

goversion_vendor_lt_go15.goH A D08-Nov-2019206 92

helper.goH A D08-Nov-201975.3 KiB2,6981,696

helper_internal.goH A D08-Nov-20193.1 KiB12276

helper_not_unsafe.goH A D08-Nov-20198 KiB327212

helper_unsafe.goH A D08-Nov-201922.7 KiB741355

json.goH A D08-Nov-201935.6 KiB1,5091,167

mammoth-test.go.tmplH A D08-Nov-20197.3 KiB155138

mammoth2-test.go.tmplH A D08-Nov-20192.8 KiB9577

msgpack.goH A D08-Nov-201928.3 KiB1,124893

rpc.goH A D08-Nov-20195.8 KiB226141

simple.goH A D08-Nov-201915.9 KiB668547

test-cbor-goldens.jsonH A D08-Nov-201910.1 KiB640637

test.pyH A D08-Nov-20193.9 KiB12798

xml.goH A D08-Nov-201917.9 KiB50988

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