JSON 19 ๐ Binary JSON Formats โ BSON, MessagePack, and CBOR
The three binary formats are not three implementations of the same idea. They are three different answers to the question of how a JSON-like data model should be encoded in bytes, and the answers diverge on the type system, the traversal model, and the standardisation. BSON is the MongoDB storage format, and it was designed for the database traversal rather than the network compactnessโit adds the types that the JSON lacks, such as the Date and the BinData, and it pays for the field names in the serialized bytes . MessagePack is the compact general-purpose format, and it saves the type information in the type-data or type-length-data prefix, with the smallest representation for each value . CBOR is the IETF standard, the RFC 8949, and it carries the major types with the additional information in the initial byte, including the indefinite-length items and the deterministic encoding rules .
The size comparison is not the simple ranking that the “binary is smaller” intuition suggests. The benchmark of the JSON-compatible binary serialization specifications found that the schema-less formats tend to produce smaller messages than the JSON, with the reductions up to 63%, but the BSON and the CBOR can produce larger bit-strings than the JSON for a subset of the input data, up to 32% larger . The Niels Lohmann comparison measured the BSON at 85.8% to 106.7% of the minified JSON across the four sample files, while the CBOR and the MessagePack stayed at roughly 50% to 88% . The BSON’s overhead is the field names, which the format repeats in every document, and the overhead is the price of the schema-less traversal that the MongoDB requires.
Key point: BSON is the MongoDB storage format with the extended types and the field-name overhead. MessagePack is the compact general-purpose format with the type prefix. CBOR is the IETF standard with the major types and the deterministic encoding. The size depends on the data: MessagePack and CBOR are consistently smaller than the JSON, and BSON is the largest of the three because it repeats the field names.
Why the binary formats exist
The size problem. The JSON is the text format, and the text is the verbose. The keys are repeated, the numbers are the decimal digits, and the whitespace is the padding. The binary formats replace the text with the compact type-tagged bytes, and the size reduction is the mechanism that makes the formats attractive for the network and the storage. The benchmark shows the MessagePack and the CBOR at the roughly 50% to 88% of the minified JSON, and the BSON at the 85% to 107% . The size is the trade-off against the readability and the interoperability.
The type problem. The JSON has the six types, and the applications need the more. The BSON adds the Date, the BinData, the ObjectId, and the other MongoDB-specific types, and the extensions are the mechanism that makes the database storage efficient . The CBOR adds the bignums, the decimal fractions, the tags, and the indefinite-length items, and the extensions are the mechanism that makes the format the general-purpose . The MessagePack stays close to the JSON types, and the closeness is the reason the format is the drop-in replacement .
The traversal problem. The BSON is designed to be traversed easily, and the traversal is the reason it is the primary data representation for the MongoDB . The document is the length-prefixed sequence of the elements, and the length prefix is the mechanism that lets the database skip the document without the full parse. The MessagePack and the CBOR are the sequential formats, and the sequential parse is the mechanism that makes them the network formats rather than the database formats.
The standardisation problem. The CBOR is the IETF standard, the RFC 8949, and the standard is the mechanism that makes the format the interoperable across the implementations and the languages . The MessagePack is the community specification, and the specification is the mechanism that makes the format the practical choice for the broad language support. The BSON is the MongoDB specification, and the specification is the mechanism that makes the format the database-specific.
The determinism problem. The CBOR has the deterministic encoding rules, and the rules are the mechanism that makes the format the canonical for the cryptographic operations . The MessagePack does not have the standard deterministic profile, and the BSON does not either. The determinism is the mechanism that makes the CBOR the choice for the signed and the hashed data.
a. The BSON format and the MongoDB storage
The BSON is the binary-encoded serialization of the JSON-like documents. The document is the length-prefixed sequence of the elements, and each element has the type byte, the field name, and the value . The length prefix is the mechanism that lets the database skip the document without the full parse, and the skip is the reason the BSON is the database format rather than the network format.
// The BSON document structure
// The document is the int32 length, the elements, and the null terminator.
// The element is the type byte, the field name (cstring), and the value.
// The example document:
// { "name": "Ada", "age": 36 }
// The BSON bytes:
// \x1a\x00\x00\x00 // the total length (26 bytes)
// \x02 // the string type
// name\x00 // the field name
// \x04\x00\x00\x00Ada\x00 // the string length + the string
// \x10 // the int32 type
// age\x00 // the field name
// \x24\x00\x00\x00 // the int32 value (36)
// \x00 // the null terminator
The BSON types include the double, the string, the document, the array, the binary data, the ObjectId, the boolean, the Date, the null, the regex, the JavaScript code, the int32, the timestamp, the int64, the decimal128, and the minkey/maxkey . The extended types are the mechanism that makes the format the database storage, and the extensions are the reason the format is not the general-purpose.
The BSON is the schema-less, and the schema-less gives it the flexibility but also the space inefficiency, because the field names are repeated in every document . The field-name overhead is the mechanism that makes the BSON the largest of the three, and the overhead is the trade-off against the database traversal.
b. The MessagePack and the CBOR formats
The MessagePack stores the type information in the serialized data, and each value is the type-data or the type-length-data . The integers have the multiple formats: the positive fixnum for the 0 to 127 in the 1 byte, the negative fixnum for the -32 to -1 in the 1 byte, and the uint8/16/32/64 and the int8/16/32/64 for the larger values . The smallest format is the mechanism that keeps the format compact, and the compactness is the reason the MessagePack is the network format.
// The MessagePack integer encodings
// The positive fixnum: 0XXXXXXX (0 to 127)
// The negative fixnum: 111XXXXX (-32 to -1)
// The uint8: 0xcc XXXXXXXX (0 to 255)
// The uint16: 0xcd XXXXXXXX XXXXXXXX (0 to 65535)
// The uint32: 0xce ... (0 to 4,294,967,295)
// The uint64: 0xcf ... (0 to 18,446,744,073,709,551,615)
// The string is the raw bytes with the length prefix.
// The fixstr: 101XXXXX + the UTF-8 bytes (up to 31 bytes)
// The str8: 0xd9 + the length + the bytes
// The str16: 0xda + the length + the bytes
The MessagePack does not have the string type; the strings are the UTF-8 encoded raw bytes, and the raw type is the mechanism that makes the format the simple . The simplicity is the reason the format is the drop-in replacement for the JSON, and the drop-in is the reason the format is the practical choice for the broad language support.
The CBOR is the IETF standard, and the format is built on the major types. The initial byte is the 3-bit major type and the 5-bit additional information, and the additional information is the mechanism that encodes the length or the value . The major types are the unsigned integer, the negative integer, the byte string, the text string, the array, the map, the tag, and the simple/float. The indefinite-length items are the mechanism that makes the format the streaming-friendly, and the break code is the terminator .
// The CBOR major types
// The major type 0: the unsigned integer
// The major type 1: the negative integer
// The major type 2: the byte string
// The major type 3: the text string
// The major type 4: the array
// The major type 5: the map
// The major type 6: the tag
// The major type 7: the simple value / the float
// The example: the unsigned integer 100
// The major type 0, the additional information 24 (the 1-byte value)
// The bytes: 0x18 0x64
// The example: the text string "Ada"
// The major type 3, the additional information 3 (the length)
// The bytes: 0x63 0x41 0x64 0x61
The CBOR has the deterministic encoding rules, and the rules are the mechanism that makes the format the canonical for the cryptographic operations . The deterministic encoding requires the shortest form for the integers, the shortest form for the floats, and the sorted map keys. The determinism is the mechanism that makes the CBOR the choice for the signed and the hashed data, and the choice is the reason the format is the IETF standard.
The size comparison places the MessagePack and the CBOR at the similar level, and the BSON at the larger level. The Niels Lohmann comparison shows the MessagePack and the CBOR at the roughly 50% to 88% of the minified JSON, and the BSON at the 85% to 107% . The benchmark of the JSON-compatible binary serialization specifications found the BSON and the CBOR can be larger than the JSON for a subset of the data, up to 32% larger, while the MessagePack is consistently smaller . The size is the data-dependent, and the choice is the data-dependent.
Complete Example Session
// ============================================
// PART 1: THE BSON DOCUMENT
// ============================================
// The BSON document is the length-prefixed elements.
// The length prefix lets the database skip the document.
// The field names are repeated in every document.
// ============================================
// PART 2: THE BSON TYPES
// ============================================
// The double, the string, the document, the array.
// The binary data, the ObjectId, the boolean, the Date.
// The null, the regex, the JavaScript code.
// The int32, the timestamp, the int64, the decimal128.
// The minkey and the maxkey.
// ============================================
// PART 3: THE MESSAGEPACK INTEGER
// ============================================
// The positive fixnum: 0 to 127 in 1 byte.
// The negative fixnum: -32 to -1 in 1 byte.
// The uint8: 0 to 255 in 2 bytes.
// The uint16: 0 to 65535 in 3 bytes.
// The uint32: 0 to 4,294,967,295 in 5 bytes.
// The uint64: 0 to 18,446,744,073,709,551,615 in 9 bytes.
// ============================================
// PART 4: THE MESSAGEPACK STRING
// ============================================
// The string is the UTF-8 raw bytes with the length prefix.
// The fixstr: up to 31 bytes.
// The str8: the 0xd9 prefix and the length.
// The str16: the 0xda prefix and the length.
// ============================================
// PART 5: THE CBOR MAJOR TYPES
// ============================================
// The major type 0: the unsigned integer.
// The major type 1: the negative integer.
// The major type 2: the byte string.
// The major type 3: the text string.
// The major type 4: the array.
// The major type 5: the map.
// The major type 6: the tag.
// The major type 7: the simple value / the float.
// ============================================
// PART 6: THE CBOR INTEGER
// ============================================
// The unsigned integer 100:
// 0x18 0x64 (the major type 0, the additional information 24)
// The shortest form is the deterministic encoding.
// ============================================
// PART 7: THE CBOR TEXT STRING
// ============================================
// The text string "Ada":
// 0x63 0x41 0x64 0x61
// The major type 3, the length 3.
// ============================================
// PART 8: THE CBOR INDEFINITE LENGTH
// ============================================
// The indefinite-length array:
// 0x9f ... 0xff
// The 0x9f is the major type 4 with the additional information 31.
// The 0xff is the break code.
// ============================================
// PART 9: THE SIZE COMPARISON
// ============================================
// The MessagePack: 50.5% to 87.9% of the minified JSON.
// The CBOR: 50.5% to 88.0% of the minified JSON.
// The BSON: 85.8% to 106.7% of the minified JSON.
// ============================================
// PART 10: THE CHECKLIST
// ============================================
// [ ] The BSON is the MongoDB storage format.
// [ ] The MessagePack is the compact general-purpose format.
// [ ] The CBOR is the IETF standard.
// [ ] The BSON has the field-name overhead.
// [ ] The MessagePack and the CBOR are the smaller.
// [ ] The CBOR has the deterministic encoding.
The ten parts covered the BSON document, the BSON types, the MessagePack integer, the MessagePack string, the CBOR major types, the CBOR integer, the CBOR text string, the CBOR indefinite length, the size comparison, and the checklist. The through-line is that the three formats are the three answers, and the answers diverge on the type system, the traversal, and the standardisation.
Quick Reference
The format comparison
| Aspect | BSON | MessagePack | CBOR |
|---|---|---|---|
| The standard | The MongoDB spec | The community spec | The RFC 8949 |
| The type system | The extended MongoDB types | The JSON types + the binary | The extended general types |
| The traversal | The length-prefixed | The sequential | The sequential |
| The determinism | No | No | The deterministic encoding |
| The size (Lohmann) | 85.8% to 106.7% | 50.5% to 87.9% | 50.5% to 88.0% |

