0doc.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. 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. - NIL in data stream decoded as zero value
  48. - Never silently skip data when decoding.
  49. User decides whether to return an error or silently skip data when keys or indexes
  50. in the data stream do not map to fields in the struct.
  51. - Provides a RPC Server and Client Codec for net/rpc communication protocol.
  52. - Handle unique idiosynchracies of codecs e.g.
  53. - For messagepack, configure how ambiguities in handling raw bytes are resolved
  54. - For messagepack, provide rpc server/client codec to support
  55. msgpack-rpc protocol defined at:
  56. https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md
  57. Extension Support
  58. Users can register a function to handle the encoding or decoding of
  59. their custom types.
  60. There are no restrictions on what the custom type can be. Some examples:
  61. type BisSet []int
  62. type BitSet64 uint64
  63. type UUID string
  64. type MyStructWithUnexportedFields struct { a int; b bool; c []int; }
  65. type GifImage struct { ... }
  66. As an illustration, MyStructWithUnexportedFields would normally be
  67. encoded as an empty map because it has no exported fields, while UUID
  68. would be encoded as a string. However, with extension support, you can
  69. encode any of these however you like.
  70. RPC
  71. RPC Client and Server Codecs are implemented, so the codecs can be used
  72. with the standard net/rpc package.
  73. Usage
  74. Typical usage model:
  75. // create and configure Handle
  76. var (
  77. bh codec.BincHandle
  78. mh codec.MsgpackHandle
  79. ch codec.CborHandle
  80. )
  81. mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
  82. // configure extensions
  83. // e.g. for msgpack, define functions and enable Time support for tag 1
  84. // mh.SetExt(reflect.TypeOf(time.Time{}), 1, myExt)
  85. // create and use decoder/encoder
  86. var (
  87. r io.Reader
  88. w io.Writer
  89. b []byte
  90. h = &bh // or mh to use msgpack
  91. )
  92. dec = codec.NewDecoder(r, h)
  93. dec = codec.NewDecoderBytes(b, h)
  94. err = dec.Decode(&v)
  95. enc = codec.NewEncoder(w, h)
  96. enc = codec.NewEncoderBytes(&b, h)
  97. err = enc.Encode(v)
  98. //RPC Server
  99. go func() {
  100. for {
  101. conn, err := listener.Accept()
  102. rpcCodec := codec.GoRpc.ServerCodec(conn, h)
  103. //OR rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h)
  104. rpc.ServeCodec(rpcCodec)
  105. }
  106. }()
  107. //RPC Communication (client side)
  108. conn, err = net.Dial("tcp", "localhost:5555")
  109. rpcCodec := codec.GoRpc.ClientCodec(conn, h)
  110. //OR rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, h)
  111. client := rpc.NewClientWithCodec(rpcCodec)
  112. */
  113. package codec