bson

package
v0.26.13 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package bson is the BSON codec: a native implementation of the BSON 1.1 specification (bsonspec.org) behind the universal core/data/codec.Codec dispatch, written with the standard library alone.

BSON is a document format: the top-level value MUST encode as a document — a struct, a map, a D — so Marshal of a top-level scalar or array surfaces BSON_MARSHAL_FAILED. Go values map onto BSON as the MongoDB Go driver's v1 default registry mapped them, struct tags included (bson:"name,omitempty,minsize,truncate,inline"), and the BSON types Go has no type for decode into this package's own: ObjectID, DateTime, Decimal128, Binary, Regex, Timestamp, D, M, A and the rest.

Unmarshal checks the whole input before it touches the target — every declared length against the bytes remaining, every terminator, every type byte, every string's UTF-8, a nesting depth of at most 100 — so a malformed document is refused whole and never half-decoded. The codec is not a StreamingCodec; it implements the optional Appender.

Package bson — Decimal128, BSON's IEEE 754-2008 128-bit decimal in the binary integer decimal (BID) encoding, and its string forms as the BSON decimal128 specification defines them. Conversion is exact or refused: nothing here rounds.

Package bson — the decoder's entry point, its dispatch on the target's plan, and the element walk. It runs over a document the validator accepted, and still checks every length it slices by, so input that changes under it yields an error and never a panic.

Package bson — decoding into an interface: the Go value each BSON type becomes when nothing else says what it should be. The table is the previous library's, its types replaced by this package's: a double is a float64, an int32 an int32, an int64 an int64, a string a string, a boolean a bool, a null nil, an array an A, a datetime a DateTime, a binary a Binary, and so on. A document becomes the ancestor type when one is set — the type of the nearest enclosing map[string]any, M or slice of E — and a D otherwise, so a document decoded into *any is a D all the way down while one decoded into a map[string]any is maps all the way down.

Package bson — decoding into the containers: slices, arrays, D, maps and structs. Each follows the previous library's rules: a slice reuses its backing array, an array keeps the elements a short value does not reach, a map is added to rather than replaced, a struct keeps the fields the document does not name.

Package bson — decoding into scalar targets. The conversions accepted are the previous library's: a number decodes into any numeric target it fits, a boolean into a number and a number into a boolean, null and undefined into the zero value; anything else is refused rather than guessed.

Package bson — decoding into the codec's own value types and into the stdlib types it knows: time.Time, url.URL and json.Number. A null or an undefined is the zero value of every one of them.

Package bson — the encoder's entry point and dispatch. It appends one document to a byte slice, reflection planned once per type, the common interface values taken by a type switch instead. Each element's type byte is written as a placeholder and set once the value has said what it encodes as; each document's length is reserved and filled in when the document ends.

Package bson — encoding of the containers: arrays, ordered documents, structs and maps. A map's entries are written in the map's own order and then put in ascending key order, so the same map always encodes to the same bytes.

Package bson — the encoder's primitive writes: the fixed-width numbers, the strings, the binaries, each little-endian as BSON stores them.

Package bson — encoding of the codec's own value types and of the stdlib types the codec knows: time.Time, url.URL and json.Number.

Package bson — the constructors that give each failure its own log-only detail. The codes and the sentinels they wrap are declared in internal/core/data/codec/bson (ADR 0160).

Package bson — ObjectID, BSON's twelve-byte identifier, and its textual forms: 24 lowercase hexadecimal digits in text and in JSON, the form MongoDB tooling prints.

Package bson — the per-type plans: how a Go type maps onto BSON, worked out once from reflection and cached by reflect.Type, so an encode or a decode only reads what was decided. A plan is built under one mutex and published complete; reading a published plan takes no lock.

Package bson — the Go types for the BSON values Go has no type of its own for. Decoding into an interface produces them, encoding writes them back as the BSON type they stand for, and they carry the semantics the codec's previous library gave its own equivalents, so a value that crossed a map[string]any before keeps meaning the same thing.