The BSON types
| Type | Purpose |
|---|---|
| The double | The 64-bit float |
| The string | The UTF-8 string |
| The document | The nested document |
| The array | The nested array |
| The binary data | The binary blob |
| The ObjectId | The 12-byte identifier |
| The Date | The 64-bit millisecond timestamp |
| The decimal128 | The 128-bit decimal |
The MessagePack integer formats
| Format | Range | Bytes |
|---|---|---|
| The positive fixnum | 0 to 127 | 1 |
| The negative fixnum | -32 to -1 | 1 |
| The uint8 | 0 to 255 | 2 |
| The uint16 | 0 to 65535 | 3 |
| The uint32 | 0 to 4,294,967,295 | 5 |
| The uint64 | 0 to 18,446,744,073,709,551,615 | 9 |
The CBOR major types
| Major type | Purpose |
|---|---|
| 0 | The unsigned integer |
| 1 | The negative integer |
| 2 | The byte string |
| 3 | The text string |
| 4 | The array |
| 5 | The map |
| 6 | The tag |
| 7 | The simple value / the float |
Best Practices
โ Do This:
// Use the BSON for the MongoDB storage
const bson = BSON.serialize(document); // โ
// Use the MessagePack for the compact network format
const msgpack = MessagePack.encode(data); // โ
// Use the CBOR for the standardised format
const cbor = CBOR.encode(data); // โ
โ Don’t Do This:
// Use the BSON for the network transfer
// The field names add the overhead. // โ
// Use the MessagePack for the deterministic signing
// The format does not have the standard determinism. // โ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| The BSON is larger than expected | The field names are repeated | Accept the overhead or use the MessagePack |
| The CBOR is larger than the JSON | The data has the high-redundancy textual values | Use the compressed variant |
| The MessagePack is not deterministic | The format has no standard determinism | Use the CBOR |
| The BSON has the 21 MB limit | The bson library limit | Use the alternative library |
Real-World Examples
1. The BSON serialize
BSON.serialize({ name: "Ada" });
2. The BSON deserialize
BSON.deserialize(bytes);
3. The MessagePack encode
MessagePack.encode({ name: "Ada" });
4. The MessagePack decode
MessagePack.decode(bytes);
5. The CBOR encode
CBOR.encode({ name: "Ada" });
6. The CBOR decode
CBOR.decode(bytes);
7. The CBOR deterministic
CBOR.encode(data, { canonical: true });
8. The MessagePack integer
// 100 โ 0x64 (the positive fixnum)
9. The CBOR integer
// 100 โ 0x18 0x64
10. The BSON ObjectId
new ObjectId();
Visual
The Three Formats
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ THE THREE ANSWERS โ
โ โ
โ BSON MessagePack โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ The MongoDB storage โ โ The compact network โ โ
โ โ The extended types โ โ The JSON types โ โ
โ โ The field-name โ โ The type prefix โ โ
โ โ overhead โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ CBOR โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ The IETF standard โ โ
โ โ The major types โ โ
โ โ The determinism โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The Size Comparison
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ THE SIZE (Lohmann) โ
โ โ
โ The minified JSON: 100% โ
โ โ
โ The MessagePack: 50.5% to 87.9% โ
โ The CBOR: 50.5% to 88.0% โ
โ The BSON: 85.8% to 106.7% โ
โ โ
โ The BSON is the largest because of the field names. โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The CBOR Major Types
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ THE CBOR INITIAL BYTE โ
โ โ
โ The 3-bit major type + the 5-bit additional information โ
โ โ
โ 0: the unsigned integer โ
โ 1: the negative integer โ
โ 2: the byte string โ
โ 3: the text string โ
โ 4: the array โ
โ 5: the map โ
โ 6: the tag โ
โ 7: the simple / the float โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| BSON | The MongoDB storage format with the extended types and the field-name overhead |
| MessagePack | The compact general-purpose format with the type prefix |
| CBOR | The IETF standard with the major types and the deterministic encoding |
| The BSON size | 85.8% to 106.7% of the minified JSON |
| The MessagePack size | 50.5% to 87.9% of the minified JSON |
| The CBOR size | 50.5% to 88.0% of the minified JSON |
| The determinism | The CBOR has the deterministic encoding |
Key takeaways:
- The BSON is the MongoDB storage format. It is designed to be traversed easily, and the length-prefixed document is the mechanism that lets the database skip the document without the full parse. The field names are repeated in every document, and the repetition is the reason the BSON is the largest of the three .
- The MessagePack is the compact general-purpose format. It saves the type information in the type-data or the type-length-data prefix, and the smallest format for each value is the mechanism that keeps the format compact. The format is the drop-in replacement for the JSON, and the broad language support is the reason it is the practical choice .
- The CBOR is the IETF standard. The RFC 8949 defines the major types and the additional information, and the indefinite-length items are the mechanism that makes the format the streaming-friendly. The deterministic encoding rules are the mechanism that makes the format the choice for the cryptographic operations .
- The BSON is the largest, and the MessagePack and the CBOR are the smaller. The Lohmann comparison measured the BSON at the 85.8% to 106.7% of the minified JSON, and the MessagePack and the CBOR at the roughly 50% to 88% . The size is the data-dependent, and the BSON’s field-name overhead is the reason it is the largest.
- The BSON and the CBOR can be larger than the JSON for a subset of the data. The benchmark of the JSON-compatible binary serialization specifications found the BSON and the CBOR up to the 32% larger than the JSON for some input, while the MessagePack is consistently smaller . The choice is the data-dependent, not the universal.
Remember: The BSON, the MessagePack, and the CBOR are the three binary formats with the different trade-offs. The BSON is the MongoDB storage with the extended types and the field-name overhead. The MessagePack is the compact general-purpose format with the type prefix. The CBOR is the IETF standard with the major types and the determinism. The size is the data-dependent, and the choice is the data-dependent.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!