Documentation ¶
Overview ¶
CBOR is IETF RFC 7049, the "Concise Binary Object Representation" http://tools.ietf.org/html/rfc7049
In can be thought of as "binary JSON" but is a superset and somewhat richer representation than JSON.
Other implementations and more information can also be found at: http://cbor.io/
Serialization and deserialization of structs uses the same tag format as the encoding/json package. If different json and cbor serialization names are needed, a tag `cbor:"fieldName"` can be specified. Example:
type DemoStruct struct { FieldNeedsDifferentName string `json:"serialization_name"` FieldNeedsJsonVsCborName int `json:"json_name" cbor:"cbor_name"` }
This might generate json: {"serialization_name":"foo", "json_name":2}
And CBOR equivalent to: {"serialization_name":"foo", "cbor_name":2}
Index ¶
- Constants
- func Dumps(ob interface{}) ([]byte, error)
- func Encode(out io.Writer, ob interface{}) error
- func EncodeInt(tag byte, v uint64, buf []byte) []byte
- func EncodeInt16(tag byte, v uint16, buf []byte) []byte
- func EncodeInt32(tag byte, v uint32, buf []byte) []byte
- func EncodeInt64(tag byte, v uint64, buf []byte) []byte
- func EncodeInt8(tag byte, v uint8, buf []byte) []byte
- func EncodeOpcode(tag byte, opcode byte, buf []byte) []byte
- func Loads(blob []byte, v interface{}) error
- type CBORTag
- type CBORValue
- type DecodeValue
- type DecodeValueArray
- type DecodeValueMap
- type Decoder
- type Encoder
- type InvalidUnmarshalError
- type MarshallValue
- type MemoryValue
- func (r *MemoryValue) CreateArray(makeLength int) (DecodeValueArray, error)
- func (r *MemoryValue) CreateMap() (DecodeValueMap, error)
- func (r *MemoryValue) CreateTag(aux uint64, decoder TagDecoder) (DecodeValue, interface{}, error)
- func (r *MemoryValue) Prepare() error
- func (mv *MemoryValue) ReflectValue() reflect.Value
- func (r *MemoryValue) SetBignum(x *big.Int) error
- func (r *MemoryValue) SetBool(b bool) error
- func (r *MemoryValue) SetBytes(buf []byte) error
- func (r *MemoryValue) SetFloat32(f float32) error
- func (r *MemoryValue) SetFloat64(d float64) error
- func (r *MemoryValue) SetInt(i int64) error
- func (r *MemoryValue) SetNil() error
- func (r *MemoryValue) SetString(xs string) error
- func (r *MemoryValue) SetTag(code uint64, val DecodeValue, decoder TagDecoder, target interface{}) error
- func (r *MemoryValue) SetUint(u uint64) error
- type SimpleMarshallValue
- type TagDecoder
Constants ¶
const ( MajorTypeUint byte = 0 MajorTypeNegInt byte = iota MajorTypeBytes MajorTypeText MajorTypeArray MajorTypeMap MajorTypeTag MajorTypeSimple MajorTypeFloat byte = MajorTypeSimple )
const ( SimpleValueFalse byte = 20 SimpleValueTrue byte = iota SimpleValueNull SimpleValueUndefined )
const (
OpcodeBreak byte = 0x1F
)
Variables ¶
This section is empty.
Functions ¶
func EncodeInt ¶
Encode a CBOR integer unit. The first argument is the major type, the second argument is the integer value. The result is a byte array from 1 to 9 bytes depending on the size of the integer value.
The major type (tag argument) must be an integer between 0 and 7 else this function panics
If the third parameter is non nil, the slice is reused to construct the result to avoid a memory allocation. It should be a slice with a sufficient capacity.
Types ¶
type DecodeValue ¶
type DecodeValue interface { // Before decoding, check if there is no error Prepare() error // Got binary string SetBytes(buf []byte) error // Got a number (different formats) SetBignum(x *big.Int) error SetUint(u uint64) error SetInt(i int64) error SetFloat32(f float32) error SetFloat64(d float64) error // Got null SetNil() error // Got boolean SetBool(b bool) error // Got text string SetString(s string) error // Got a Map (beginning) CreateMap() (DecodeValueMap, error) // Got an array (beginning) CreateArray(makeLength int) (DecodeValueArray, error) // Got a tag CreateTag(aux uint64, decoder TagDecoder) (DecodeValue, interface{}, error) // Got the tag value (maybe transformed by TagDecoder.PostDecode) SetTag(aux uint64, v DecodeValue, decoder TagDecoder, i interface{}) error }
type DecodeValueArray ¶
type DecodeValueArray interface { // Got an array item GetArrayValue(index uint64) (DecodeValue, error) // After the array item is decoded AppendArray(value DecodeValue) error // The array is at the end EndArray() error }
type DecodeValueMap ¶
type DecodeValueMap interface { // Got a map key CreateMapKey() (DecodeValue, error) // Got a map value CreateMapValue(key DecodeValue) (DecodeValue, error) // Got a key / value pair SetMap(key, val DecodeValue) error // The map is at the end EndMap() error }
type Decoder ¶
type Decoder struct { // Extra processing for CBOR TAG objects. TagDecoders map[uint64]TagDecoder // contains filtered or unexported fields }
func NewDecoder ¶
func (*Decoder) DecodeAny ¶
func (dec *Decoder) DecodeAny(v DecodeValue) error
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
func NewEncoder ¶
Return new Encoder object for writing to supplied io.Writer.
TODO: set options on Encoder object.
type InvalidUnmarshalError ¶
copied from encoding/json/decode.go 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 MarshallValue ¶
type MarshallValue interface { // Convert the value to CBOR. Specific CBOR data (such as tags) can be written // on the io.Writer and more complex datatype can be written using the // Encoder. // // To Write a Tag value, a possible implementation would be: // // w.Write(cbor.EncodeTag(6, tag_value)) // enc.Encode(tagged_value) // ToCBOR(w io.Writer, enc *Encoder) error }
type MemoryValue ¶
type MemoryValue struct { Value interface{} // contains filtered or unexported fields }
func NewMemoryValue ¶
func NewMemoryValue(value interface{}) *MemoryValue
func (*MemoryValue) CreateArray ¶
func (r *MemoryValue) CreateArray(makeLength int) (DecodeValueArray, error)
func (*MemoryValue) CreateMap ¶
func (r *MemoryValue) CreateMap() (DecodeValueMap, error)
func (*MemoryValue) CreateTag ¶
func (r *MemoryValue) CreateTag(aux uint64, decoder TagDecoder) (DecodeValue, interface{}, error)
func (*MemoryValue) ReflectValue ¶
func (mv *MemoryValue) ReflectValue() reflect.Value
func (*MemoryValue) SetFloat32 ¶
func (*MemoryValue) SetFloat64 ¶
func (*MemoryValue) SetTag ¶
func (r *MemoryValue) SetTag(code uint64, val DecodeValue, decoder TagDecoder, target interface{}) error
type SimpleMarshallValue ¶
type TagDecoder ¶
type TagDecoder interface { // Handle things which match this. // // Setup like this: // var dec Decoder // var myTagDec TagDecoder // dec.TagDecoders[myTagDec.GetTag()] = myTagDec GetTag() uint64 // Sub-object will be decoded onto the returned object. DecodeTarget() interface{} // Run after decode onto DecodeTarget has happened. // The return value from this is returned in place of the // raw decoded object. PostDecode(interface{}) (interface{}, error) }