// Marshal returns the CBOR encoding of v using default encoding options. // See EncOptions for encoding options. // // Marshal uses the following encoding rules: // // If value implements the Marshaler interface, Marshal calls its // MarshalCBOR method. // // If value implements encoding.BinaryMarshaler, Marhsal calls its // MarshalBinary method and encode it as CBOR byte string. // // Boolean values encode as CBOR booleans (type 7). // // Positive integer values encode as CBOR positive integers (type 0). // // Negative integer values encode as CBOR negative integers (type 1). // // Floating point values encode as CBOR floating points (type 7). // // String values encode as CBOR text strings (type 3). // // []byte values encode as CBOR byte strings (type 2). // // Array and slice values encode as CBOR arrays (type 4). // // Map values encode as CBOR maps (type 5). // // Struct values encode as CBOR maps (type 5). Each exported struct field // becomes a pair with field name encoded as CBOR text string (type 3) and // field value encoded based on its type. See struct tag option "keyasint" // to encode field name as CBOR integer (type 0 and 1). Also see struct // tag option "toarray" for special field "_" to encode struct values as // CBOR array (type 4). // // Marshal supports format string stored under the "cbor" key in the struct // field's tag. CBOR format string can specify the name of the field, // "omitempty" and "keyasint" options, and special case "-" for field omission. // If "cbor" key is absent, Marshal uses "json" key. // // Struct field name is treated as integer if it has "keyasint" option in // its format string. The format string must specify an integer as its // field name. // // Special struct field "_" is used to specify struct level options, such as // "toarray". "toarray" option enables Go struct to be encoded as CBOR array. // "omitempty" is disabled by "toarray" to ensure that the same number // of elements are encoded every time. // // Anonymous struct fields are marshaled as if their exported fields // were fields in the outer struct. Marshal follows the same struct fields // visibility rules used by JSON encoding package. // // time.Time values encode as text strings specified in RFC3339 or numerical // representation of seconds since January 1, 1970 UTC depending on // EncOptions.Time setting. Also See EncOptions.TimeTag to encode // time.Time as CBOR tag with tag number 0 or 1. // // big.Int values encode as CBOR integers (type 0 and 1) if values fit. // Otherwise, big.Int values encode as CBOR bignums (tag 2 and 3). See // EncOptions.BigIntConvert to always encode big.Int values as CBOR // bignums. // // Pointer values encode as the value pointed to. // // Interface values encode as the value stored in the interface. // // Nil slice/map/pointer/interface values encode as CBOR nulls (type 7). // // Values of other types cannot be encoded in CBOR. Attempting // to encode such a value causes Marshal to return an UnsupportedTypeError. func Marshal(v interface{ … } // MarshalToBuffer encodes v into provided buffer (instead of using built-in buffer pool) // and uses default encoding options. // // NOTE: Unlike Marshal, the buffer provided to MarshalToBuffer can contain // partially encoded data if error is returned. // // See Marshal for more details. func MarshalToBuffer(v interface{ … } type Marshaler … type MarshalerError … func (e *MarshalerError) Error() string { … } func (e *MarshalerError) Unwrap() error { … } type UnsupportedTypeError … func (e *UnsupportedTypeError) Error() string { … } type UnsupportedValueError … func (e *UnsupportedValueError) Error() string { … } type SortMode … const SortNone … const SortLengthFirst … const SortBytewiseLexical … const SortFastShuffle … const SortCanonical … const SortCTAP2 … const SortCoreDeterministic … const maxSortMode … func (sm SortMode) valid() bool { … } type StringMode … const StringToTextString … const StringToByteString … func (st StringMode) cborType() (cborType, error) { … } type ShortestFloatMode … const ShortestFloatNone … const ShortestFloat16 … const maxShortestFloat … func (sfm ShortestFloatMode) valid() bool { … } type NaNConvertMode … const NaNConvert7e00 … const NaNConvertNone … const NaNConvertPreserveSignal … const NaNConvertQuiet … const NaNConvertReject … const maxNaNConvert … func (ncm NaNConvertMode) valid() bool { … } type InfConvertMode … const InfConvertFloat16 … const InfConvertNone … const InfConvertReject … const maxInfConvert … func (icm InfConvertMode) valid() bool { … } type TimeMode … const TimeUnix … const TimeUnixMicro … const TimeUnixDynamic … const TimeRFC3339 … const TimeRFC3339Nano … const maxTimeMode … func (tm TimeMode) valid() bool { … } type BigIntConvertMode … const BigIntConvertShortest … const BigIntConvertNone … const BigIntConvertReject … const maxBigIntConvert … func (bim BigIntConvertMode) valid() bool { … } type NilContainersMode … const NilContainerAsNull … const NilContainerAsEmpty … const maxNilContainersMode … func (m NilContainersMode) valid() bool { … } type OmitEmptyMode … const OmitEmptyCBORValue … const OmitEmptyGoValue … const maxOmitEmptyMode … func (om OmitEmptyMode) valid() bool { … } type FieldNameMode … const FieldNameToTextString … const FieldNameToByteString … const maxFieldNameMode … func (fnm FieldNameMode) valid() bool { … } type ByteSliceLaterFormatMode … const ByteSliceLaterFormatNone … const ByteSliceLaterFormatBase64URL … const ByteSliceLaterFormatBase64 … const ByteSliceLaterFormatBase16 … func (bsefm ByteSliceLaterFormatMode) encodingTag() (uint64, error) { … } type ByteArrayMode … const ByteArrayToByteSlice … const ByteArrayToArray … const maxByteArrayMode … func (bam ByteArrayMode) valid() bool { … } type BinaryMarshalerMode … const BinaryMarshalerByteString … const BinaryMarshalerNone … const maxBinaryMarshalerMode … func (bmm BinaryMarshalerMode) valid() bool { … } type EncOptions … // CanonicalEncOptions returns EncOptions for "Canonical CBOR" encoding, // defined in RFC 7049 Section 3.9 with the following rules: // // 1. "Integers must be as small as possible." // 2. "The expression of lengths in major types 2 through 5 must be as short as possible." // 3. The keys in every map must be sorted in length-first sorting order. // See SortLengthFirst for details. // 4. "Indefinite-length items must be made into definite-length items." // 5. "If a protocol allows for IEEE floats, then additional canonicalization rules might // need to be added. One example rule might be to have all floats start as a 64-bit // float, then do a test conversion to a 32-bit float; if the result is the same numeric // value, use the shorter value and repeat the process with a test conversion to a // 16-bit float. (This rule selects 16-bit float for positive and negative Infinity // as well.) Also, there are many representations for NaN. If NaN is an allowed value, // it must always be represented as 0xf97e00." func CanonicalEncOptions() EncOptions { … } // CTAP2EncOptions returns EncOptions for "CTAP2 Canonical CBOR" encoding, // defined in CTAP specification, with the following rules: // // 1. "Integers must be encoded as small as possible." // 2. "The representations of any floating-point values are not changed." // 3. "The expression of lengths in major types 2 through 5 must be as short as possible." // 4. "Indefinite-length items must be made into definite-length items."" // 5. The keys in every map must be sorted in bytewise lexicographic order. // See SortBytewiseLexical for details. // 6. "Tags as defined in Section 2.4 in [RFC7049] MUST NOT be present." func CTAP2EncOptions() EncOptions { … } // CoreDetEncOptions returns EncOptions for "Core Deterministic" encoding, // defined in RFC 7049bis with the following rules: // // 1. "Preferred serialization MUST be used. In particular, this means that arguments // (see Section 3) for integers, lengths in major types 2 through 5, and tags MUST // be as short as possible" // "Floating point values also MUST use the shortest form that preserves the value" // 2. "Indefinite-length items MUST NOT appear." // 3. "The keys in every map MUST be sorted in the bytewise lexicographic order of // their deterministic encodings." func CoreDetEncOptions() EncOptions { … } // PreferredUnsortedEncOptions returns EncOptions for "Preferred Serialization" encoding, // defined in RFC 7049bis with the following rules: // // 1. "The preferred serialization always uses the shortest form of representing the argument // (Section 3);" // 2. "it also uses the shortest floating-point encoding that preserves the value being // encoded (see Section 5.5)." // "The preferred encoding for a floating-point value is the shortest floating-point encoding // that preserves its value, e.g., 0xf94580 for the number 5.5, and 0xfa45ad9c00 for the // number 5555.5, unless the CBOR-based protocol specifically excludes the use of the shorter // floating-point encodings. For NaN values, a shorter encoding is preferred if zero-padding // the shorter significand towards the right reconstitutes the original NaN value (for many // applications, the single NaN encoding 0xf97e00 will suffice)." // 3. "Definite length encoding is preferred whenever the length is known at the time the // serialization of the item starts." func PreferredUnsortedEncOptions() EncOptions { … } // EncMode returns EncMode with immutable options and no tags (safe for concurrency). func (opts EncOptions) EncMode() (EncMode, error) { … } // UserBufferEncMode returns UserBufferEncMode with immutable options and no tags (safe for concurrency). func (opts EncOptions) UserBufferEncMode() (UserBufferEncMode, error) { … } // EncModeWithTags returns EncMode with options and tags that are both immutable (safe for concurrency). func (opts EncOptions) EncModeWithTags(tags TagSet) (EncMode, error) { … } // UserBufferEncModeWithTags returns UserBufferEncMode with options and tags that are both immutable (safe for concurrency). func (opts EncOptions) UserBufferEncModeWithTags(tags TagSet) (UserBufferEncMode, error) { … } // EncModeWithSharedTags returns EncMode with immutable options and mutable shared tags (safe for concurrency). func (opts EncOptions) EncModeWithSharedTags(tags TagSet) (EncMode, error) { … } // UserBufferEncModeWithSharedTags returns UserBufferEncMode with immutable options and mutable shared tags (safe for concurrency). func (opts EncOptions) UserBufferEncModeWithSharedTags(tags TagSet) (UserBufferEncMode, error) { … } func (opts EncOptions) encMode() (*encMode, error) { … } type EncMode … type UserBufferEncMode … type encMode … var ( defaultEncMode … _ … ) var marshalerForbidIndefLengthForbidTagsDecMode … var marshalerAllowIndefLengthForbidTagsDecMode … var marshalerForbidIndefLengthAllowTagsDecMode … var marshalerAllowIndefLengthAllowTagsDecMode … // getMarshalerDecMode returns one of four existing decoding modes // which can be reused (safe for parallel use) for the purpose of // checking if data returned by Marshaler is well-formed. func getMarshalerDecMode(indefLength IndefLengthMode, tagsMd TagsMode) *decMode { … } // EncOptions returns user specified options used to create this EncMode. func (em *encMode) EncOptions() EncOptions { … } func (em *encMode) unexport() { … } func (em *encMode) encTagBytes(t reflect.Type) []byte { … } // Marshal returns the CBOR encoding of v using em encoding mode. // // See the documentation for Marshal for details. func (em *encMode) Marshal(v interface{ … } // MarshalToBuffer encodes v into provided buffer (instead of using built-in buffer pool) // and uses em encoding mode. // // NOTE: Unlike Marshal, the buffer provided to MarshalToBuffer can contain // partially encoded data if error is returned. // // See Marshal for more details. func (em *encMode) MarshalToBuffer(v interface{ … } // NewEncoder returns a new encoder that writes to w using em EncMode. func (em *encMode) NewEncoder(w io.Writer) *Encoder { … } var encodeBufferPool … func getEncodeBuffer() *bytes.Buffer { … } func putEncodeBuffer(e *bytes.Buffer) { … } type encodeFunc … type isEmptyFunc … func encode(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } func encodeBool(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } func encodeInt(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } func encodeUint(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } func encodeFloat(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } func encodeInf(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } func encodeNaN(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } func encodeFloat16(e *bytes.Buffer, f16 float16.Float16) error { … } func encodeFloat32(e *bytes.Buffer, f32 float32) error { … } func encodeFloat64(e *bytes.Buffer, f64 float64) error { … } func encodeByteString(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } func encodeString(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } type arrayEncodeFunc … func (ae arrayEncodeFunc) encode(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } type encodeKeyValueFunc … type mapEncodeFunc … func (me mapEncodeFunc) encode(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } type keyValue … type bytewiseKeyValueSorter … func (x *bytewiseKeyValueSorter) Len() int { … } func (x *bytewiseKeyValueSorter) Swap(i, j int) { … } func (x *bytewiseKeyValueSorter) Less(i, j int) bool { … } type lengthFirstKeyValueSorter … func (x *lengthFirstKeyValueSorter) Len() int { … } func (x *lengthFirstKeyValueSorter) Swap(i, j int) { … } func (x *lengthFirstKeyValueSorter) Less(i, j int) bool { … } var keyValuePool … func getKeyValues(length int) *[]keyValue { … } func putKeyValues(x *[]keyValue) { … } func encodeStructToArray(e *bytes.Buffer, em *encMode, v reflect.Value) (err error) { … } func encodeStruct(e *bytes.Buffer, em *encMode, v reflect.Value) (err error) { … } func encodeIntf(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } func encodeTime(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } func encodeBigInt(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } type binaryMarshalerEncoder … func (bme binaryMarshalerEncoder) encode(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } func (bme binaryMarshalerEncoder) isEmpty(em *encMode, v reflect.Value) (bool, error) { … } func encodeMarshalerType(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } func encodeTag(e *bytes.Buffer, em *encMode, v reflect.Value) error { … } // encodeHead writes CBOR head of specified type t and returns number of bytes written. func encodeHead(e *bytes.Buffer, t byte, n uint64) int { … } var typeMarshaler … var typeBinaryMarshaler … var typeRawMessage … var typeByteString … func getEncodeFuncInternal(t reflect.Type) (ef encodeFunc, ief isEmptyFunc) { … } func getEncodeIndirectValueFunc(t reflect.Type) encodeFunc { … } func alwaysNotEmpty(_ *encMode, _ reflect.Value) (empty bool, err error) { … } func isEmptyBool(_ *encMode, v reflect.Value) (bool, error) { … } func isEmptyInt(_ *encMode, v reflect.Value) (bool, error) { … } func isEmptyUint(_ *encMode, v reflect.Value) (bool, error) { … } func isEmptyFloat(_ *encMode, v reflect.Value) (bool, error) { … } func isEmptyString(_ *encMode, v reflect.Value) (bool, error) { … } func isEmptySlice(_ *encMode, v reflect.Value) (bool, error) { … } func isEmptyMap(_ *encMode, v reflect.Value) (bool, error) { … } func isEmptyPtr(_ *encMode, v reflect.Value) (bool, error) { … } func isEmptyIntf(_ *encMode, v reflect.Value) (bool, error) { … } func isEmptyStruct(em *encMode, v reflect.Value) (bool, error) { … } func cannotFitFloat32(f64 float64) bool { … } // float32NaNFromReflectValue extracts float32 NaN from reflect.Value while preserving NaN's quiet bit. func float32NaNFromReflectValue(v reflect.Value) float32 { … }