Skip to main content

Documents · BSON

Keep JSON-like documents in a binary form.

@loaders.gl/documents loads and writes BSON documents as JavaScript objects. It is useful when a binary document format is required but the application wants the familiar JSON data model.

Structured data boundaries

Keep the source shape. Choose the application shape.

JSON, delimited text, BSON, and XML describe data differently. loaders.gl keeps those input boundaries explicit while letting applications choose documents, tables, features, or trees for the next stage.

Common API boundary

documentbatchstream

load · parse · write

Application results

Documentnested objects and arrays
Tablerows, columns, or Arrow batches
Featuresgeometry plus properties
Treeformat-specific document structure

A readable interchange format can stay readable at the edge, or become a typed table when that is what the application needs.

The BSON path

Decode binary documents. Work with objects. Encode them again.

The module keeps the representation boundary simple: BSON bytes at the edge, ordinary JavaScript values in the application, and BSON bytes when writing.

Input

BSON binary documents

Decode

MongoDB-compatible JavaScript objects

Application

JSON-like values and nested documents

Output

BSON binary documents

Detailed guide

Loader and writer details

The reference below covers installation, entry points, supported data representation, and the native BSON codec.

From-v3.4

The @loaders.gl/documents module provides support for the BSON format. The BSON format stores arbitrary (loosely structured) data largely equivalent to the textual JSON format.

Installation​

npm install @loaders.gl/core @loaders.gl/documents

The native codec runs in browsers and Node.js without an external BSON dependency.

Loaders and Writers​

Loader / WriterDescription
BSONLoaderLoads BSON binary documents into JSON-like JavaScript objects.
BSONWriterWrites JSON-like JavaScript objects as BSON binary documents.

Attribution​

The codec is an original loaders.gl implementation of the BSON specification. Historical test fixtures retain their upstream notices.

Native value representations​

The codec supports all BSON element types, including deprecated types. It does not provide MongoDB driver classes, Extended JSON parsing, or Decimal128 arithmetic.

BSON typeDecoded JavaScript value
Document / arrayPlain object / array
String, boolean, null, double, int32Corresponding JavaScript value
Int64Number when safe; otherwise bigint. Options can request bigint or a typed scalar.
ObjectIdBSONObjectId; toString() returns 24 lowercase hexadecimal characters.
DateDate when representable; otherwise a date scalar containing int64 milliseconds.
Binary subtype 0Copied Uint8Array
Other binary subtypesbinary scalar with subtype and data; payloads remain opaque.
Decimal128decimal128 scalar with the exact 16 wire bytes.
Timestamptimestamp scalar containing the unsigned 64-bit bit pattern.
Regular expressionregexp scalar with pattern and sorted BSON options.
Code / code with scopecode scalar with source text and optional scope document.
Symbol / DBPointersymbol or dbPointer scalar preserving its payload.
Undefined / MinKey / MaxKeyundefined, minKey, or maxKey scalar.

Create explicit scalars with createBSONValue(). Decoded scalars already carry a private, non-enumerable symbol brand, so a normal document containing a $bsonType key is never mistaken for a scalar. JSON serialization and structured cloning lose this brand; recreate the scalar explicitly afterward. MongoDB class instances are not accepted.

import {BSONObjectId, createBSONValue, encodeBSONSync, parseBSONSync} from '@loaders.gl/documents/bson-codec';

const bytes = encodeBSONSync({
id: new BSONObjectId('0123456789abcdef01234567'),
count: 9007199254740993n,
timestamp: createBSONValue({$bsonType: 'timestamp', value: 42n})
});
const document = parseBSONSync(bytes);

Plain numbers encode as int32 when integral and in range, otherwise as double (including negative zero, infinities, and NaN). Bigints encode as signed int64. Use preserveTypes: true when decoding to retain numeric wire types for re-encoding; without it, a safe int64 or an integral double may later encode as int32. Native regular expressions are accepted for writing but decode into unevaluated scalars.

Embedded BSON in Parquet​

Parquet BSON columns use this same synchronous codec for document encoding and row materialization. Arrow output preserves the encoded bytes in Binary columns; use parseBSONSync() for explicit document decoding. No optional BSON backend is required.