Package bson — the structural validator. Unmarshal runs it over the whole input before the target is touched, so a malformed document never leaves a half-decoded value behind, and the decoder that follows reads bytes whose every length, terminator, type byte and string has been checked. Every declared length is compared with the bytes actually remaining before anything is sliced or allocated from it.

Package bson — the BSON 1.1 wire vocabulary (bsonspec.org): the element type bytes, the binary subtypes, the bounds every reader and writer shares, and the little-endian reads they are built from.

Index

Constants

View Source
const (
	// BinaryGeneric is the default subtype, and the one a []byte is written as.
	BinaryGeneric byte = 0x00
	// BinaryFunction marks a function.
	BinaryFunction byte = 0x01
	// BinaryOld is the deprecated generic subtype: its bytes are themselves an
	// int32 length followed by that many bytes, and the codec writes and checks
	// that inner length.
	BinaryOld byte = 0x02
	// BinaryUUIDOld is the deprecated, byte-order-ambiguous UUID subtype.
	BinaryUUIDOld byte = 0x03
	// BinaryUUID is an RFC 9562 UUID.
	BinaryUUID byte = 0x04
	// BinaryMD5 is an MD5 digest.
	BinaryMD5 byte = 0x05
	// BinaryEncrypted is a client-side encrypted value.
	BinaryEncrypted byte = 0x06
	// BinaryColumn is a compressed time-series column.
	BinaryColumn byte = 0x07
	// BinarySensitive marks data a server must not log.
	BinarySensitive byte = 0x08
	// BinaryVector is a packed numeric vector.
	BinaryVector byte = 0x09
	// BinaryUserDefined is the first of the subtypes reserved for applications.
	BinaryUserDefined byte = 0x80
)

Binary subtypes, BSON 1.1. A subtype is a single byte the codec carries through unchanged; only BinaryGeneric and BinaryOld decode into []byte.

Variables

View Source
var Codec codec.Codec = codec.Register(&bsonCodec{})

Codec is the registered BSON singleton.

Functions

func New

func New() codec.Codec

New returns the BSON codec singleton.

Types

type A

type A []any

A is an array: the shape a decode into an interface produces for one.

type Binary

type Binary struct {
	// Subtype is the BSON binary subtype, BinaryGeneric for opaque bytes.
	Subtype byte
	// Data is the payload.
	Data []byte
}

Binary is a BSON binary value: the bytes and the subtype naming what they are. A []byte encodes as a Binary of subtype BinaryGeneric; a Binary of any other subtype keeps its subtype through a round trip.

func (Binary) Equal

func (b Binary) Equal(other Binary) bool

Equal reports whether b and other hold the same subtype and bytes.

func (Binary) IsZero

func (b Binary) IsZero() bool

IsZero reports whether b is the empty generic binary — what omitempty leaves out.

type CodeWithScope

type CodeWithScope struct {
	// Code is the JavaScript.
	Code JavaScript
	// Scope is the document.
	Scope any
}

CodeWithScope is the deprecated BSON JavaScript-with-scope: code and the document its free variables are bound in. Scope must encode as a document — a struct, a map or a D; a decode always produces a D.

func (CodeWithScope) String

func (c CodeWithScope) String() string

String renders c for a log line or a test failure.

type D

type D []E

D is a document whose element order matters: the shape a decode into an interface produces for a document, and the one to encode when the order of the keys is part of the meaning — a command, a sort specification. Encode an M or a struct when it is not. A nil D encodes as null.

type DBPointer

type DBPointer struct {
	// DB is the namespace the pointer names.
	DB string
	// Pointer is the identifier within it.
	Pointer ObjectID
}

DBPointer is the deprecated BSON DBPointer: a namespace and an ObjectID. It is read and written so a document carrying one survives a round trip.

func (DBPointer) Equal

func (p DBPointer) Equal(other DBPointer) bool

Equal reports whether p and other name the same namespace and identifier.

func (DBPointer) IsZero

func (p DBPointer) IsZero() bool

IsZero reports whether p is the empty pointer — what omitempty leaves out.

func (DBPointer) String

func (p DBPointer) String() string

String renders p for a log line or a test failure.

type DateTime

type DateTime int64

