0doc.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 binc, msgpack, cbor, json.
  5. Supported Serialization formats are:
  6. - msgpack: [https://github.com/msgpack/msgpack]
  7. - binc: [http://github.com/ugorji/binc]
  8. - cbor: [http://cbor.io] [http://tools.ietf.org/html/rfc7049]
  9. - simple:
  10. - json: [http://json.org] [http://tools.ietf.org/html/rfc7159]
  11. To install:
  12. go get github.com/ugorji/go/codec
  13. The idiomatic Go support is as seen in other encoding packages in
  14. the standard library (ie json, xml, gob, etc).
  15. Rich Feature Set includes:
  16. - Simple but extremely powerful and feature-rich API
  17. - Very High Performance.
  18. Our extensive benchmarks show us outperforming Gob, Json and Bson by 2-4X.
  19. Achieved by extreme care on allocations, recursions, bypassing reflection, zero-copy, etc.
  20. - Multiple conversions:
  21. Package coerces types where appropriate e.g. decode an int in the stream into a float, etc
  22. - Corner Cases: Overflows, nil maps/slices, nil values in streams are handled correctly
  23. - Standard field renaming via tags
  24. - Encoding from any value and decoding into pointer to any value
  25. (struct, slice, map, primitives, pointers, interface{}, etc)
  26. - Supports extension functions to handle the encode/decode of custom types
  27. - Support encoding.(Binary|Text)(M|Unm)arshaler interfaces
  28. - Schema-less decoding
  29. (decode into a pointer to a nil interface{} as opposed to a typed value).
  30. Includes Options to configure what specific map or slice type to use
  31. when decoding an encoded list or map into a nil interface{}
  32. - Provides a RPC Server and Client Codec for net/rpc communication protocol.
  33. - Fast Paths for common maps and slices of built-in types (numbers, string, bool).
  34. Reflection (and its associated overhead) is bypassed.
  35. - Handle unique idiosynchracies of codecs e.g.
  36. - For messagepack, configure how ambiguities in handling raw bytes are resolved
  37. - For messagepack, provide rpc server/client codec to support msgpack-rpc protocol defined at:
  38. https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md
  39. Extension Support
  40. Users can register a function to handle the encoding or decoding of
  41. their custom types.
  42. There are no restrictions on what the custom type can be. Some examples:
  43. type BisSet []int
  44. type BitSet64 uint64
  45. type UUID string
  46. type MyStructWithUnexportedFields struct { a int; b bool; c []int; }
  47. type GifImage struct { ... }
  48. As an illustration, MyStructWithUnexportedFields would normally be
  49. encoded as an empty map because it has no exported fields, while UUID
  50. would be encoded as a string. However, with extension support, you can
  51. encode any of these however you like.
  52. RPC
  53. RPC Client and Server Codecs are implemented, so the codecs can be used
  54. with the standard net/rpc package.
  55. Usage
  56. Typical usage model:
  57. // create and configure Handle
  58. var (
  59. bh codec.BincHandle
  60. mh codec.MsgpackHandle
  61. ch codec.CborHandle
  62. )
  63. mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
  64. // configure extensions
  65. // e.g. for msgpack, define functions and enable Time support for tag 1
  66. // mh.SetExt(reflect.TypeOf(time.Time{}), 1, myExt)
  67. // create and use decoder/encoder
  68. var (
  69. r io.Reader
  70. w io.Writer
  71. b []byte
  72. h = &bh // or mh to use msgpack
  73. )
  74. dec = codec.NewDecoder(r, h)
  75. dec = codec.NewDecoderBytes(b, h)
  76. err = dec.Decode(&v)
  77. enc = codec.NewEncoder(w, h)
  78. enc = codec.NewEncoderBytes(&b, h)
  79. err = enc.Encode(v)
  80. //RPC Server
  81. go func() {
  82. for {
  83. conn, err := listener.Accept()
  84. rpcCodec := codec.GoRpc.ServerCodec(conn, h)
  85. //OR rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h)
  86. rpc.ServeCodec(rpcCodec)
  87. }
  88. }()
  89. //RPC Communication (client side)
  90. conn, err = net.Dial("tcp", "localhost:5555")
  91. rpcCodec := codec.GoRpc.ClientCodec(conn, h)
  92. //OR rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, h)
  93. client := rpc.NewClientWithCodec(rpcCodec)
  94. */
  95. package codec