doc.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (c) Faye Amacker. All rights reserved.
  2. // Licensed under the MIT License. See LICENSE in the project root for license information.
  3. /*
  4. Package cbor is a modern CBOR codec (RFC 8949 & RFC 8742) with CBOR tags,
  5. Go struct tag options (toarray/keyasint/omitempty/omitzero), Core Deterministic Encoding,
  6. CTAP2, Canonical CBOR, float64->32->16, and duplicate map key detection.
  7. Encoding options allow "preferred serialization" by encoding integers and floats
  8. to their smallest forms (e.g. float16) when values fit.
  9. Struct tag options "keyasint", "toarray", "omitempty", and "omitzero" reduce encoding size
  10. and reduce programming effort.
  11. For example, "toarray" tag makes struct fields encode to CBOR array elements. And
  12. "keyasint" makes a field encode to an element of CBOR map with specified int key.
  13. Latest docs can be viewed at https://github.com/fxamacker/cbor#cbor-library-in-go
  14. # Basics
  15. The Quick Start guide is at https://github.com/fxamacker/cbor#quick-start
  16. Function signatures identical to encoding/json include:
  17. Marshal, Unmarshal, NewEncoder, NewDecoder, (*Encoder).Encode, (*Decoder).Decode
  18. Standard interfaces include:
  19. BinaryMarshaler, BinaryUnmarshaler, Marshaler, and Unmarshaler
  20. Diagnostic functions translate CBOR data item into Diagnostic Notation:
  21. Diagnose, DiagnoseFirst
  22. Functions that simplify using CBOR Sequences (RFC 8742) include:
  23. UnmarshalFirst
  24. Custom encoding and decoding is possible by implementing standard interfaces for
  25. user-defined Go types.
  26. Codec functions are available at package-level (using defaults options) or by
  27. creating modes from options at runtime.
  28. "Mode" in this API means definite way of encoding (EncMode) or decoding (DecMode).
  29. EncMode and DecMode interfaces are created from EncOptions or DecOptions structs.
  30. em, err := cbor.EncOptions{...}.EncMode()
  31. em, err := cbor.CanonicalEncOptions().EncMode()
  32. em, err := cbor.CTAP2EncOptions().EncMode()
  33. Modes use immutable options to avoid side-effects and simplify concurrency. Behavior of
  34. modes won't accidentally change at runtime after they're created.
  35. Modes are intended to be reused and are safe for concurrent use.
  36. EncMode and DecMode Interfaces
  37. // EncMode interface uses immutable options and is safe for concurrent use.
  38. type EncMode interface {
  39. Marshal(v interface{}) ([]byte, error)
  40. NewEncoder(w io.Writer) *Encoder
  41. EncOptions() EncOptions // returns copy of options
  42. }
  43. // DecMode interface uses immutable options and is safe for concurrent use.
  44. type DecMode interface {
  45. Unmarshal(data []byte, v interface{}) error
  46. NewDecoder(r io.Reader) *Decoder
  47. DecOptions() DecOptions // returns copy of options
  48. }
  49. Using Default Encoding Mode
  50. b, err := cbor.Marshal(v)
  51. encoder := cbor.NewEncoder(w)
  52. err = encoder.Encode(v)
  53. Using Default Decoding Mode
  54. err := cbor.Unmarshal(b, &v)
  55. decoder := cbor.NewDecoder(r)
  56. err = decoder.Decode(&v)
  57. Using Default Mode of UnmarshalFirst to Decode CBOR Sequences
  58. // Decode the first CBOR data item and return remaining bytes:
  59. rest, err = cbor.UnmarshalFirst(b, &v) // decode []byte b to v
  60. Using Extended Diagnostic Notation (EDN) to represent CBOR data
  61. // Translate the first CBOR data item into text and return remaining bytes.
  62. text, rest, err = cbor.DiagnoseFirst(b) // decode []byte b to text
  63. Creating and Using Encoding Modes
  64. // Create EncOptions using either struct literal or a function.
  65. opts := cbor.CanonicalEncOptions()
  66. // If needed, modify encoding options
  67. opts.Time = cbor.TimeUnix
  68. // Create reusable EncMode interface with immutable options, safe for concurrent use.
  69. em, err := opts.EncMode()
  70. // Use EncMode like encoding/json, with same function signatures.
  71. b, err := em.Marshal(v)
  72. // or
  73. encoder := em.NewEncoder(w)
  74. err := encoder.Encode(v)
  75. // NOTE: Both em.Marshal(v) and encoder.Encode(v) use encoding options
  76. // specified during creation of em (encoding mode).
  77. # CBOR Options
  78. Predefined Encoding Options: https://github.com/fxamacker/cbor#predefined-encoding-options
  79. Encoding Options: https://github.com/fxamacker/cbor#encoding-options
  80. Decoding Options: https://github.com/fxamacker/cbor#decoding-options
  81. # Struct Tags
  82. Struct tags like `cbor:"name,omitempty"` and `json:"name,omitempty"` work as expected.
  83. If both struct tags are specified then `cbor` is used.
  84. Struct tag options like "keyasint", "toarray", "omitempty", and "omitzero" make it easy to use
  85. very compact formats like COSE and CWT (CBOR Web Tokens) with structs.
  86. The "omitzero" option omits zero values from encoding, matching
  87. [stdlib encoding/json behavior](https://pkg.go.dev/encoding/json#Marshal).
  88. When specified in the `cbor` tag, the option is always honored.
  89. When specified in the `json` tag, the option is honored when building with Go 1.24+.
  90. For example, "toarray" makes struct fields encode to array elements. And "keyasint"
  91. makes struct fields encode to elements of CBOR map with int keys.
  92. https://raw.githubusercontent.com/fxamacker/images/master/cbor/v2.0.0/cbor_easy_api.png
  93. Struct tag options are listed at https://github.com/fxamacker/cbor#struct-tags-1
  94. # Tests and Fuzzing
  95. Over 375 tests are included in this package. Cover-guided fuzzing is handled by
  96. a private fuzzer that replaced fxamacker/cbor-fuzz years ago.
  97. */
  98. package cbor