Skip to main content

BSON

BSON allows saving precious bytes compared to JSON, while offering the advantages of a schema-less protocol. Consider, for example, a simple value and timestamp payload. The encoded JSON version, {"v":25.367812,"t":1537346756844} counts 33 bytes.

The hexdump of the same message encoded with BSON is:

0000000 1b 00 00 00 09 74 00 ec e0 01 f1 65 01 00 00 01
0000020 76 00 8c 13 5f ed 28 5e 39 40 00

that fits just in 27 bytes.

BSON format​

BSON is a really simple binary format, breaking down the previous example is very easy thanks to BSON simplicity: the first 4 bytes (1b 00 00 00) are the document size header, follows the timestamp marker (09), the timestamp key name (74 00, that is "t"), the timestamp value (5f 48 06 f1 65 01 00 00 as int64), the double value marker (01), the value key name (76 00, that is "v"), the actual value (cd cc cc cc cc 4c 39 40 as 64-bit IEEE 754-2008 floating point) and the end of document marker (00).

Data Orchestrator payload standard fields​

KeyTypeMandatoryDescription
vAny Data Orchestrator typeYesThe value being sent (both properties and datastream)
tUTC datetimeNoExplicit timestamp, if present (optional, datastream only)

Data Orchestrator data types to BSON types​

Data Orchestrator Data TypeBSON TypeSize in Bytes
doubledouble (0x01)8
integerint32 (0x10)4
booleanboolean (0x08)1
longintegerint64 (0x12)8
stringUTF-8 string (0x02)≥ length (encoding dependent)
binaryblobbinary (0x05)length
datetimeUTC datetime (0x09)8
doublearrayArray (0x04)(8 + keysize) × count
integerarrayArray (0x04)(4 + keysize) × count
booleanarrayArray (0x04)(1 + keysize) × count
longintegerarrayArray (0x04)(1 + keysize) × count
stringarrayArray (0x04)depends on count, length, keys length and encoding
binaryblobarrayArray (0x04)depends on count, keys length and length

integer and long integer are signed integer values, double must be a valid number (+inf, NaN, etc... are not supported), variable data types might be subject to size limitations and object aggregations are encoded as embedded documents.