The encode path
Serialize a document without flattening its richer scalar types.
Pass ordinary object structures and BSON-aware values to the writer. It encodes BSON directly without an external backend.
Input
JSON-like JavaScript objects and nested values
Output
ArrayBuffer containing one BSON document
Types
Extended BSON scalar values remain available
Options
Native encode and nesting options
Detailed guide
BSONWriter reference
The sections below document installation, usage, native options and encoded value representations.
BSONWriter writes JSON-like JavaScript objects as BSON binary documents.
Installation
BSON decoding and encoding are implemented natively; no bson package is needed.
npm install @loaders.gl/core @loaders.gl/documents
Install @loaders.gl/documents. Its root contains metadata; use flat subpaths for implementations.
Usage
import {encode} from '@loaders.gl/core';
import {BSONWriter} from '@loaders.gl/documents/bson';
const arrayBuffer = await encode(data, BSONWriter);
BSONWriter Options
| Option | Default | Behavior |
|---|---|---|
bson.ignoreUndefined | true | Omit undefined object properties. Set false to encode them as null. Undefined and sparse array slots always encode as null. |
bson.maxDepth | 100 | Maximum document/array nesting, including the root and code scopes. |
Both async encode() and encodeSync() honor these options. Cycles, invalid dates,
int64 values outside their representable range, unsupported objects, and field names
containing null characters are rejected. Regular expressions must use BSON-supported
flags (i, l, m, s, u, x); use a native scalar marker for BSON-only regex flags.
Use an explicit undefined BSON marker to write the deprecated BSON undefined type.
Value representations
See the native BSON value contract.
