Skip to content

LonelyPale/bson

Repository files navigation

bson

import "github.com/pkg/bson"

Package bson implements encoding and decoding of BSON objects as defined at a href="http://bsonspec.org/spec.html">http://bsonspec.org/spec.html. The mapping between BSON objects and Go values is described in the documentation for the Marshal and Unmarshal functions.

Variables

var ErrTooShort = errors.New("bson document too short")

func Marshal

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

Marshal returns the BSON encoding of v.

Struct values encode as JSON objects. Each exported struct field becomes a member of the object unless

- the field's tag is "-", or
- the field is empty and its tag specifies the "omitempty" option.

Map values encode as JSON objects. The map's key type must be string; the object keys are used directly as map keys.

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal parses the BSON-encoded data and stores the result in the value pointed to by v.

Unmarshal uses the inverse of the encodings that Marshal uses, allocating maps, slices, and pointers as necessary.

Portions of data may be retained by the decoded result in v. Data should not be reused.

type Decoder

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

A Decoder reads and decodes BSON objects from an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

func (*Decoder) Decode

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

Decode reads the next BSON-encoded value from its input and stores it in the value pointed to by v.

See the documentation for Unmarshal for details about the conversion of BSON into a Go value.

type Encoder

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

An Encoder writes BSON objects to an output stream.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode

func (e *Encoder) Encode(v interface{}) error

Encode writes the BSON encoding of v to the stream, followed by a newline character.

See the documentation for Marshal for details about the conversion of Go values to BSON.

type InvalidBSONTypeError

type InvalidBSONTypeError struct {
    Type byte
}

An InvalidBSONTypeError describes an unhandled BSON document element type.

func (*InvalidBSONTypeError) Error

func (e *InvalidBSONTypeError) Error() string

type InvalidUnmarshalError

type InvalidUnmarshalError struct {
    Type reflect.Type
}

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 MarshalerError

type MarshalerError struct {
    Type reflect.Type
    Err  error
}

func (*MarshalerError) Error

func (e *MarshalerError) Error() string

type UnsupportedTypeError

type UnsupportedTypeError struct {
    Type reflect.Type
}

An UnsupportedTypeError is returned by Marshal when attempting to encode an unsupported value type.

func (*UnsupportedTypeError) Error

func (e *UnsupportedTypeError) Error() string

Generated by godoc2md

About

Package bson implements encoding and decoding of BSON objects defined at http://bsonspec.org/spec.html.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages