日本語のREADMEはこちらです: README.ja.md
BSON is short for "Binary JSON," and is the binary-encoded serialization of JSON-like documents. You can learn more about it in the specification.
import { BSON } from "https://code4fukui.github.io/BSON/BSON.js";
const object = {
nil: null,
integer: 1,
float: Math.PI,
string: "Hello, world!",
binary: Uint8Array.from([1, 2, 3]),
array: [10, 20, 30],
map: { foo: "bar" },
timestampExt: new Date(),
};
const encoded = BSON.encode(object);
console.log(encoded);
const obj = BSON.decode(encoded);
console.log(obj);Think you've found a bug? Want to see a new feature in bson? Please open a case in our issue management tool, JIRA:
- Create an account and login: jira.mongodb.org
- Navigate to the NODE project: jira.mongodb.org/browse/NODE
- Click Create Issue - Please provide as much information as possible about the issue and how to reproduce it.
Bug reports in JIRA for the NODE driver project are public.
To build a new version perform the following operations:
npm install
npm run build
When using a bundler or Node.js you can import bson using the package name:
import { BSON, EJSON, ObjectId } from 'bson';
// or:
// const { BSON, EJSON, ObjectId } = require('bson');
const bytes = BSON.serialize({ _id: new ObjectId() });
console.log(bytes);
const doc = BSON.deserialize(bytes);
console.log(EJSON.stringify(doc));
// {"_id":{"$oid":"..."}}If you are working directly in the browser without a bundler please use the .mjs bundle like so:
<script type="module">
import { BSON, EJSON, ObjectId } from './lib/bson.mjs';
const bytes = BSON.serialize({ _id: new ObjectId() });
console.log(bytes);
const doc = BSON.deserialize(bytes);
console.log(EJSON.stringify(doc));
// {"_id":{"$oid":"..."}}
</script>npm install bsonOnly the following version combinations with the MongoDB Node.js Driver are considered stable.
bson@1.x |
bson@4.x |
bson@5.x |
bson@6.x |
bson@7.x |
|
|---|---|---|---|---|---|
mongodb@7.x |
N/A | N/A | N/A | N/A | ✓ |
mongodb@6.x |
N/A | N/A | N/A | ✓ | N/A |
mongodb@5.x |
N/A | N/A | ✓ | N/A | N/A |
mongodb@4.x |
N/A | ✓ | N/A | N/A | N/A |
mongodb@3.x |
✓ | N/A | N/A | N/A | N/A |
| Param | Type | Default | Description |
|---|---|---|---|
| text | string |
||
| [options] | object |
Optional settings | |
| [options.relaxed] | boolean |
true |
Attempt to return native JS types where possible, rather than BSON types (if true) |
Parse an Extended JSON string, constructing the JavaScript value or object described by that string.
Example
const { EJSON } = require('bson');
const text = '{ "int32": { "$numberInt": "10" } }';
// prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } }
console.log(EJSON.parse(text, { relaxed: false }));
// prints { int32: 10 }
console.log(EJSON.parse(text));| Param | Type | Default | Description |
|---|---|---|---|
| value | object |
The value to convert to extended JSON | |
| [replacer] | function | array |
A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string | |
| [space] | string | number |
A String or Number object that's used to insert white space |
MIT License — see LICENSE.