0doc.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. High Performance, Feature-Rich Idiomatic Go codec/encoding library for
  5. binc, msgpack, cbor, json.
  6. Supported Serialization formats are:
  7. - msgpack: https://github.com/msgpack/msgpack
  8. - binc: http://github.com/ugorji/binc
  9. - cbor: http://cbor.io http://tools.ietf.org/html/rfc7049
  10. - json: http://json.org http://tools.ietf.org/html/rfc7159
  11. - simple:
  12. To install:
  13. go get github.com/ugorji/go/codec
  14. This package understands the 'unsafe' tag, to allow using unsafe semantics:
  15. - When decoding into a struct, you need to read the field name as a string
  16. so you can find the struct field it is mapped to.
  17. Using `unsafe` will bypass the allocation and copying overhead of []byte->string conversion.
  18. To install using unsafe, pass the 'unsafe' tag:
  19. go get -tags=unsafe github.com/ugorji/go/codec
  20. For detailed usage information, read the primer at http://ugorji.net/blog/go-codec-primer .
  21. The idiomatic Go support is as seen in other encoding packages in
  22. the standard library (ie json, xml, gob, etc).
  23. Rich Feature Set includes:
  24. - Simple but extremely powerful and feature-rich API
  25. - Very High Performance.
  26. Our extensive benchmarks show us outperforming Gob, Json, Bson, etc by 2-4X.
  27. - Multiple conversions:
  28. Package coerces types where appropriate
  29. e.g. decode an int in the stream into a float, etc.
  30. - Corner Cases:
  31. Overflows, nil maps/slices, nil values in streams are handled correctly
  32. - Standard field renaming via tags
  33. - Support for omitting empty fields during an encoding
  34. - Encoding from any value and decoding into pointer to any value
  35. (struct, slice, map, primitives, pointers, interface{}, etc)
  36. - Extensions to support efficient encoding/decoding of any named types
  37. - Support encoding.(Binary|Text)(M|Unm)arshaler interfaces
  38. - Decoding without a schema (into a interface{}).
  39. Includes Options to configure what specific map or slice type to use
  40. when decoding an encoded list or map into a nil interface{}
  41. - Encode a struct as an array, and decode struct from an array in the data stream
  42. - Comprehensive support for anonymous fields
  43. - Fast (no-reflection) encoding/decoding of common maps and slices
  44. - Code-generation for faster performance.
  45. - Support binary (e.g. messagepack, cbor) and text (e.g. json) formats
  46. - Support indefinite-length formats to enable true streaming
  47. (for formats which support it e.g. json, cbor)
  48. - Support canonical encoding, where a value is ALWAYS encoded as same sequence of bytes.
  49. This mostly applies to maps, where iteration order is non-deterministic.
  50. - NIL in data stream decoded as zero value
  51. - Never silently skip data when decoding.
  52. User decides whether to return an error or silently skip data when keys or indexes
  53. in the data stream do not map to fields in the struct.
  54. - Encode/Decode from/to chan types (for iterative streaming support)
  55. - Drop-in replacement for encoding/json. `json:` key in struct tag supported.
  56. - Provides a RPC Server and Client Codec for net/rpc communication protocol.
  57. - Handle unique idiosynchracies of codecs e.g.
  58. - For messagepack, configure how ambiguities in handling raw bytes are resolved
  59. - For messagepack, provide rpc server/client codec to support
  60. msgpack-rpc protocol defined at:
  61. https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md
  62. Extension Support
  63. Users can register a function to handle the encoding or decoding of
  64. their custom types.
  65. There are no restrictions on what the custom type can be. Some examples:
  66. type BisSet []int
  67. type BitSet64 uint64
  68. type UUID string
  69. type MyStructWithUnexportedFields struct { a int; b bool; c []int; }
  70. type GifImage struct { ... }
  71. As an illustration, MyStructWithUnexportedFields would normally be
  72. encoded as an empty map because it has no exported fields, while UUID
  73. would be encoded as a string. However, with extension support, you can
  74. encode any of these however you like.
  75. RPC
  76. RPC Client and Server Codecs are implemented, so the codecs can be used
  77. with the standard net/rpc package.
  78. Usage
  79. The Handle is SAFE for concurrent READ, but NOT SAFE for concurrent modification.
  80. The Encoder and Decoder are NOT safe for concurrent use.
  81. Consequently, the usage model is basically:
  82. - Create and initialize the Handle before any use.
  83. Once created, DO NOT modify it.
  84. - Multiple Encoders or Decoders can now use the Handle concurrently.
  85. They only read information off the Handle (never write).
  86. - However, each Encoder or Decoder MUST not be used concurrently
  87. - To re-use an Encoder/Decoder, call Reset(...) on it first.
  88. This allows you use state maintained on the Encoder/Decoder.
  89. Sample usage model:
  90. // create and configure Handle
  91. var (
  92. bh codec.BincHandle
  93. mh codec.MsgpackHandle
  94. ch codec.CborHandle
  95. )
  96. mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
  97. // configure extensions
  98. // e.g. for msgpack, define functions and enable Time support for tag 1
  99. // mh.SetExt(reflect.TypeOf(time.Time{}), 1, myExt)
  100. // create and use decoder/encoder
  101. var (
  102. r io.Reader
  103. w io.Writer
  104. b []byte
  105. h = &bh // or mh to use msgpack
  106. )
  107. dec = codec.NewDecoder(r, h)
  108. dec = codec.NewDecoderBytes(b, h)
  109. err = dec.Decode(&v)
  110. enc = codec.NewEncoder(w, h)
  111. enc = codec.NewEncoderBytes(&b, h)
  112. err = enc.Encode(v)
  113. //RPC Server
  114. go func() {
  115. for {
  116. conn, err := listener.Accept()
  117. rpcCodec := codec.GoRpc.ServerCodec(conn, h)
  118. //OR rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h)
  119. rpc.ServeCodec(rpcCodec)
  120. }
  121. }()
  122. //RPC Communication (client side)
  123. conn, err = net.Dial("tcp", "localhost:5555")
  124. rpcCodec := codec.GoRpc.ClientCodec(conn, h)
  125. //OR rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, h)
  126. client := rpc.NewClientWithCodec(rpcCodec)
  127. */
  128. package codec
  129. // Benefits of go-codec:
  130. //
  131. // - encoding/json always reads whole file into memory first.
  132. // This makes it unsuitable for parsing very large files.
  133. // - encoding/xml cannot parse into a map[string]interface{}
  134. // I found this out on reading https://github.com/clbanning/mxj
  135. // TODO:
  136. //
  137. // - (En|De)coder should store an error when it occurs.
  138. // Until reset, subsequent calls return that error that was stored.
  139. // This means that free panics must go away.
  140. // All errors must be raised through errorf method.
  141. // - Decoding using a chan is good, but incurs concurrency costs.
  142. // This is because there's no fast way to use a channel without it
  143. // having to switch goroutines constantly.
  144. // Callback pattern is still the best. Maybe cnsider supporting something like:
  145. // type X struct {
  146. // Name string
  147. // Ys []Y
  148. // Ys chan <- Y
  149. // Ys func(interface{}) -> call this interface for each entry in there.
  150. // }
  151. // - Consider adding a isZeroer interface { isZero() bool }
  152. // It is used within isEmpty, for omitEmpty support.
  153. // - Consider making Handle used AS-IS within the encoding/decoding session.
  154. // This means that we don't cache Handle information within the (En|De)coder,
  155. // except we really need it at Reset(...)
  156. // - Handle recursive types during encoding/decoding?