You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CBOR is a [trusted alternative](https://www.rfc-editor.org/rfc/rfc8949.html#name-comparison-of-other-binary-) to JSON, MessagePack, Protocol Buffers, etc. CBOR is an Internet Standard defined by [IETF STD 94 (RFC 8949)](https://www.rfc-editor.org/info/std94) and is designed to be relevant for decades.
6
6
7
-
`fxamacker/cbor` is used in projects by Arm Ltd., Cisco, EdgeX Foundry, Flow Foundation, Fraunhofer‑AISEC, Kubernetes, Let's Encrypt (ISRG), Linux Foundation, Microsoft, Mozilla, Oasis Protocol, Tailscale, Teleport, [etc](https://github.com/fxamacker/cbor#who-uses-fxamackercbor).
7
+
`fxamacker/cbor` is used in projects by Arm Ltd., EdgeX Foundry, Flow Foundation, Fraunhofer‑AISEC, IBM, Kubernetes[*](https://github.com/search?q=org%3Akubernetes%20fxamacker%2Fcbor&type=code), Let's Encrypt, Linux Foundation, Microsoft, Oasis Protocol, Red Hat[*](https://github.com/search?q=org%3Aopenshift+fxamacker%2Fcbor&type=code), Tailscale[*](https://github.com/search?q=org%3Atailscale+fxamacker%2Fcbor&type=code), Veraison[*](https://github.com/search?q=org%3Averaison+fxamacker%2Fcbor&type=code), [etc](https://github.com/fxamacker/cbor#who-uses-fxamackercbor).
8
8
9
9
See [Quick Start](#quick-start) and [Releases](https://github.com/fxamacker/cbor/releases/). 🆕 `UnmarshalFirst` and `DiagnoseFirst` can decode CBOR Sequences. `MarshalToBuffer` and `UserBufferEncMode` accepts user-specified buffer.
10
10
@@ -39,7 +39,7 @@ Codec passed multiple confidential security assessments in 2022. No vulnerabili
39
39
40
40
__🗜️ Data Size__
41
41
42
-
Struct tag options (`toarray`, `keyasint`, `omitempty`, `omitzero`) automatically reduce size of encoded structs. Encoding optionally shrinks float64→32→16 when values fit.
42
+
Struct tag options (`toarray`, `keyasint`, `omitempty`, `omitzero`) and field tag "-" automatically reduce size of encoded structs. Encoding optionally shrinks float64→32→16 when values fit.
43
43
44
44
__:jigsaw: Usability__
45
45
@@ -146,8 +146,12 @@ Struct tags automatically reduce encoded size of structs and improve speed.
146
146
We can write less code by using struct tag options:
147
147
- `toarray`: encode without field names (decode back to original struct)
148
148
- `keyasint`: encode field names as integers (decode back to original struct)
149
-
- `omitempty`: omit empty fields when encoding
150
-
- `omitzero`: omit zero-value fields when encoding
149
+
- `omitempty`: omit empty field when encoding
150
+
- `omitzero`: omit zero-value field when encoding
151
+
152
+
As a special case, struct field tag "-" omits the field.
153
+
154
+
NOTE: When a struct uses `toarray`, the encoder will ignore `omitempty` and `omitzero` to prevent position of encoded array elements from changing. This allows decoder to match encoded elements to their Go struct field.
151
155
152
156

153
157
@@ -353,6 +357,60 @@ err = em.MarshalToBuffer(v, &buf) // encode v to provided buf
353
357
354
358
Struct tag options (`toarray`, `keyasint`, `omitempty`, `omitzero`) reduce encoded size of structs.
355
359
360
+
As a special case, struct field tag "-" omits the field.
361
+
362
+
<details><summary> 🔎 Example encoding with struct field tag "-"</summary><p/>
363
+
364
+
https://go.dev/play/p/aWEIFxd7InX
365
+
366
+
```Go
367
+
// https://github.com/fxamacker/cbor/issues/652
368
+
package main
369
+
370
+
import (
371
+
"encoding/json"
372
+
"fmt"
373
+
374
+
"github.com/fxamacker/cbor/v2"
375
+
)
376
+
377
+
// The `cbor:"-"` tag omits the Type field when encoding to CBOR.
👉 `fxamacker/cbor` allows user apps to use almost any current or future CBOR tag number by implementing `cbor.Marshaler` and `cbor.Unmarshaler` interfaces.
538
+
539
+
Basically, `MarshalCBOR` and `UnmarshalCBOR` functions can be implemented by user apps and those functions will automatically be called by this CBOR codec's `Marshal`, `Unmarshal`, etc.
540
+
541
+
The following [example](https://github.com/fxamacker/cbor/blob/master/example_embedded_json_tag_for_cbor_test.go) shows how to encode and decode a tagged CBOR data item with tag number 262. The tag content is a JSON object "embedded" as a CBOR byte string (major type 2).
542
+
543
+
<details><summary> 🔎 Example using Embedded JSON Tag for CBOR (tag 262)</summary>
544
+
545
+
```go
546
+
// https://github.com/fxamacker/cbor/issues/657
547
+
548
+
package cbor_test
549
+
550
+
// NOTE: RFC 8949 does not mention tag number 262. IANA assigned
551
+
// CBOR tag number 262 as "Embedded JSON Object" specified by the
552
+
// document Embedded JSON Tag for CBOR:
553
+
//
554
+
// "Tag 262 can be applied to a byte string (major type 2) to indicate
555
+
// that the byte string is a JSON Object. The length of the byte string
556
+
// indicates the content."
557
+
//
558
+
// For more info, see Embedded JSON Tag for CBOR at:
<details><summary> 🔎 Functions and interfaces at a glance</summary><p/>
@@ -492,7 +683,7 @@ because RFC 8949 treats CBOR data item with remaining bytes as malformed.
492
683
Other useful functions:
493
684
-`Diagnose`, `DiagnoseFirst` produce human-readable [Extended Diagnostic Notation](https://www.rfc-editor.org/rfc/rfc8610.html#appendix-G) from CBOR data.
494
685
-`UnmarshalFirst` decodes first CBOR data item and return any remaining bytes.
495
-
-`Wellformed` returns true if the the CBOR data item is well-formed.
686
+
-`Wellformed` returns true if the CBOR data item is well-formed.
496
687
497
688
Interfaces identical or comparable to Go `encoding` packages include:
498
689
`Marshaler`, `Unmarshaler`, `BinaryMarshaler`, and `BinaryUnmarshaler`.
@@ -511,28 +702,28 @@ Default limits may need to be increased for systems handling very large data (e.
511
702
512
703
## Status
513
704
514
-
v2.8.0 (March 30, 2025) is a small release primarily to add `omitzero` option to struct field tags and fix bugs. It passed fuzz tests (billions of executions) and is production quality.
515
-
516
-
v2.8.0 and v2.7.1 fixes these 3 functions (when called directly by user apps) to use same error handling on bad inputs as `cbor.Unmarshal()`:
517
-
-`ByteString.UnmarshalCBOR()`
518
-
-`RawTag.UnmarshalCBOR()`
519
-
-`SimpleValue.UnmarshalCBOR()`
705
+
[v2.9.0](https://github.com/fxamacker/cbor/releases/tag/v2.9.0) (Jul 13, 2025) improved interoperability/transcoding between CBOR & JSON, refactored tests, and improved docs.
706
+
- Add opt-in support for `encoding.TextMarshaler` and `encoding.TextUnmarshaler` to encode and decode from CBOR text string.
707
+
- Add opt-in support for `json.Marshaler` and `json.Unmarshaler` via user-provided transcoding function.
708
+
- Update docs for TimeMode, Tag, RawTag, and add example for Embedded JSON Tag for CBOR.
520
709
521
-
The above 3 `UnmarshalCBOR()` functions were initially created for internal use and are deprecated now, so please use `Unmarshal()` or `UnmarshalFirst()` instead. To preserve backward compatibility, these deprecated functions were added to fuzz tests and will not be removed in v2.
710
+
v2.9.0 passed fuzz tests and is production quality.
522
711
523
712
The minimum version of Go required to build:
524
-
- v2.8.0 requires go 1.20.
525
-
- v2.7.1 and older releases require go 1.17.
713
+
- v2.8.0 and newer releases require go 1.20+.
714
+
- v2.7.1 and older releases require go 1.17+.
526
715
527
716
For more details, see [release notes](https://github.com/fxamacker/cbor/releases).
528
717
529
718
### Prior Releases
530
719
531
-
v2.7.0 (June 23, 2024) adds features and improvements that help large projects (e.g. Kubernetes) use CBOR as an alternative to JSON and Protocol Buffers. Other improvements include speedups, improved memory use, bug fixes, new serialization options, etc. It passed fuzz tests (5+ billion executions) and is production quality.
720
+
[v2.8.0](https://github.com/fxamacker/cbor/releases/tag/v2.8.0) (March 30, 2025) is a small release primarily to add `omitzero` option to struct field tags and fix bugs. It passed fuzz tests (billions of executions) and is production quality.
721
+
722
+
[v2.7.0](https://github.com/fxamacker/cbor/releases/tag/v2.7.0) (June 23, 2024) adds features and improvements that help large projects (e.g. Kubernetes) use CBOR as an alternative to JSON and Protocol Buffers. Other improvements include speedups, improved memory use, bug fixes, new serialization options, etc. It passed fuzz tests (5+ billion executions) and is production quality.
532
723
533
724
[v2.6.0](https://github.com/fxamacker/cbor/releases/tag/v2.6.0) (February 2024) adds important new features, optimizations, and bug fixes. It is especially useful to systems that need to convert data between CBOR and JSON. New options and optimizations improve handling of bignum, integers, maps, and strings.
534
725
535
-
v2.5.0 was released on Sunday, August 13, 2023 with new features and important bug fixes. It is fuzz tested and production quality after extended beta [v2.5.0-beta](https://github.com/fxamacker/cbor/releases/tag/v2.5.0-beta) (Dec 2022) -> [v2.5.0](https://github.com/fxamacker/cbor/releases/tag/v2.5.0) (Aug 2023).
726
+
[v2.5.0](https://github.com/fxamacker/cbor/releases/tag/v2.5.0) was released on Sunday, August 13, 2023 with new features and important bug fixes. It is fuzz tested and production quality after extended beta [v2.5.0-beta](https://github.com/fxamacker/cbor/releases/tag/v2.5.0-beta) (Dec 2022) -> [v2.5.0](https://github.com/fxamacker/cbor/releases/tag/v2.5.0) (Aug 2023).
536
727
537
728
__IMPORTANT__: 👉 Before upgrading from v2.4 or older release, please read the notable changes highlighted in the release notes. v2.5.0 is a large release with bug fixes to error handling for extraneous data in `Unmarshal`, etc. that should be reviewed before upgrading.
538
729
@@ -601,9 +792,9 @@ geomean 2.782
601
792
602
793
## Who uses fxamacker/cbor
603
794
604
-
`fxamacker/cbor` is used in projects by Arm Ltd., Berlin Institute of Health at Charité, Chainlink, Cisco, Confidential Computing Consortium, ConsenSys, EdgeX Foundry, F5, Flow Foundation, Fraunhofer‑AISEC, IBM, Kubernetes, Let's Encrypt (ISRG), Linux Foundation, Matrix.org, Microsoft, Mozilla, National Cybersecurity Agency of France (govt), Netherlands (govt), Oasis Protocol, Smallstep, Tailscale, Taurus SA, Teleport, TIBCO, and others.
795
+
`fxamacker/cbor` is used in projects by Arm Ltd., Berlin Institute of Health at Charité, Chainlink, Confidential Computing Consortium, ConsenSys, EdgeX Foundry, F5, Flow Foundation, Fraunhofer‑AISEC, IBM, Kubernetes, Let's Encrypt (ISRG), Linaro, Linux Foundation, Matrix.org, Microsoft, National Cybersecurity Agency of France (govt), Netherlands (govt), Oasis Protocol, Red Hat OpenShift, Smallstep, Tailscale, Taurus SA, TIBCO, Veraison, and others.
605
796
606
-
`fxamacker/cbor` passed multiple confidential security assessments. A [nonconfidential security assessment](https://github.com/veraison/go-cose/blob/v1.0.0-rc.1/reports/NCC_Microsoft-go-cose-Report_2022-05-26_v1.0.pdf) (prepared by NCC Group for Microsoft Corporation) includes a subset of fxamacker/cbor v2.4.0 in its scope.
797
+
`fxamacker/cbor` passed multiple confidential security assessments in 2022. A [nonconfidential security assessment](https://github.com/veraison/go-cose/blob/v1.0.0-rc.1/reports/NCC_Microsoft-go-cose-Report_2022-05-26_v1.0.pdf) (prepared by NCC Group for Microsoft Corporation) assessed a subset of fxamacker/cbor v2.4.
0 commit comments