Documentation
¶
Overview ¶
Package cbor implements encoding and decoding of CBOR (Concise Binary Object Representation, RFC 8949 / STD 94) with an API that mirrors encoding/json.
Quick start ¶
b, _ := cbor.Marshal(map[string]int{"a": 1}) // deterministic CBOR bytes
var v map[string]int
_ = cbor.Unmarshal(b, &v)
Marshal produces deterministic (canonical) CBOR by default, per RFC 8949 §4.2.1: shortest-form integers and floats, and map keys sorted by the bytewise lexicographic order of their encodings. COSE, CWT and ISO mdoc all require this, so nothing needs configuring for signing or hashing.
Struct tags ¶
Fields use cbor:"..." tags, like encoding/json, with CBOR-specific extras:
type Header struct {
Alg int `cbor:"1,asint"` // integer map key (e.g. COSE labels)
Kid []byte `cbor:"4,asint,omitempty"`
}
type Sign1 struct {
_ struct{} `cbor:",toarray"` // encode as an array, not a map
Protected []byte
Payload []byte
Signature []byte
}
Recognised options: a name (or an integer with asint), "-" to skip a field, "omitempty", "omitzero", and ",toarray" on a blank _ field to encode the whole struct as a positional array.
The API shape mirrors encoding/json, but the semantics are CBOR's, not JSON's. In particular: only cbor:"..." tags are read (json:"..." tags, including json:"-", are ignored); untagged fields use the Go field name; and field-name matching on decode is case-sensitive (unlike encoding/json). Embedded (anonymous) struct fields follow encoding/json's rules — an untagged embedded struct's fields are promoted to the parent, an embedded field with a tag is nested under that name, and name conflicts resolve by the same shallowest-wins, tagged-beats-untagged, ties-dropped rules. Prefer omitzero over omitempty for time.Time and other struct types: omitempty tests a zero value and never omits a struct, so a zero time.Time is emitted as its year-1 sentinel rather than dropped.
Type mapping ¶
Marshal: bool→true/false, nil→null, int/uint→major 0/1, float32/64→shortest float, string→text string, []byte→byte string, slice/array→array, map/struct→map (or array with toarray), *big.Int→integer or bignum tag, time.Time→tag 1 epoch (or tag 0 text, or a bare untagged epoch, via TimeMode). A nil slice, nil map, or nil pointer encodes to null. Types implementing Marshaler encode themselves.
Unmarshal into a concrete Go type is the reverse. Unmarshal into an any yields native values: int64 (or uint64 / *big.Int at the 64-bit extremes; bignum tags 2 and 3 also yield *big.Int), float64, string, []byte, bool, nil, []any, Map (an ordered key/value list), Tag, SimpleValue and Undefined. Unmarshal into a *DataItem yields the loss-free tree; into a *RawMessage captures the item's exact bytes undecoded; into a *RawTag captures a tag's content verbatim; into a *Map yields the ordered map directly. A bare (untagged) number also decodes into a time.Time (RFC 8392 / RFC 7519 NumericDate).
Options ¶
Encoding and decoding are configured through immutable, reusable, goroutine- safe modes:
em, _ := cbor.EncoderOptions{Sort: cbor.SortLengthFirst}.Encoding()
b, _ := em.Marshal(v)
dm, _ := cbor.UntrustedDecoderOptions().Decoding() // safe limits for untrusted input
err := dm.Unmarshal(data, &v)
See EncoderOptions (sort order, float/NaN/bignum form, time format) and DecoderOptions (nesting depth, duplicate keys, element caps, strict canonical validation, indefinite-length rejection).
Streaming ¶
NewEncoder and NewDecoder write and read a sequence of items; Decoder.Decode returns io.EOF at the end of the stream.
Inspecting ¶
Diagnostic renders bytes in CBOR diagnostic notation (RFC 8949 §8), and DataItem.MarshalJSON gives a JSON view for debugging.
Index ¶
- func Diagnostic(data []byte) (string, error)
- func Marshal(v any) ([]byte, error)
- func Unmarshal(data []byte, v any) error
- func Valid(data []byte) error
- type BigIntMode
- type DataItem
- func ArrayOf(items ...*DataItem) *DataItem
- func Bool(b bool) *DataItem
- func ByteString(b []byte) *DataItem
- func Float(f float64) *DataItem
- func Int(i int64) *DataItem
- func MapOf(pairs ...*DataItem) *DataItem
- func Nint(n uint64) *DataItem
- func Null() *DataItem
- func Simple(v byte) *DataItem
- func TagOf(number uint64, content *DataItem) *DataItem
- func Text(s string) *DataItem
- func Uint(n uint64) *DataItem
- type Decoder
- type DecoderOptions
- type Decoding
- type DupMode
- type EncodedCBOR
- type Encoder
- type EncoderOptions
- type Encoding
- type FloatMode
- type InvalidUnmarshalError
- type MajorType
- type Map
- func (m Map) Get(key any) (any, bool)
- func (m Map) GetBool(key any) (bool, bool)
- func (m Map) GetBytes(key any) ([]byte, bool)
- func (m Map) GetFloat(key any) (float64, bool)
- func (m Map) GetInt(key any) (int64, bool)
- func (m Map) GetMap(key any) (Map, bool)
- func (m Map) GetSlice(key any) ([]any, bool)
- func (m Map) GetString(key any) (string, bool)
- func (m Map) GetTag(key any) (Tag, bool)
- func (m Map) GetUint(key any) (uint64, bool)
- func (m Map) ToStringMap() (map[string]any, error)
- type MapEntry
- type Marshaler
- type NaNMode
- type RawMessage
- type RawTag
- type SimpleValue
- type SortMode
- type SyntaxError
- type Tag
- type TimeMode
- type Undefined
- type UnmarshalTypeError
- type Unmarshaler
- type UnsupportedTypeError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Diagnostic ¶
Diagnostic renders one CBOR item as CBOR diagnostic notation (RFC 8949 §8): e.g. [1, 2, 3], {1: 2}, h'01ff', "text", 0("2013-…"), true, NaN. Unlike the JSON view it distinguishes byte strings from text strings and shows tag numbers.
Example ¶
package main
import (
"fmt"
cbor "github.com/MichaelFraser99/go-cbor"
)
func main() {
s, _ := cbor.Diagnostic([]byte{0xa1, 0x01, 0x83, 0x01, 0x02, 0x03})
fmt.Println(s)
}
Output: {1: [1, 2, 3]}
func Marshal ¶
Marshal returns the deterministic (canonical, RFC 8949 §4.2.1) CBOR encoding of v. See the package overview for the Go-to-CBOR type mapping and struct tags, and EncoderOptions for non-default encoding.
Example ¶
package main
import (
"fmt"
cbor "github.com/MichaelFraser99/go-cbor"
)
func main() {
b, _ := cbor.Marshal(map[string]int{"a": 1, "b": 2})
fmt.Printf("%x\n", b)
}
Output: a2616101616202
Example (StructTags) ¶
package main
import (
"fmt"
cbor "github.com/MichaelFraser99/go-cbor"
)
func main() {
type Header struct {
Alg int `cbor:"1,asint"`
Kid []byte `cbor:"4,asint,omitempty"`
}
b, _ := cbor.Marshal(Header{Alg: -7}) // {1: -7}, Kid omitted
fmt.Printf("%x\n", b)
}
Output: a10126
Example (Toarray) ¶
package main
import (
"fmt"
cbor "github.com/MichaelFraser99/go-cbor"
)
func main() {
type Pair struct {
_ struct{} `cbor:",toarray"`
A int
B int
}
b, _ := cbor.Marshal(Pair{A: 1, B: 2}) // encodes as [1, 2]
fmt.Printf("%x\n", b)
}
Output: 820102
func Unmarshal ¶
Unmarshal parses one CBOR item from data and stores it in the value pointed to by v, which must be a non-nil pointer. v may be a pointer to a concrete Go type, to an any (yielding native values, with maps as a Map), to a DataItem (the loss-free tree), or to a RawMessage (the item's exact bytes). It is an error for data to contain trailing bytes after the item; use a Decoder for a sequence.
When a tagged item is decoded into a concrete (non-Tag) type, the tag is unwrapped and its number is not checked. Decode into a Tag or an any if the tag's identity is significant — for example the COSE message type carried by tag 18 vs 17.
Example ¶
package main
import (
"fmt"
cbor "github.com/MichaelFraser99/go-cbor"
)
func main() {
data := []byte{0xa2, 0x61, 0x61, 0x01, 0x61, 0x62, 0x02}
var m map[string]int
_ = cbor.Unmarshal(data, &m)
fmt.Println(m["a"], m["b"])
}
Output: 1 2
Types ¶
type BigIntMode ¶
type BigIntMode uint8
BigIntMode controls how *big.Int values are encoded.
const ( // BigIntShortest uses a plain integer when the value fits, else a bignum tag. BigIntShortest BigIntMode = iota // BigIntTag always uses a bignum tag (2 or 3). BigIntTag )
type DataItem ¶
type DataItem struct {
// Major is the item's major type.
Major MajorType
// Argument holds the head argument: the value for MajorUint/MajorNint, the
// tag number for MajorTag, or the simple value for MajorOther when
// FloatWidth is 0.
Argument uint64
// Float and FloatWidth apply when Major is MajorOther and FloatWidth is
// non-zero; FloatWidth is the encoded width in bytes (2, 4, or 8).
Float float64
FloatWidth uint8
// Bytes holds a byte string (MajorBytes) or a text string's UTF-8 bytes
// (MajorText).
Bytes []byte
// Content holds child items: a tag's single item, an array's elements, or a
// map's flattened key, value, key, value… sequence.
Content []*DataItem
}
DataItem is the loss-free, fully-typed representation of one decoded CBOR item. Unmarshal populates it when the target is a *DataItem, and Marshal re-encodes it. Its fields are interpreted by Major (see the MajorType constants). Build one directly or with the constructor helpers (Uint, ArrayOf, TagOf, …), and use Native to convert it to ordinary Go values.
func ArrayOf ¶
ArrayOf builds an array data item from the given elements.
Example ¶
package main
import (
"fmt"
cbor "github.com/MichaelFraser99/go-cbor"
)
func main() {
item := cbor.ArrayOf(cbor.Uint(1), cbor.Text("hi"), cbor.TagOf(0, cbor.Text("t")))
b, _ := cbor.Marshal(item)
fmt.Printf("%x\n", b)
}
Output: 8301626869c06174
func Float ¶
Float builds a double-precision (8-byte) float data item. It does not select the shortest width; set FloatWidth yourself, or encode via the reflect path (a plain float64), for canonical shortest-form floats.
func Nint ¶
Nint builds a negative-integer data item whose value is -1 - n (so Nint(0) is -1 and Nint(6) is -7). For a signed value, prefer Int.
func (DataItem) MarshalJSON ¶
MarshalJSON renders the item as a JSON debugging view of the form {"majorType":N,"data":…}; it is not the CBOR codec. For CBOR diagnostic notation, use Diagnostic. Conventions: byte strings are base64url (no padding); integers outside ±(2^53-1) and bignums are emitted as strings to avoid JSON precision loss; NaN, Infinity, -Infinity and undefined are emitted as the strings "NaN"/"Infinity"/"-Infinity"/"undefined" (JSON has no literal for them), so use the majorType field to disambiguate them from genuine text strings.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder reads a sequence of CBOR items from an io.Reader. It reads only as many bytes as each item requires, so it is suitable for streaming from a connection that stays open between items: Decode returns as soon as one complete item has arrived rather than waiting for the reader to reach EOF.
func NewDecoder ¶
NewDecoder returns a Decoder that reads from r with default decoding. Use DecoderOptions.NewDecoder to configure it.
func (*Decoder) Buffered ¶
Buffered returns a reader over bytes read from the underlying reader but not yet consumed by Decode. Use it to hand a connection to another protocol after reading a known number of CBOR items.
func (*Decoder) Decode ¶
Decode reads the next CBOR item from the stream into v, returning io.EOF once the stream is exhausted. It reads exactly enough bytes to frame one item, so a reader that stays open after the item does not block the call. A truncated final item returns io.ErrUnexpectedEOF. A malformed item is a terminal condition: the error recurs on each subsequent call rather than resyncing into the garbage, so treat a non-EOF error as the end of the stream and stop reading. A Decoder is not safe for concurrent use.
func (*Decoder) InputOffset ¶
InputOffset returns the number of bytes consumed from the stream so far — the position at which the next item begins.
type DecoderOptions ¶
type DecoderOptions struct {
// MaxNestingDepth bounds container nesting; <= 0 uses the default (1024).
MaxNestingDepth int
// DuplicateKeys selects the duplicate-key policy.
DuplicateKeys DupMode
// Strict rejects non-shortest integer/float encodings, non-minimal bignums,
// and unsorted map keys.
Strict bool
// RejectIndefinite rejects indefinite-length items.
RejectIndefinite bool
// MaxArrayLen bounds an array's element count; 0 means unlimited.
MaxArrayLen int
// MaxMapLen bounds a map's key/value pair count; 0 means unlimited.
MaxMapLen int
// MaxStringLength bounds the byte length of any single byte or text string
// (and of a bignum's byte string); 0 means unlimited.
MaxStringLength int
}
DecoderOptions configures a Decoding. The zero value matches the default Unmarshal (a 1024 nesting-depth cap, otherwise permissive). See UntrustedDecoderOptions for a hardened preset.
func CanonicalDecoderOptions ¶
func CanonicalDecoderOptions() DecoderOptions
CanonicalDecoderOptions returns a preset that rejects any input not in RFC 8949 §4.2.1 deterministic (canonical) form: non-shortest integers/floats, non-minimal bignums, unsorted map keys, indefinite-length items, and duplicate map keys. Use it on the verify side of COSE/CWT/mdoc, where accepting a non-canonical re-encoding of signed content is a hazard. Note that Strict alone does NOT reject indefinite-length items, which is why this preset sets RejectIndefinite too. It adds no size caps; combine with UntrustedDecoderOptions (or set the Max* fields) to also bound allocation.
func UntrustedDecoderOptions ¶
func UntrustedDecoderOptions() DecoderOptions
UntrustedDecoderOptions returns a conservative preset for decoding untrusted input: a shallow nesting cap, duplicate-key rejection, element/pair caps, a per-string length cap (1 MiB), and rejection of indefinite-length items (whose streaming form otherwise sidesteps the element and duplicate-key caps). It is not a substitute for bounding the total input size. Adjust the returned value before calling Decoding if the limits don't fit your data — for example, raise MaxStringLength for larger embedded blobs.
Example ¶
package main
import (
"fmt"
cbor "github.com/MichaelFraser99/go-cbor"
)
func main() {
dm, _ := cbor.UntrustedDecoderOptions().Decoding()
err := dm.Valid([]byte{0xa2, 0x01, 0x01, 0x01, 0x02}) // {1:1, 1:2} duplicate key
fmt.Println(err != nil)
}
Output: true
func (DecoderOptions) Decoding ¶
func (o DecoderOptions) Decoding() (Decoding, error)
Decoding builds an immutable Decoding from the options, or an error if any option value is out of range.
func (DecoderOptions) NewDecoder ¶
func (o DecoderOptions) NewDecoder(r io.Reader) (*Decoder, error)
NewDecoder returns a Decoder reading from r with the configured options.
type EncodedCBOR ¶
type EncodedCBOR []byte
EncodedCBOR is an already-encoded CBOR data item carried as "embedded CBOR": a byte string wrapped in tag 24 (RFC 8949 §3.4.5.1). Marshal emits tag 24 around the bytes; Unmarshal expects tag 24 wrapping a byte string and stores a copy of its content. This is the common shape in COSE and ISO mdoc/VICAL (for example IssuerSignedItemBytes and MobileSecurityObjectBytes), where a structure is frozen as opaque bytes so it can be signed or hashed independently of re-encoding. An empty EncodedCBOR marshals to null.
Example ¶
package main
import (
"fmt"
cbor "github.com/MichaelFraser99/go-cbor"
)
func main() {
inner, _ := cbor.Marshal(map[int]int{1: 2})
type Signed struct {
Payload cbor.EncodedCBOR `cbor:"p"` // tag-24-wrapped embedded CBOR
}
b, _ := cbor.Marshal(Signed{Payload: inner})
fmt.Printf("%x\n", b)
var got Signed
_ = cbor.Unmarshal(b, &got)
fmt.Printf("%x\n", []byte(got.Payload))
}
Output: a16170d81843a10102 a10102
func (EncodedCBOR) Decode ¶
func (e EncodedCBOR) Decode(v any) error
Decode unmarshals the embedded item into v. The embedded bytes are captured verbatim and are NOT validated by UnmarshalCBOR, so Decode is where malformed embedded CBOR surfaces. For untrusted input, pass the EncodedCBOR to a configured Decoding.Unmarshal instead, so the decode limits apply to the embedded item too.
func (EncodedCBOR) MarshalCBOR ¶
func (e EncodedCBOR) MarshalCBOR() ([]byte, error)
MarshalCBOR wraps the embedded item in tag 24, or emits null when empty.
func (*EncodedCBOR) UnmarshalCBOR ¶
func (e *EncodedCBOR) UnmarshalCBOR(data []byte) error
UnmarshalCBOR stores a copy of the tag-24 byte string's content, or nil for null. It does not check that the content is well-formed CBOR — the bytes are captured verbatim so they can be signed or hashed as received; use Decode or Valid to check.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder writes a sequence of CBOR items to an io.Writer. It reuses its encoding state and buffer across Encode calls, so a high-rate stream allocates nothing per item beyond what encoding the value itself requires.
func NewEncoder ¶
NewEncoder returns an Encoder that writes to w using the default (canonical) encoding. Use EncoderOptions.NewEncoder to configure it.
Example ¶
package main
import (
"bytes"
"fmt"
"io"
cbor "github.com/MichaelFraser99/go-cbor"
)
func main() {
var buf bytes.Buffer
enc := cbor.NewEncoder(&buf)
_ = enc.Encode(1)
_ = enc.Encode("two")
dec := cbor.NewDecoder(&buf)
for {
var v any
if err := dec.Decode(&v); err == io.EOF {
break
}
fmt.Println(v)
}
}
Output: 1 two
type EncoderOptions ¶
type EncoderOptions struct {
Sort SortMode
Time TimeMode
Float FloatMode
NaN NaNMode
BigInt BigIntMode
}
EncoderOptions configures an Encoding. The zero value is the canonical default used by Marshal.
Example ¶
package main
import (
"fmt"
cbor "github.com/MichaelFraser99/go-cbor"
)
func main() {
em, _ := cbor.EncoderOptions{Sort: cbor.SortLengthFirst}.Encoding()
b, _ := em.Marshal(map[int]int{24: 1, -1: 2}) // shorter key (-1) first
fmt.Printf("%x\n", b)
}
Output: a22002181801
func (EncoderOptions) Encoding ¶
func (o EncoderOptions) Encoding() (Encoding, error)
Encoding builds an immutable Encoding from the options, or an error if any option value is out of range.
func (EncoderOptions) NewEncoder ¶
func (o EncoderOptions) NewEncoder(w io.Writer) (*Encoder, error)
NewEncoder returns an Encoder writing to w with the configured options.
type InvalidUnmarshalError ¶
InvalidUnmarshalError reports an invalid argument to Unmarshal; the argument must be a non-nil pointer.
func (*InvalidUnmarshalError) Error ¶
func (e *InvalidUnmarshalError) Error() string
type MajorType ¶
type MajorType uint8
MajorType is a CBOR major type (0–7), the top three bits of an item's initial byte.
const ( MajorUint MajorType = 0 // unsigned integer MajorNint MajorType = 1 // negative integer (value is -1 - Argument) MajorBytes MajorType = 2 // byte string MajorText MajorType = 3 // UTF-8 text string MajorArray MajorType = 4 // array MajorMap MajorType = 5 // map MajorTag MajorType = 6 // tagged value MajorOther MajorType = 7 // float or simple value )
type Map ¶
type Map []MapEntry
Map is the ordered representation of a CBOR map produced when decoding into an any. It preserves wire order and supports keys of any type (including byte strings, arrays and maps, which a Go map cannot hold). Marshal(Map) re-encodes it as a CBOR map, reporting an error if two entries encode to the same key bytes; Unmarshal accepts a *Map as a decode target. Decode into a Go map[K]V instead when you know the schema and want O(1) lookup.
Example ¶
package main
import (
"fmt"
cbor "github.com/MichaelFraser99/go-cbor"
)
func main() {
var v any
_ = cbor.Unmarshal([]byte{0xa2, 0x01, 0x02, 0x03, 0x04}, &v) // {1:2, 3:4}
m := v.(cbor.Map)
val, _ := m.Get(int64(3))
fmt.Println(val)
}
Output: 4
func (Map) Get ¶
Get returns the value for the first entry whose key deep-equals key. Keys must match by exact Go type: integer keys decode as int64, so use Get(int64(k)), not Get(k) with an untyped constant that defaults to int.
func (Map) GetBool ¶
GetBool returns the value for key as a bool. ok is false if the key is absent or the value is not a bool.
func (Map) GetBytes ¶
GetBytes returns the value for key as a byte string. ok is false if the key is absent or the value is not a byte string.
func (Map) GetFloat ¶
GetFloat returns the value for key as a float64. ok is false if the key is absent or the value is not a float.
func (Map) GetInt ¶
GetInt returns the value for key as an int64, coercing across the integer representations: it succeeds for a value that decoded as int64, as a uint64 within the int64 range, or as a *big.Int that fits in an int64. ok is false if the key is absent, the value is not an integer, or it does not fit in an int64.
func (Map) GetMap ¶
GetMap returns the value for key as a nested Map. ok is false if the key is absent or the value is not a map.
func (Map) GetSlice ¶
GetSlice returns the value for key as a []any. ok is false if the key is absent or the value is not an array.
func (Map) GetString ¶
GetString returns the value for key as a string. ok is false if the key is absent or the value is not a text string.
func (Map) GetTag ¶
GetTag returns the value for key as a Tag. ok is false if the key is absent or the value is not a tag. Note that bignum tags 2 and 3 decode to *big.Int, not Tag.
func (Map) GetUint ¶
GetUint returns the value for key as a uint64, coercing across the integer representations: it succeeds for a value that decoded as uint64, as a non-negative int64, or as a *big.Int that fits in a uint64. ok is false if the key is absent, the value is not an integer, or it is negative or too large.
func (Map) ToStringMap ¶
ToStringMap converts the Map to a map[string]any, returning an error if any key is not a text string. Nested maps remain as Map values (call ToStringMap on them as needed). Use this at a call site when you know the keys are strings and want a plain Go map; note it discards key order and rejects the non-string keys a Map can otherwise hold.
type Marshaler ¶
Marshaler is implemented by types that encode themselves to CBOR. Define MarshalCBOR on a value receiver if instances are ever marshaled by value (including as a field, slice element, or map value): a pointer-receiver method is not in the method set of a non-addressable value, so it is silently skipped and the value is encoded with the default rules instead. MarshalCBOR's bytes are written verbatim and are not validated or re-canonicalised, so returning malformed or non-canonical CBOR breaks the well-formedness or determinism of the surrounding document.
type RawMessage ¶
type RawMessage []byte
RawMessage is a raw encoded CBOR item. It implements Marshaler and Unmarshaler, so a struct field of this type is captured verbatim on decode and re-emitted unchanged on encode — useful for deferred/two-stage decoding and for preserving exact bytes (e.g. a COSE protected header). An empty RawMessage marshals to null.
Example ¶
package main
import (
"fmt"
cbor "github.com/MichaelFraser99/go-cbor"
)
func main() {
type Envelope struct {
Type int `cbor:"t"`
Body cbor.RawMessage `cbor:"b"` // captured verbatim, decoded later
}
data, _ := cbor.Marshal(map[string]any{"t": 1, "b": []int{1, 2, 3}})
var env Envelope
_ = cbor.Unmarshal(data, &env)
fmt.Printf("%x\n", []byte(env.Body))
var body []int
_ = cbor.Unmarshal(env.Body, &body)
fmt.Println(body)
}
Output: 83010203 [1 2 3]
func (RawMessage) MarshalCBOR ¶
func (m RawMessage) MarshalCBOR() ([]byte, error)
MarshalCBOR returns m as the raw CBOR encoding, or null for an empty message.
func (*RawMessage) UnmarshalCBOR ¶
func (m *RawMessage) UnmarshalCBOR(data []byte) error
UnmarshalCBOR stores a copy of the raw CBOR bytes in m.
type RawTag ¶
type RawTag struct {
Number uint64
Content RawMessage
}
RawTag is a tag number together with its content as an undecoded RawMessage. It lets a verifier capture a tag's content byte-for-byte without a schema (e.g. a COSE protected header), avoiding the re-canonicalisation that decoding into Tag would apply. Marshal(RawTag{N, raw}) emits tag N wrapping raw's bytes.
Example ¶
package main
import (
"fmt"
cbor "github.com/MichaelFraser99/go-cbor"
)
func main() {
// A verifier captures a tag's content byte-for-byte, without a schema and
// without the re-canonicalisation that decoding into cbor.Tag would apply.
msg, _ := cbor.Marshal(cbor.Tag{Number: 18, Content: []int{1, 2}})
var rt cbor.RawTag
_ = cbor.Unmarshal(msg, &rt)
fmt.Printf("tag %d, content %x\n", rt.Number, []byte(rt.Content))
}
Output: tag 18, content 820102
type SimpleValue ¶
type SimpleValue byte
SimpleValue is a CBOR simple value (major type 7, values 0–19 and 32–255). The named simples true, false and null map to Go bool and nil instead.
func (SimpleValue) String ¶
func (s SimpleValue) String() string
String renders the simple value as simple(N).
type SortMode ¶
type SortMode uint8
SortMode controls how map (and struct-as-map) keys are ordered when encoding.
const ( // SortBytewise orders keys by the bytewise lexicographic order of their // encodings (RFC 8949 §4.2.1 core deterministic). This is the default. SortBytewise SortMode = iota // SortLengthFirst orders keys by encoded length, then bytewise (RFC 7049 / // CTAP2 canonical). SortLengthFirst // SortNone preserves insertion/declaration order and is non-deterministic // for Go maps. SortNone )
type SyntaxError ¶
type SyntaxError struct {
Offset int64
// contains filtered or unexported fields
}
SyntaxError reports malformed or not-well-formed CBOR input. Offset is the byte position at which the problem was detected.
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string
type Tag ¶
Tag is a CBOR tag number together with its content. Marshal(Tag{N, c}) emits tag N wrapping the encoding of c; decoding a tag into an any yields a Tag, except tags 2 and 3 (bignums), which decode to a *big.Int.
type TimeMode ¶
type TimeMode uint8
TimeMode controls how time.Time values are encoded.
const ( // TimeUnix encodes as tag 1: an integer epoch, or a float when sub-second. TimeUnix TimeMode = iota // TimeRFC3339 encodes as tag 0: an RFC 3339 text string. TimeRFC3339 // TimeNumericDate encodes as a bare (untagged) numeric epoch — an integer, or a // float when sub-second. This is the NumericDate form used by CWT (RFC 8392) and // JWT (RFC 7519), where exp/nbf/iat are untagged numbers. The decoder accepts a // bare number into a time.Time field regardless of this setting. TimeNumericDate )
type Undefined ¶
type Undefined struct{}
Undefined is the decoded form of the CBOR "undefined" simple value (0xf7). Decoding 0xf7 into an any yields Undefined{}, distinct from other simple values so an inspector can tell them apart. Marshal(Undefined{}) emits 0xf7.
type UnmarshalTypeError ¶
UnmarshalTypeError reports a CBOR value that cannot be stored in the target Go type. Offset is the byte position of the value (0 if unknown).
func (*UnmarshalTypeError) Error ¶
func (e *UnmarshalTypeError) Error() string
type Unmarshaler ¶
Unmarshaler is implemented by types that decode themselves from CBOR. The input is exactly one well-formed CBOR item and is only valid for the duration of the call; an implementation that retains it must copy it first (data may alias the caller's buffer).
type UnsupportedTypeError ¶
UnsupportedTypeError reports a Go type that cannot be encoded to CBOR.
func (*UnsupportedTypeError) Error ¶
func (e *UnsupportedTypeError) Error() string