Documentation
¶
Overview ¶
Package bson decodes a BSON document (the binary serialization MongoDB stores and that `mongodump` writes to `.bson` files) into a structured tree. It is the document-format complement to mongodb_decode (which dissects the MongoDB *wire protocol* and only shallowly extracts a command name + a few argument fields): this fully, recursively decodes a standalone BSON document — every element type, nested documents and arrays, ObjectId, dates, binary subtypes, regex, timestamps — the way cbor_decode / msgpack_decode handle their formats. An operator pastes the hex of a `.bson` record (mongodump loot, a stored document, a captured payload) and gets the full structure without a MongoDB driver. Pure offline transform; no network or device.
Wrap-vs-native judgement ¶
Native. BSON is a fully public, little-endian, length-prefixed format (bsonspec.org v1.1): a document is an int32 byte-length, a sequence of typed elements (type byte + NUL-terminated name + type-specific value), and a 0x00 terminator. It is a recursive-descent walk over a byte cursor; there is nothing to wrap, and adding go.mongodb.org/mongo-driver/bson as a runtime dependency to decode untrusted bytes is unwarranted. Consistent with internal/cbordecode, internal/msgpack, and internal/protobufdecode owning their parse in-tree.
Verifiable / no confidently-wrong output ¶
Strongest verification class — every element type is gated byte-for-byte against vectors produced by the reference PyMongo `bson` library (double, string, embedded doc, array, binary + subtype, ObjectId, bool, UTC datetime, null, regex, int32, timestamp, int64, decimal128, min/max key, and a nested doc+array). A truncated/malformed document is rejected with an error (never a partial/guessed decode), nesting is depth-capped, and length fields are bounds-checked against the buffer.
Covered / deferred ¶
Covered: all current BSON element types, including Decimal128 (0x13) decoded from its IEEE 754-2008 Binary-Integer-Decimal form to sign + 113-bit coefficient + biased exponent and an exact plain (non-scientific) decimal string, with NaN / ±Infinity surfaced as such (the raw 16 bytes are also kept for traceability). The deprecated DBPointer (0x0C) and Symbol (0x0E) and JavaScript-code-with-scope (0x0F) are decoded structurally.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Result ¶
type Result struct {
Document []*Field `json:"document"`
TotalBytes int `json:"total_bytes"`
TrailingHex string `json:"trailing_bytes_hex,omitempty"`
TrailingCount int `json:"trailing_bytes,omitempty"`
}
Result is the decoded top-level document plus framing metadata.
func Decode ¶
Decode parses the hex of a BSON document (separators and an optional 0x prefix tolerated).
func DecodeBytes ¶
DecodeBytes parses a BSON document from raw bytes.
type Value ¶
type Value struct {
Type string `json:"type"`
Double *float64 `json:"double,omitempty"`
Str *string `json:"string,omitempty"`
Doc []*Field `json:"document,omitempty"`
Array []*Value `json:"array,omitempty"`
BinarySubtype *int `json:"binary_subtype,omitempty"`
BinarySubtypeName string `json:"binary_subtype_name,omitempty"`
BytesHex string `json:"bytes_hex,omitempty"`
ObjectID string `json:"object_id,omitempty"`
Bool *bool `json:"bool,omitempty"`
DateTime string `json:"datetime,omitempty"` // RFC 3339 (UTC)
DateUnixMS *int64 `json:"date_unix_ms,omitempty"`
Int32 *int32 `json:"int32,omitempty"`
Int64 *int64 `json:"int64,omitempty"`
TimestampSeconds *uint32 `json:"timestamp_seconds,omitempty"`
TimestampIncrement *uint32 `json:"timestamp_increment,omitempty"`
RegexPattern *string `json:"regex_pattern,omitempty"`
RegexOptions *string `json:"regex_options,omitempty"`
Code *string `json:"code,omitempty"`
Symbol *string `json:"symbol,omitempty"`
Decimal128Hex string `json:"decimal128_hex,omitempty"`
Decimal128 string `json:"decimal128,omitempty"` // plain value, or NaN / ±Infinity
Decimal128Coefficient string `json:"decimal128_coefficient,omitempty"`
Decimal128Exponent *int `json:"decimal128_exponent,omitempty"`
Note string `json:"note,omitempty"`
}
Value is one decoded BSON value.