0doc.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a BSD-style 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. For detailed usage information, read the primer at http://ugorji.net/blog/go-codec-primer .
  15. The idiomatic Go support is as seen in other encoding packages in
  16. the standard library (ie json, xml, gob, etc).
  17. Rich Feature Set includes:
  18. - Simple but extremely powerful and feature-rich API
  19. - Very High Performance.
  20. Our extensive benchmarks show us outperforming Gob, Json, Bson, etc by 2-4X.
  21. - Multiple conversions:
  22. Package coerces types where appropriate
  23. e.g. decode an int in the stream into a float, etc.
  24. - Corner Cases:
  25. Overflows, nil maps/slices, nil values in streams are handled correctly
  26. - Standard field renaming via tags
  27. - Support for omitting empty fields during an encoding
  28. - Encoding from any value and decoding into pointer to any value
  29. (struct, slice, map, primitives, pointers, interface{}, etc)
  30. - Extensions to support efficient encoding/decoding of any named types
  31. - Support encoding.(Binary|Text)(M|Unm)arshaler interfaces
  32. - Decoding without a schema (into a interface{}).
  33. Includes Options to configure what specific map or slice type to use
  34. when decoding an encoded list or map into a nil interface{}
  35. - Encode a struct as an array, and decode struct from an array in the data stream
  36. - Comprehensive support for anonymous fields
  37. - Fast (no-reflection) encoding/decoding of common maps and slices
  38. - Code-generation for faster performance.
  39. - Support binary (e.g. messagepack, cbor) and text (e.g. json) formats
  40. - Support indefinite-length formats to enable true streaming
  41. - NIL in data stream decoded as zero value
  42. - Never silently skip data when decoding.
  43. User decides whether to return an error or silently skip data when keys or indexes
  44. in the data stream do not map to fields in the struct.
  45. - Provides a RPC Server and Client Codec for net/rpc communication protocol.
  46. - Handle unique idiosynchracies of codecs e.g.
  47. - For messagepack, configure how ambiguities in handling raw bytes are resolved
  48. - For messagepack, provide rpc server/client codec to support
  49. msgpack-rpc protocol defined at:
  50. https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md
  51. Extension Support
  52. Users can register a function to handle the encoding or decoding of
  53. their custom types.
  54. There are no restrictions on what the custom type can be. Some examples:
  55. type BisSet []int
  56. type BitSet64 uint64
  57. type UUID string
  58. type MyStructWithUnexportedFields struct { a int; b bool; c []int; }
  59. type GifImage struct { ... }
  60. As an illustration, MyStructWithUnexportedFields would normally be
  61. encoded as an empty map because it has no exported fields, while UUID
  62. would be encoded as a string. However, with extension support, you can
  63. encode any of these however you like.
  64. RPC
  65. RPC Client and Server Codecs are implemented, so the codecs can be used
  66. with the standard net/rpc package.
  67. Usage
  68. Typical usage model:
  69. // create and configure Handle
  70. var (
  71. bh codec.BincHandle
  72. mh codec.MsgpackHandle
  73. ch codec.CborHandle
  74. )
  75. mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
  76. // configure extensions
  77. // e.g. for msgpack, define functions and enable Time support for tag 1
  78. // mh.SetExt(reflect.TypeOf(time.Time{}), 1, myExt)
  79. // create and use decoder/encoder
  80. var (
  81. r io.Reader
  82. w io.Writer
  83. b []byte
  84. h = &bh // or mh to use msgpack
  85. )
  86. dec = codec.NewDecoder(r, h)
  87. dec = codec.NewDecoderBytes(b, h)
  88. err = dec.Decode(&v)
  89. enc = codec.NewEncoder(w, h)
  90. enc = codec.NewEncoderBytes(&b, h)
  91. err = enc.Encode(v)
  92. //RPC Server
  93. go func() {
  94. for {
  95. conn, err := listener.Accept()
  96. rpcCodec := codec.GoRpc.ServerCodec(conn, h)
  97. //OR rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h)
  98. rpc.ServeCodec(rpcCodec)
  99. }
  100. }()
  101. //RPC Communication (client side)
  102. conn, err = net.Dial("tcp", "localhost:5555")
  103. rpcCodec := codec.GoRpc.ClientCodec(conn, h)
  104. //OR rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, h)
  105. client := rpc.NewClientWithCodec(rpcCodec)
  106. */
  107. package codec