File: doc.go

package info (click to toggle)
golang-github-fxamacker-cbor 2.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 1,716 kB
  • sloc: makefile: 2
file content (152 lines) | stat: -rw-r--r-- 5,261 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) Faye Amacker. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

/*
Package cbor is a modern CBOR codec (RFC 8949 & RFC 8742) with CBOR tags,
Go struct tag options (toarray/keyasint/omitempty/omitzero), Core Deterministic Encoding,
CTAP2, Canonical CBOR, float64->32->16, and duplicate map key detection.

Encoding options allow "preferred serialization" by encoding integers and floats
to their smallest forms (e.g. float16) when values fit.

Struct tag options "keyasint", "toarray", "omitempty", and "omitzero" reduce encoding size
and reduce programming effort.

For example, "toarray" tag makes struct fields encode to CBOR array elements.  And
"keyasint" makes a field encode to an element of CBOR map with specified int key.

Latest docs can be viewed at https://github.com/fxamacker/cbor#cbor-library-in-go

# Basics

The Quick Start guide is at https://github.com/fxamacker/cbor#quick-start

Function signatures identical to encoding/json include:

	Marshal, Unmarshal, NewEncoder, NewDecoder, (*Encoder).Encode, (*Decoder).Decode

Standard interfaces include:

	BinaryMarshaler, BinaryUnmarshaler, Marshaler, and Unmarshaler

Diagnostic functions translate CBOR data item into Diagnostic Notation:

	Diagnose, DiagnoseFirst

Functions that simplify using CBOR Sequences (RFC 8742) include:

	UnmarshalFirst

Custom encoding and decoding is possible by implementing standard interfaces for
user-defined Go types.

Codec functions are available at package-level (using defaults options) or by
creating modes from options at runtime.

"Mode" in this API means definite way of encoding (EncMode) or decoding (DecMode).

EncMode and DecMode interfaces are created from EncOptions or DecOptions structs.

	em, err := cbor.EncOptions{...}.EncMode()
	em, err := cbor.CanonicalEncOptions().EncMode()
	em, err := cbor.CTAP2EncOptions().EncMode()

Modes use immutable options to avoid side-effects and simplify concurrency. Behavior of
modes won't accidentally change at runtime after they're created.

Modes are intended to be reused and are safe for concurrent use.

EncMode and DecMode Interfaces

	// EncMode interface uses immutable options and is safe for concurrent use.
	type EncMode interface {
		Marshal(v interface{}) ([]byte, error)
		NewEncoder(w io.Writer) *Encoder
		EncOptions() EncOptions  // returns copy of options
	}

	// DecMode interface uses immutable options and is safe for concurrent use.
	type DecMode interface {
		Unmarshal(data []byte, v interface{}) error
		NewDecoder(r io.Reader) *Decoder
		DecOptions() DecOptions  // returns copy of options
	}

Using Default Encoding Mode

	b, err := cbor.Marshal(v)

	encoder := cbor.NewEncoder(w)
	err = encoder.Encode(v)

Using Default Decoding Mode

	err := cbor.Unmarshal(b, &v)

	decoder := cbor.NewDecoder(r)
	err = decoder.Decode(&v)

Using Default Mode of UnmarshalFirst to Decode CBOR Sequences

	// Decode the first CBOR data item and return remaining bytes:
	rest, err = cbor.UnmarshalFirst(b, &v)   // decode []byte b to v

Using Extended Diagnostic Notation (EDN) to represent CBOR data

	// Translate the first CBOR data item into text and return remaining bytes.
	text, rest, err = cbor.DiagnoseFirst(b)  // decode []byte b to text

Creating and Using Encoding Modes

	// Create EncOptions using either struct literal or a function.
	opts := cbor.CanonicalEncOptions()

	// If needed, modify encoding options
	opts.Time = cbor.TimeUnix

	// Create reusable EncMode interface with immutable options, safe for concurrent use.
	em, err := opts.EncMode()

	// Use EncMode like encoding/json, with same function signatures.
	b, err := em.Marshal(v)
	// or
	encoder := em.NewEncoder(w)
	err := encoder.Encode(v)

	// NOTE: Both em.Marshal(v) and encoder.Encode(v) use encoding options
	// specified during creation of em (encoding mode).

# CBOR Options

Predefined Encoding Options: https://github.com/fxamacker/cbor#predefined-encoding-options

Encoding Options: https://github.com/fxamacker/cbor#encoding-options

Decoding Options: https://github.com/fxamacker/cbor#decoding-options

# Struct Tags

Struct tags like `cbor:"name,omitempty"` and `json:"name,omitempty"` work as expected.
If both struct tags are specified then `cbor` is used.

Struct tag options like "keyasint", "toarray", "omitempty", and "omitzero" make it easy to use
very compact formats like COSE and CWT (CBOR Web Tokens) with structs.

The "omitzero" option omits zero values from encoding, matching
[stdlib encoding/json behavior](https://pkg.go.dev/encoding/json#Marshal).
When specified in the `cbor` tag, the option is always honored.
When specified in the `json` tag, the option is honored when building with Go 1.24+.

For example, "toarray" makes struct fields encode to array elements.  And "keyasint"
makes struct fields encode to elements of CBOR map with int keys.

https://raw.githubusercontent.com/fxamacker/images/master/cbor/v2.0.0/cbor_easy_api.png

Struct tag options are listed at https://github.com/fxamacker/cbor#struct-tags-1

# Tests and Fuzzing

Over 375 tests are included in this package. Cover-guided fuzzing is handled by
a private fuzzer that replaced fxamacker/cbor-fuzz years ago.
*/
package cbor