cbor

package
v0.0.0-...-8b9b725 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2019 License: MIT, Apache-2.0 Imports: 10 Imported by: 5

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

View Source
const (
	MajorTypeUint   byte = 0
	MajorTypeNegInt byte = iota
	MajorTypeBytes
	MajorTypeText
	MajorTypeArray
	MajorTypeMap
	MajorTypeTag
	MajorTypeSimple
	MajorTypeFloat byte = MajorTypeSimple
)
View Source
const (
	SimpleValueFalse byte = 20
	SimpleValueTrue  byte = iota
	SimpleValueNull
	SimpleValueUndefined
)
View Source
const (
	OpcodeBreak byte = 0x1F
)

Variables

This section is empty.

Functions

func Dumps

func Dumps(ob interface{}) ([]byte, error)

Write out an object to a new byte slice

func Encode

func Encode(out io.Writer, ob interface{}) error

Write out an object to an io.Writer

func EncodeInt

func EncodeInt(tag byte, v uint64, buf []byte) []byte

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.

func EncodeInt16

func EncodeInt16(tag byte, v uint16, buf []byte) []byte

func EncodeInt32

func EncodeInt32(tag byte, v uint32, buf []byte) []byte

func EncodeInt64

func EncodeInt64(tag byte, v uint64, buf []byte) []byte

func EncodeInt8

func EncodeInt8(tag byte, v uint8, buf []byte) []byte

func EncodeOpcode

func EncodeOpcode(tag byte, opcode byte, buf []byte) []byte

func Loads

func Loads(blob []byte, v interface{}) error

Load one object into v

Types

type CBORTag

type CBORTag struct {
	Tag           uint64
	WrappedObject interface{}
}

func (*CBORTag) ToCBOR

func (t *CBORTag) ToCBOR(w io.Writer, enc *Encoder) error

type CBORValue

type CBORValue []byte

func (CBORValue) ToCBOR

func (v CBORValue) ToCBOR(w io.Writer) error

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 NewDecoder(r io.Reader) *Decoder

func (*Decoder) Decode

func (dec *Decoder) Decode(v interface{}) error

func (*Decoder) DecodeAny

func (dec *Decoder) DecodeAny(v DecodeValue) error

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

func NewEncoder

func NewEncoder(out io.Writer) *Encoder

Return new Encoder object for writing to supplied io.Writer.

TODO: set options on Encoder object.

func (*Encoder) Encode

func (enc *Encoder) Encode(ob interface{}) error

func (*Encoder) SetFilter

func (enc *Encoder) SetFilter(filter func(v interface{}) interface{})

type InvalidUnmarshalError

type InvalidUnmarshalError struct {
	Type reflect.Type
}

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) Prepare

func (r *MemoryValue) Prepare() error

func (*MemoryValue) ReflectValue

func (mv *MemoryValue) ReflectValue() reflect.Value

func (*MemoryValue) SetBignum

func (r *MemoryValue) SetBignum(x *big.Int) error

func (*MemoryValue) SetBool

func (r *MemoryValue) SetBool(b bool) error

func (*MemoryValue) SetBytes

func (r *MemoryValue) SetBytes(buf []byte) error

func (*MemoryValue) SetFloat32

func (r *MemoryValue) SetFloat32(f float32) error

func (*MemoryValue) SetFloat64

func (r *MemoryValue) SetFloat64(d float64) error

func (*MemoryValue) SetInt

func (r *MemoryValue) SetInt(i int64) error

func (*MemoryValue) SetNil

func (r *MemoryValue) SetNil() error

func (*MemoryValue) SetString

func (r *MemoryValue) SetString(xs string) error

func (*MemoryValue) SetTag

func (r *MemoryValue) SetTag(code uint64, val DecodeValue, decoder TagDecoder, target interface{}) error

func (*MemoryValue) SetUint

func (r *MemoryValue) SetUint(u uint64) error

type SimpleMarshallValue

type SimpleMarshallValue interface {
	// Convert the value to CBOR. The object is responsible to convert to CBOR
	// in the correct format.
	ToCBOR(w io.Writer) error
}

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)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL