Documentation ¶
Index ¶
- Variables
- func Marshal(v any) ([]byte, error)
- func Unmarshal(data []byte, v any) error
- func WellFormed(data []byte) bool
- type Base64String
- type Base64URLString
- type CBORMarshaler
- type Decoder
- type EncodedData
- type Encoder
- type ExpectedBase16
- type ExpectedBase64
- type ExpectedBase64URL
- type Integer
- type InvalidUnmarshalError
- type Options
- type RawMessage
- type RawTag
- type SemanticError
- type Simple
- type SyntaxError
- type Tag
- type TagNumber
- type UnmarshalTypeError
- type Unmarshaler
- type UnsupportedTypeError
- type UnsupportedValueError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrUnexpectedEnd = errors.New("cbor: unexpected end")
ErrUnexpectedEnd is returned when the CBOR data ends abruptly.
var Undefined undefined = nil
Functions ¶
func Unmarshal ¶
Unmarshal parses the CBOR-encoded data and stores the result in the value pointed to by v.
func WellFormed ¶
WellFormed reports whether data is a valid CBOR encoding.
Types ¶
type Base64String ¶
type Base64String string
Base64String is a base64 with padding encoded data. CBOR tags that has tag number 34 is converted to this type. The decoder and the encoder validate that it is a valid base64-encoded string. See RFC 8949 Section 3.4.5.3.
type Base64URLString ¶
type Base64URLString string
Base64URLString is a base64url with no padding encoded string. CBOR tags that has tag number 33 is converted to this type. The decoder and the encoder validate that it is a valid base64url-encoded string. See RFC 8949 Section 3.4.5.3.
type CBORMarshaler ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder reads and decodes CBOR values from an input stream.
func NewDecoder ¶
NewDecoder returns a new decoder that reads from r.
func (*Decoder) Decode ¶
Decode reads the next CBOR-encoded value from its input and stores it in the value pointed to by v.
func (*Decoder) UseAnyKey ¶
func (dec *Decoder) UseAnyKey()
UseAnyKey allows decoding maps to map[any]any instead of map[string]any.
func (*Decoder) UseInteger ¶
func (dec *Decoder) UseInteger()
UseInteger allows decoding integers to Integer instead of int64.
type EncodedData ¶
type EncodedData []byte
EncodedData is a CBOR encoded data. CBOR tags that has tag number 24 is converted to this type. See RFC 8949 Section 3.4.5.1.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
An Encoder writes CBOR to an output stream.
func NewEncoder ¶
NewEncoder returns a new encoder that writes to w.
type ExpectedBase16 ¶
type ExpectedBase16 struct {
Content any
}
ExpectedBase16 is data expected to be encoded as base16-encoding (as known as hex-encoding). CBOR tags that has tag number 23 is converted to this type. When encoded to JSON, []byte in Content is converted to a base16-encoded string. See RFC 8949 Section 3.4.5.2.
Example ¶
data, _ := json.Marshal(cbor.ExpectedBase16{ Content: []byte("🍣"), }) fmt.Println(string(data))
Output: "f09f8da3"
func (ExpectedBase16) MarshalJSON ¶
func (e ExpectedBase16) MarshalJSON() ([]byte, error)
type ExpectedBase64 ¶
type ExpectedBase64 struct {
Content any
}
ExpectedBase64 is data expected to be encoded as base64-encoding. CBOR tags that has tag number 22 is converted to this type. When encoded to JSON, []byte in Content is converted to a base64-encoded string. See RFC 8949 Section 3.4.5.2.
Example ¶
data, _ := json.Marshal(cbor.ExpectedBase64{ Content: []byte("🍣"), }) fmt.Println(string(data))
Output: "8J+Now=="
func (ExpectedBase64) MarshalJSON ¶
func (e ExpectedBase64) MarshalJSON() ([]byte, error)
type ExpectedBase64URL ¶
type ExpectedBase64URL struct {
Content any
}
ExpectedBase64URL is data expected to be encoded as base64url-encoding. CBOR tags that has tag number 21 is converted to this type. When encoded to JSON, []byte in Content is converted to a base64url-encoded string. See RFC 8949 Section 3.4.5.2.
Example ¶
data, _ := json.Marshal(cbor.ExpectedBase64URL{ Content: []byte("🍣"), }) fmt.Println(string(data))
Output: "8J-Now"
func (ExpectedBase64URL) MarshalJSON ¶
func (e ExpectedBase64URL) MarshalJSON() ([]byte, error)
type Integer ¶
type Integer struct { // Sign is true if the integer is negative. Sign bool // Value presents the value of the integer. // If the integer is positive, the value is Value itself. // If the integer is negative, the value is -Value-1. Value uint64 }
Integer is a CBOR integer type.
func (Integer) MarshalJSON ¶
func (*Integer) UnmarshalJSON ¶
type InvalidUnmarshalError ¶
An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. (The argument to Unmarshal must be a non-nil pointer.)
func (*InvalidUnmarshalError) Error ¶
func (e *InvalidUnmarshalError) Error() string
type Options ¶
type RawMessage ¶
type RawMessage []byte
RawMessage is a raw encoded CBOR value. It implements Marshaler and Unmarshaler and can be used to delay CBOR decoding or precompute a CBOR encoding. nil RawMessage encodes as the CBOR undefined value.
func DecodeEDN ¶ added in v0.2.0
func DecodeEDN(data []byte) (RawMessage, error)
DecodeEDN parses the Extended Diagnostic Notation encoded data and returns the result.
func (RawMessage) EncodeEDN ¶ added in v0.2.0
func (m RawMessage) EncodeEDN() ([]byte, error)
EncodeEDN returns the Extended Diagnostic Notation encoding of msg.
func (RawMessage) MarshalCBOR ¶
func (m RawMessage) MarshalCBOR() ([]byte, error)
MarshalCBOR returns m as the CBOR encoding of m.
func (*RawMessage) UnmarshalCBOR ¶
func (m *RawMessage) UnmarshalCBOR(data []byte) error
UnmarshalCBOR sets *m to a copy of data.
type RawTag ¶
type RawTag struct { Number TagNumber Content RawMessage }
type SemanticError ¶
type SemanticError struct {
// contains filtered or unexported fields
}
SemanticError is a description of a CBOR semantic error.
func (*SemanticError) Error ¶
func (e *SemanticError) Error() string
func (*SemanticError) Unwrap ¶
func (e *SemanticError) Unwrap() error
type SyntaxError ¶
type SyntaxError struct { Offset int64 // error occurred after reading Offset bytes // contains filtered or unexported fields }
A SyntaxError is a description of a CBOR syntax error. Unmarshal will return a SyntaxError if the CBOR can't be parsed.
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string
type Tag ¶
Tag is a CBOR tag.
func (Tag) Decode ¶
Decode decodes the tag content. The following tags are supported:
- tag number 0: date/time string is decoded as time.Time.
- tag number 1: epoch-based date/time is decoded as time.Time.
- tag number 2: positive bignum is decoded as *big.Int.
- tag number 3: negative bignum is decoded as *big.Int.
- tag number 4: decimal fraction is not implemented.
- tag number 5: bigfloat is decoded as *big.Float.
- tag number 21: expected conversion to base64url is decoded as ExpectedBase64URL.
- tag number 22: expected conversion to base64 is decoded as ExpectedBase64.
- tag number 23: expected conversion to base16 is decoded as ExpectedBase16.
- tag number 24: encoded CBOR data item is decoded as EncodedData.
- tag number 32: URI is decoded as *url.URL.
- tag number 33: base64url is decoded as Base64URLString.
- tag number 34: base64 is decoded as Base64String.
- tag number 55799: Self-Described CBOR return the content as is.
Other tags returns tag itself.
type UnmarshalTypeError ¶
type UnmarshalTypeError struct { Value string // description of CBOR value - "bool", "array", "number -5" Type reflect.Type // type of Go value it could not be assigned to Offset int64 // error occurred after reading Offset bytes Struct string // name of the struct type containing the field Field string // the full path from root node to the field }
An UnmarshalTypeError describes a CBOR value that was not appropriate for a value of a specific Go type.
func (*UnmarshalTypeError) Error ¶
func (e *UnmarshalTypeError) Error() string
type Unmarshaler ¶
Unmarshaler is the interface implemented by types that can unmarshal a CBOR description of themselves. The input can be assumed to be a valid encoding of a CBOR value. UnmarshalCBOR must copy the CBOR data if it wishes to retain the data after returning.
type UnsupportedTypeError ¶
An UnsupportedTypeError is returned by Marshal when attempting to encode an unsupported value type.
func (*UnsupportedTypeError) Error ¶
func (e *UnsupportedTypeError) Error() string
type UnsupportedValueError ¶
An UnsupportedValueError is returned by Marshal when attempting to encode an unsupported value.
func (*UnsupportedValueError) Error ¶
func (e *UnsupportedValueError) Error() string