0doc.go 4.6 KB

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