Debian Codegraph @

vendor/github.com/ugorji/go/codec/0doc.go

raw
1 // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
2 // Use of this source code is governed by a MIT license found in the LICENSE file.
3
4 /*
5 High Performance, Feature-Rich Idiomatic Go 1.4+ codec/encoding library for
6 binc, msgpack, cbor, json
7
8 Supported Serialization formats are:
9
10 - msgpack: https://github.com/msgpack/msgpack
11 - binc: http://github.com/ugorji/binc
12 - cbor: http://cbor.io http://tools.ietf.org/html/rfc7049
13 - json: http://json.org http://tools.ietf.org/html/rfc7159
14 - simple:
15
16 To install:
17
18 go get github.com/ugorji/go/codec
19
20 This package will carefully use 'unsafe' for performance reasons in specific places.
21 You can build without unsafe use by passing the safe or appengine tag
22 i.e. 'go install -tags=safe ...'. Note that unsafe is only supported for the last 3
23 go sdk versions e.g. current go release is go 1.9, so we support unsafe use only from
24 go 1.7+ . This is because supporting unsafe requires knowledge of implementation details.
25
26 For detailed usage information, read the primer at http://ugorji.net/blog/go-codec-primer .
27
28 The idiomatic Go support is as seen in other encoding packages in
29 the standard library (ie json, xml, gob, etc).
30
31 Rich Feature Set includes:
32
33 - Simple but extremely powerful and feature-rich API
34 - Support for go1.4 and above, while selectively using newer APIs for later releases
35 - Good code coverage ( > 70% )
36 - Very High Performance.
37 Our extensive benchmarks show us outperforming Gob, Json, Bson, etc by 2-4X.
38 - Careful selected use of 'unsafe' for targeted performance gains.
39 100% mode exists where 'unsafe' is not used at all.
40 - Lock-free (sans mutex) concurrency for scaling to 100's of cores
41 - Multiple conversions:
42 Package coerces types where appropriate
43 e.g. decode an int in the stream into a float, etc.
44 - Corner Cases:
45 Overflows, nil maps/slices, nil values in streams are handled correctly
46 - Standard field renaming via tags
47 - Support for omitting empty fields during an encoding
48 - Encoding from any value and decoding into pointer to any value
49 (struct, slice, map, primitives, pointers, interface{}, etc)
50 - Extensions to support efficient encoding/decoding of any named types
51 - Support encoding.(Binary|Text)(M|Unm)arshaler interfaces
52 - Decoding without a schema (into a interface{}).
53 Includes Options to configure what specific map or slice type to use
54 when decoding an encoded list or map into a nil interface{}
55 - Encode a struct as an array, and decode struct from an array in the data stream
56 - Comprehensive support for anonymous fields
57 - Fast (no-reflection) encoding/decoding of common maps and slices
58 - Code-generation for faster performance.
59 - Support binary (e.g. messagepack, cbor) and text (e.g. json) formats
60 - Support indefinite-length formats to enable true streaming
61 (for formats which support it e.g. json, cbor)
62 - Support canonical encoding, where a value is ALWAYS encoded as same sequence of bytes.
63 This mostly applies to maps, where iteration order is non-deterministic.
64 - NIL in data stream decoded as zero value
65 - Never silently skip data when decoding.
66 User decides whether to return an error or silently skip data when keys or indexes
67 in the data stream do not map to fields in the struct.
68 - Detect and error when encoding a cyclic reference (instead of stack overflow shutdown)
69 - Encode/Decode from/to chan types (for iterative streaming support)
70 - Drop-in replacement for encoding/json. `json:` key in struct tag supported.
71 - Provides a RPC Server and Client Codec for net/rpc communication protocol.
72 - Handle unique idiosyncrasies of codecs e.g.
73 - For messagepack, configure how ambiguities in handling raw bytes are resolved
74 - For messagepack, provide rpc server/client codec to support
75 msgpack-rpc protocol defined at:
76 https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md
77
78 Extension Support
79
80 Users can register a function to handle the encoding or decoding of
81 their custom types.
82
83 There are no restrictions on what the custom type can be. Some examples:
84
85 type BisSet []int
86 type BitSet64 uint64
87 type UUID string
88 type MyStructWithUnexportedFields struct { a int; b bool; c []int; }
89 type GifImage struct { ... }
90
91 As an illustration, MyStructWithUnexportedFields would normally be
92 encoded as an empty map because it has no exported fields, while UUID
93 would be encoded as a string. However, with extension support, you can
94 encode any of these however you like.
95
96 RPC
97
98 RPC Client and Server Codecs are implemented, so the codecs can be used
99 with the standard net/rpc package.
100
101 Usage
102
103 The Handle is SAFE for concurrent READ, but NOT SAFE for concurrent modification.
104
105 The Encoder and Decoder are NOT safe for concurrent use.
106
107 Consequently, the usage model is basically:
108
109 - Create and initialize the Handle before any use.
110 Once created, DO NOT modify it.
111 - Multiple Encoders or Decoders can now use the Handle concurrently.
112 They only read information off the Handle (never write).
113 - However, each Encoder or Decoder MUST not be used concurrently
114 - To re-use an Encoder/Decoder, call Reset(...) on it first.
115 This allows you use state maintained on the Encoder/Decoder.
116
117 Sample usage model:
118
119 // create and configure Handle
120 var (
121 bh codec.BincHandle
122 mh codec.MsgpackHandle
123 ch codec.CborHandle
124 )
125
126 mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
127
128 // configure extensions
129 // e.g. for msgpack, define functions and enable Time support for tag 1
130 // mh.SetExt(reflect.TypeOf(time.Time{}), 1, myExt)
131
132 // create and use decoder/encoder
133 var (
134 r io.Reader
135 w io.Writer
136 b []byte
137 h = &bh // or mh to use msgpack
138 )
139
140 dec = codec.NewDecoder(r, h)
141 dec = codec.NewDecoderBytes(b, h)
142 err = dec.Decode(&v)
143
144 enc = codec.NewEncoder(w, h)
145 enc = codec.NewEncoderBytes(&b, h)
146 err = enc.Encode(v)
147
148 //RPC Server
149 go func() {
150 for {
151 conn, err := listener.Accept()
152 rpcCodec := codec.GoRpc.ServerCodec(conn, h)
153 //OR rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h)
154 rpc.ServeCodec(rpcCodec)
155 }
156 }()
157
158 //RPC Communication (client side)
159 conn, err = net.Dial("tcp", "localhost:5555")
160 rpcCodec := codec.GoRpc.ClientCodec(conn, h)
161 //OR rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, h)
162 client := rpc.NewClientWithCodec(rpcCodec)
163
164 Running Tests
165
166 To run tests, use the following:
167
168 go test
169
170 To run the full suite of tests, use the following:
171
172 go test -tags alltests -run Suite
173
174 You can run the tag 'safe' to run tests or build in safe mode. e.g.
175
176 go test -tags safe -run Json
177 go test -tags "alltests safe" -run Suite
178
179 Running Benchmarks
180
181 Please see http://github.com/ugorji/go-codec-bench .
182
183 */
184 package codec
185