DateTime is a BSON datetime: milliseconds since the Unix epoch, UTC. A time.Time encodes as one and decodes from one; DateTime is what a decode into an interface produces.

func NewDateTimeFromTime

func NewDateTimeFromTime(t time.Time) DateTime

NewDateTimeFromTime returns the DateTime t falls in, truncated to the millisecond: the instant a time.Time is written as.

func (DateTime) MarshalJSON

func (dt DateTime) MarshalJSON() ([]byte, error)

MarshalJSON writes dt as encoding/json writes the UTC time.Time it denotes.

func (DateTime) Time

func (dt DateTime) Time() time.Time

Time returns the instant dt denotes, in the local time zone; call UTC on it for the zone BSON stores.

func (*DateTime) UnmarshalJSON

func (dt *DateTime) UnmarshalJSON(data []byte) error

UnmarshalJSON reads the textual form MarshalJSON writes. A JSON null leaves dt unchanged, as it leaves a time.Time.

type Decimal128

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

Decimal128 is a BSON decimal128: a coefficient of up to 34 decimal digits, an exponent in [-6176, 6111], a sign, and the specials NaN and ±Infinity. The zero value is 0E-6176, the value whose sixteen bytes are all zero; it is what IsZero reports, and what omitempty leaves out.

func NewDecimal128

func NewDecimal128(high, low uint64) Decimal128

NewDecimal128 returns the decimal whose BID encoding is high (the first eight bytes, as a uint64) and low (the last eight).

func ParseDecimal128

func ParseDecimal128(s string) (Decimal128, error)

ParseDecimal128 parses the specification's string form: an optional sign, a decimal with or without a point, an optional exponent introduced by e or E, or one of NaN, Inf and Infinity in any case. A value that a decimal128 cannot hold exactly — more than 34 significant digits, or an exponent out of range — is refused with BSON_VALUE_INVALID rather than rounded; trailing zeros are traded for exponent, both ways, as far as that keeps it exact.

func (Decimal128) GetBytes

func (d Decimal128) GetBytes() (high, low uint64)

GetBytes returns the two halves NewDecimal128 takes.

func (Decimal128) IsInf

func (d Decimal128) IsInf() int

IsInf returns +1 for +Infinity, -1 for -Infinity, and 0 otherwise.

func (Decimal128) IsNaN

func (d Decimal128) IsNaN() bool

IsNaN reports whether d is a NaN, quiet or signalling, whatever its sign.

func (Decimal128) IsZero

func (d Decimal128) IsZero() bool

IsZero reports whether d is the zero value — all sixteen bytes zero. It is not a numeric test: "0", "0E+3" and "-0" are zeros IsZero does not report.

func (Decimal128) MarshalJSON

func (d Decimal128) MarshalJSON() ([]byte, error)

MarshalJSON writes d's string form as a JSON string.

func (Decimal128) String

func (d Decimal128) String() string

String returns the specification's string form: "NaN", "Infinity", "-Infinity", plain notation when the exponent is not positive and the adjusted exponent is at least -6, scientific notation otherwise.

func (*Decimal128) UnmarshalJSON

func (d *Decimal128) UnmarshalJSON(data []byte) error

UnmarshalJSON reads a JSON string holding the string form, or the extended-JSON object {"$numberDecimal": "…"}. A JSON null leaves d unchanged.

type E

type E struct {
	// Key is the element name. It must not contain a NUL byte.
	Key string
	// Value is any value the codec encodes.
	Value any
}

E is one element of a D: a key and its value. Encoded on its own, outside a D, it is an ordinary struct, the document {"key": …, "value": …}.

type JavaScript

type JavaScript string

JavaScript is BSON JavaScript code.

type M

type M map[string]any

M is a document whose element order does not matter. The codec writes a map's keys in sorted order, so encoding the same M twice gives the same bytes.

type MaxKey

type MaxKey struct{}

MaxKey is the BSON value that sorts higher than every other.

type MinKey

type MinKey struct{}

MinKey is the BSON value that sorts lower than every other.

type Null

type Null struct{}

Null is the BSON null value, for writing an explicit null into a D. A null decodes into an interface as nil.

