吕君喜 406d880ac6 first | 3 년 전 | |
---|---|---|
.. | ||
browser_build | 3 년 전 | |
dist | 3 년 전 | |
lib | 3 년 전 | |
HISTORY.md | 3 년 전 | |
LICENSE.md | 3 년 전 | |
README.md | 3 년 전 | |
bower.json | 3 년 전 | |
index.js | 3 년 전 | |
package.json | 3 년 전 |
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.
This browser version of the BSON parser is compiled using rollup and the current version is pre-compiled in the dist
directory.
This is the default BSON parser, however, there is a C++ Node.js addon version as well that does not support the browser. It can be found at mongod-js/bson-ext.
Think you've found a bug? Want to see a new feature in bson
? Please open a case in our issue management tool, JIRA:
Bug reports in JIRA for all driver projects (i.e. NODE, PYTHON, CSHARP, JAVA) and the Core Server (i.e. SERVER) project are public.
To build a new version perform the following operations:
npm install
npm run build
A simple example of how to use BSON in the browser:
<script src="./dist/bson.js"></script>
<script>
function start() {
// Get the Long type
const Long = BSON.Long;
// Serialize document
const doc = { long: Long.fromNumber(100) }
// Serialize a document
const data = BSON.serialize(doc)
// De serialize it again
const doc_2 = BSON.deserialize(data)
}
</script>
A simple example of how to use BSON in Node.js
:
const BSON = require('bson');
const Long = BSON.Long;
const doc = { long: Long.fromNumber(100) };
// Serialize a document
const data = BSON.serialize(doc);
console.log('data:', data);
// Deserialize the resulting Buffer
const doc_2 = BSON.deserialize(data);
console.log('doc_2:', doc_2);
npm install bson
object
Sets the size of the internal serialization buffer.
Buffer
Serialize a Javascript object.
Number
Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.
Object
Deserialize data as BSON.
Number
Calculate the bson size for a passed in Javascript object.
Number
Deserialize stream data as BSON documents.
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 into the output JSON string for readability purposes. | |
[options] | object |
Optional settings | |
[options.relaxed] | boolean |
true |
Enabled Extended JSON's relaxed mode |
Converts a BSON document to an Extended JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
Example
const { EJSON } = require('bson');
const Int32 = require('mongodb').Int32;
const doc = { int32: new Int32(10) };
// prints '{"int32":{"$numberInt":"10"}}'
console.log(EJSON.stringify(doc, { relaxed: false }));
// prints '{"int32":10}'
console.log(EJSON.stringify(doc));
Param | Type | Description |
---|---|---|
bson | object |
The object to serialize |
[options] | object |
Optional settings passed to the stringify function |
Serializes an object to an Extended JSON string, and reparse it as a JavaScript object.
Param | Type | Description |
---|---|---|
ejson | object |
The Extended JSON object to deserialize |
[options] | object |
Optional settings passed to the parse method |
Deserializes an Extended JSON object into a plain JavaScript object with native/BSON types
Param | Type | Description |
---|---|---|
size | number |
The desired size for the internal serialization buffer |
Sets the size of the internal serialization buffer.
Param | Type | Default | Description |
---|---|---|---|
object | Object |
the Javascript object to serialize. | |
[options.checkKeys] | Boolean |
the serializer will check if keys are valid. | |
[options.serializeFunctions] | Boolean |
false |
serialize the javascript functions (default:false). |
[options.ignoreUndefined] | Boolean |
true |
ignore undefined fields (default:true). |
Serialize a Javascript object.
Returns: Buffer
- returns the Buffer object containing the serialized object.
Param | Type | Default | Description |
---|---|---|---|
object | Object |
the Javascript object to serialize. | |
buffer | Buffer |
the Buffer you pre-allocated to store the serialized BSON object. | |
[options.checkKeys] | Boolean |
the serializer will check if keys are valid. | |
[options.serializeFunctions] | Boolean |
false |
serialize the javascript functions (default:false). |
[options.ignoreUndefined] | Boolean |
true |
ignore undefined fields (default:true). |
[options.index] | Number |
the index in the buffer where we wish to start serializing into. |
Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.
Returns: Number
- returns the index pointing to the last written byte in the buffer.
Param | Type | Default | Description |
---|---|---|---|
buffer | Buffer |
the buffer containing the serialized set of BSON documents. | |
[options.evalFunctions] | Object |
false |
evaluate functions in the BSON document scoped to the object deserialized. |
[options.cacheFunctions] | Object |
false |
cache evaluated functions for reuse. |
[options.cacheFunctionsCrc32] | Object |
false |
use a crc32 code for caching, otherwise use the string of the function. |
[options.promoteLongs] | Object |
true |
when deserializing a Long will fit it into a Number if it's smaller than 53 bits |
[options.promoteBuffers] | Object |
false |
when deserializing a Binary will return it as a node.js Buffer instance. |
[options.promoteValues] | Object |
false |
when deserializing will promote BSON values to their Node.js closest equivalent types. |
[options.fieldsAsRaw] | Object |
|
allow to specify if there what fields we wish to return as unserialized raw buffer. |
[options.bsonRegExp] | Object |
false |
return BSON regular expressions as BSONRegExp instances. |
[options.allowObjectSmallerThanBufferSize] | boolean |
false |
allows the buffer to be larger than the parsed BSON object |
Deserialize data as BSON.
Returns: Object
- returns the deserialized Javascript Object.
Param | Type | Default | Description |
---|---|---|---|
object | Object |
the Javascript object to calculate the BSON byte size for. | |
[options.serializeFunctions] | Boolean |
false |
serialize the javascript functions (default:false). |
[options.ignoreUndefined] | Boolean |
true |
ignore undefined fields (default:true). |
Calculate the bson size for a passed in Javascript object.
Returns: Number
- returns the number of bytes the BSON object will take up.
Param | Type | Default | Description |
---|---|---|---|
data | Buffer |
the buffer containing the serialized set of BSON documents. | |
startIndex | Number |
the start index in the data Buffer where the deserialization is to start. | |
numberOfDocuments | Number |
number of documents to deserialize. | |
documents | Array |
an array where to store the deserialized documents. | |
docStartIndex | Number |
the index in the documents array from where to start inserting documents. | |
[options] | Object |
additional options used for the deserialization. | |
[options.evalFunctions] | Object |
false |
evaluate functions in the BSON document scoped to the object deserialized. |
[options.cacheFunctions] | Object |
false |
cache evaluated functions for reuse. |
[options.cacheFunctionsCrc32] | Object |
false |
use a crc32 code for caching, otherwise use the string of the function. |
[options.promoteLongs] | Object |
true |
when deserializing a Long will fit it into a Number if it's smaller than 53 bits |
[options.promoteBuffers] | Object |
false |
when deserializing a Binary will return it as a node.js Buffer instance. |
[options.promoteValues] | Object |
false |
when deserializing will promote BSON values to their Node.js closest equivalent types. |
[options.fieldsAsRaw] | Object |
|
allow to specify if there what fields we wish to return as unserialized raw buffer. |
[options.bsonRegExp] | Object |
false |
return BSON regular expressions as BSONRegExp instances. |
Deserialize stream data as BSON documents.
Returns: Number
- returns the next index in the buffer after deserialization x numbers of documents.
undefined
get converted to null
?The undefined
BSON type has been deprecated for many years, so this library has dropped support for it. Use the ignoreUndefined
option (for example, from the driver ) to instead remove undefined
keys.
This library looks for toBSON()
functions on every path, and calls the toBSON()
function to get the value to serialize.
const BSON = require('bson');
class CustomSerialize {
toBSON() {
return 42;
}
}
const obj = { answer: new CustomSerialize() };
// "{ answer: 42 }"
console.log(BSON.deserialize(BSON.serialize(obj)));