0doc.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. Typical usage model:
  80. // create and configure Handle
  81. var (
  82. bh codec.BincHandle
  83. mh codec.MsgpackHandle
  84. ch codec.CborHandle
  85. )
  86. mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
  87. // configure extensions
  88. // e.g. for msgpack, define functions and enable Time support for tag 1
  89. // mh.SetExt(reflect.TypeOf(time.Time{}), 1, myExt)
  90. // create and use decoder/encoder
  91. var (
  92. r io.Reader
  93. w io.Writer
  94. b []byte
  95. h = &bh // or mh to use msgpack
  96. )
  97. dec = codec.NewDecoder(r, h)
  98. dec = codec.NewDecoderBytes(b, h)
  99. err = dec.Decode(&v)
  100. enc = codec.NewEncoder(w, h)
  101. enc = codec.NewEncoderBytes(&b, h)
  102. err = enc.Encode(v)
  103. //RPC Server
  104. go func() {
  105. for {
  106. conn, err := listener.Accept()
  107. rpcCodec := codec.GoRpc.ServerCodec(conn, h)
  108. //OR rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h)
  109. rpc.ServeCodec(rpcCodec)
  110. }
  111. }()
  112. //RPC Communication (client side)
  113. conn, err = net.Dial("tcp", "localhost:5555")
  114. rpcCodec := codec.GoRpc.ClientCodec(conn, h)
  115. //OR rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, h)
  116. client := rpc.NewClientWithCodec(rpcCodec)
  117. */
  118. package codec