type ObjectID

type ObjectID [objectIDSize]byte

ObjectID is a BSON ObjectID: a four-byte big-endian creation time in seconds, five bytes unique to the generating process, and a three-byte counter. The zero value is NilObjectID. Decoding an ObjectID into a string target yields its hexadecimal form; decoding a 24-digit hexadecimal string into an ObjectID target parses it.

var NilObjectID ObjectID

NilObjectID is the zero ObjectID.

func ObjectIDFromHex

func ObjectIDFromHex(s string) (ObjectID, error)

ObjectIDFromHex parses the 24-digit hexadecimal form of an ObjectID, in either case. Anything else is refused with BSON_VALUE_INVALID.

func (ObjectID) Hex

func (id ObjectID) Hex() string

Hex returns the 24-digit lowercase hexadecimal form of id.

func (ObjectID) IsZero

func (id ObjectID) IsZero() bool

IsZero reports whether id is NilObjectID — what omitempty leaves out.

func (ObjectID) MarshalJSON

func (id ObjectID) MarshalJSON() ([]byte, error)

MarshalJSON writes the hexadecimal form as a JSON string.

func (ObjectID) MarshalText

func (id ObjectID) MarshalText() ([]byte, error)

MarshalText writes the hexadecimal form, so an ObjectID can key a map that encoding/json writes.

func (ObjectID) String

func (id ObjectID) String() string

String renders id as ObjectID("…") for a log line or a test failure.

func (ObjectID) Timestamp

func (id ObjectID) Timestamp() time.Time

Timestamp returns the creation time id carries, to the second, in UTC.

func (*ObjectID) UnmarshalJSON

func (id *ObjectID) UnmarshalJSON(data []byte) error

UnmarshalJSON reads a JSON string holding the hexadecimal form, or the extended-JSON object {"$oid": "…"}. The empty string reads as NilObjectID, and a JSON null leaves id unchanged.

func (*ObjectID) UnmarshalText

func (id *ObjectID) UnmarshalText(text []byte) error

UnmarshalText reads the hexadecimal form MarshalText writes.

type Regex

type Regex struct {
	// Pattern is the expression.
	Pattern string
	// Options are the single-letter flags, such as "i" or "imx".
	Options string
}

Regex is a BSON regular expression: a pattern and its options. Neither may contain a NUL byte. The codec writes the options in alphabetical order, as the specification requires, whatever order they were given in.

func (Regex) Equal

func (r Regex) Equal(other Regex) bool

Equal reports whether r and other have the same pattern and options.

func (Regex) IsZero

func (r Regex) IsZero() bool

IsZero reports whether r is the empty regex — what omitempty leaves out.

func (Regex) String

func (r Regex) String() string

String renders r for a log line or a test failure.

type Symbol

type Symbol string

Symbol is the deprecated BSON symbol, read and written as a distinct type so a round trip does not turn it into a string.

type Timestamp

type Timestamp struct {
	// T is the seconds since the Unix epoch.
	T uint32
	// I is the increment, ordering operations within the same second.
	I uint32
}

Timestamp is the BSON replication timestamp: seconds since the epoch and an ordinal within the second. It is not a date; encode a time.Time for one.

func (Timestamp) After

func (t Timestamp) After(other Timestamp) bool

After reports whether t is later than other.

func (Timestamp) Before

func (t Timestamp) Before(other Timestamp) bool

Before reports whether t is earlier than other.

func (Timestamp) Compare

func (t Timestamp) Compare(other Timestamp) int

Compare returns -1 when t is before other, +1 when it is after, 0 when equal.

func (Timestamp) Equal

func (t Timestamp) Equal(other Timestamp) bool

Equal reports whether t and other are the same timestamp.

func (Timestamp) IsZero

func (t Timestamp) IsZero() bool

IsZero reports whether t is the zero timestamp — what omitempty leaves out.

type Undefined

type Undefined struct{}

Undefined is the deprecated BSON undefined value. It decodes into an interface as Undefined{}, and into a typed target as that type's zero.

Jump to

Keyboard shortcuts

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