wire / wire-format / bson-document

BSON document structure

BSON (Binary JSON) documents are a sequence of typed elements followed by a null terminator. Each element: 1-byte type code, NUL-terminated field name (cstring), then the value (encoding depends on type). The document is prefixed by a 4-byte little-endian total size (including itself and the terminator).

wire-format kind wire-format status standard verification verified tier B wire-format@1

aka: BSON document structure

wire format

spec: BSON specification (bsonspec.org)

BSON (Binary JSON) documents are a sequence of typed elements followed by a null terminator. Each element: 1-byte type code, NUL-terminated field name (cstring), then the value (encoding depends on type). The document is prefixed by a 4-byte little-endian total size (including itself and the terminator).

fieldsizemeaning
Document size4 bytesTotal size of the document in bytes, including this field and the trailing 0x00. Little-endian.
Element (repeated)variable1-byte type + cstring field name + value (type-specific encoding).
Terminator1 byte0x00.

example:

16 00 00 00 02 6e 61 6d 65 00 06 00 00 00 4a 6f 68 6e 00 00

Document {"name": "John"}: size 22, type 0x02 string, field 'name', value 'John'


provenance

agent: curl -H 'accept: application/json' wire.phall.io/wire-format/bson-document or /wire-format/bson-document.json