root / HServer / 00.Server / 00.Program / node_modules / bson / README.md
이력 | 보기 | 이력해설 | 다운로드 (7.26 KB)
1 |
# BSON parser |
---|---|
2 |
|
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](http://bsonspec.org). |
4 |
|
5 |
This browser version of the BSON parser is compiled using [webpack](https://webpack.js.org/) and the current version is pre-compiled in the `browser_build` directory. |
6 |
|
7 |
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](https://github.com/mongodb-js/bson-ext). |
8 |
|
9 |
## Usage |
10 |
|
11 |
To build a new version perform the following operations: |
12 |
|
13 |
``` |
14 |
npm install |
15 |
npm run build |
16 |
``` |
17 |
|
18 |
A simple example of how to use BSON in the browser: |
19 |
|
20 |
```html |
21 |
<script src="./browser_build/bson.js"></script> |
22 |
|
23 |
<script> |
24 |
function start() { |
25 |
// Get the Long type |
26 |
var Long = BSON.Long; |
27 |
// Create a bson parser instance |
28 |
var bson = new BSON(); |
29 |
|
30 |
// Serialize document |
31 |
var doc = { long: Long.fromNumber(100) } |
32 |
|
33 |
// Serialize a document |
34 |
var data = bson.serialize(doc) |
35 |
// De serialize it again |
36 |
var doc_2 = bson.deserialize(data) |
37 |
} |
38 |
</script> |
39 |
``` |
40 |
|
41 |
A simple example of how to use BSON in `Node.js`: |
42 |
|
43 |
```js |
44 |
// Get BSON parser class |
45 |
var BSON = require('bson') |
46 |
// Get the Long type |
47 |
var Long = BSON.Long; |
48 |
// Create a bson parser instance |
49 |
var bson = new BSON(); |
50 |
|
51 |
// Serialize document |
52 |
var doc = { long: Long.fromNumber(100) } |
53 |
|
54 |
// Serialize a document |
55 |
var data = bson.serialize(doc) |
56 |
console.log('data:', data) |
57 |
|
58 |
// Deserialize the resulting Buffer |
59 |
var doc_2 = bson.deserialize(data) |
60 |
console.log('doc_2:', doc_2) |
61 |
``` |
62 |
|
63 |
## Installation |
64 |
|
65 |
`npm install bson` |
66 |
|
67 |
## API |
68 |
|
69 |
### BSON types |
70 |
|
71 |
For all BSON types documentation, please refer to the following sources: |
72 |
* [MongoDB BSON Type Reference](https://docs.mongodb.com/manual/reference/bson-types/) |
73 |
* [BSON Spec](https://bsonspec.org/) |
74 |
|
75 |
### BSON serialization and deserialiation |
76 |
|
77 |
**`new BSON()`** - Creates a new BSON serializer/deserializer you can use to serialize and deserialize BSON. |
78 |
|
79 |
#### BSON.serialize |
80 |
|
81 |
The BSON `serialize` method takes a JavaScript object and an optional options object and returns a Node.js Buffer. |
82 |
|
83 |
* `BSON.serialize(object, options)` |
84 |
* @param {Object} object the JavaScript object to serialize. |
85 |
* @param {Boolean} [options.checkKeys=false] the serializer will check if keys are valid. |
86 |
* @param {Boolean} [options.serializeFunctions=false] serialize the JavaScript functions. |
87 |
* @param {Boolean} [options.ignoreUndefined=true] |
88 |
* @return {Buffer} returns a Buffer instance. |
89 |
|
90 |
#### BSON.serializeWithBufferAndIndex |
91 |
|
92 |
The BSON `serializeWithBufferAndIndex` method takes an object, a target buffer instance and an optional options object and returns the end serialization index in the final buffer. |
93 |
|
94 |
* `BSON.serializeWithBufferAndIndex(object, buffer, options)` |
95 |
* @param {Object} object the JavaScript object to serialize. |
96 |
* @param {Buffer} buffer the Buffer you pre-allocated to store the serialized BSON object. |
97 |
* @param {Boolean} [options.checkKeys=false] the serializer will check if keys are valid. |
98 |
* @param {Boolean} [options.serializeFunctions=false] serialize the JavaScript functions. |
99 |
* @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields. |
100 |
* @param {Number} [options.index=0] the index in the buffer where we wish to start serializing into. |
101 |
* @return {Number} returns the index pointing to the last written byte in the buffer. |
102 |
|
103 |
#### BSON.calculateObjectSize |
104 |
|
105 |
The BSON `calculateObjectSize` method takes a JavaScript object and an optional options object and returns the size of the BSON object. |
106 |
|
107 |
* `BSON.calculateObjectSize(object, options)` |
108 |
* @param {Object} object the JavaScript object to serialize. |
109 |
* @param {Boolean} [options.serializeFunctions=false] serialize the JavaScript functions. |
110 |
* @param {Boolean} [options.ignoreUndefined=true] |
111 |
* @return {Buffer} returns a Buffer instance. |
112 |
|
113 |
#### BSON.deserialize |
114 |
|
115 |
The BSON `deserialize` method takes a Node.js Buffer and an optional options object and returns a deserialized JavaScript object. |
116 |
|
117 |
* `BSON.deserialize(buffer, options)` |
118 |
* @param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized. |
119 |
* @param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse. |
120 |
* @param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function. |
121 |
* @param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits |
122 |
* @param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a Node.js Buffer instance. |
123 |
* @param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types. |
124 |
* @param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer. |
125 |
* @param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances. |
126 |
* @return {Number} returns the next index in the buffer after deserialization **x** numbers of documents. |
127 |
|
128 |
#### BSON.deserializeStream |
129 |
|
130 |
The BSON `deserializeStream` method takes a Node.js Buffer, `startIndex` and allow more control over deserialization of a Buffer containing concatenated BSON documents. |
131 |
|
132 |
* `BSON.deserializeStream(buffer, startIndex, numberOfDocuments, documents, docStartIndex, options)` |
133 |
* @param {Buffer} buffer the buffer containing the serialized set of BSON documents. |
134 |
* @param {Number} startIndex the start index in the data Buffer where the deserialization is to start. |
135 |
* @param {Number} numberOfDocuments number of documents to deserialize. |
136 |
* @param {Array} documents an array where to store the deserialized documents. |
137 |
* @param {Number} docStartIndex the index in the documents array from where to start inserting documents. |
138 |
* @param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized. |
139 |
* @param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse. |
140 |
* @param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function. |
141 |
* @param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits |
142 |
* @param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a Node.js Buffer instance. |
143 |
* @param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types. |
144 |
* @param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer. |
145 |
* @param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances. |
146 |
* @return {Object} returns the deserialized JavaScript Object. |
147 |
|
148 |
## FAQ |
149 |
|
150 |
#### Why does `undefined` get converted to `null`? |
151 |
|
152 |
The `undefined` BSON type has been [deprecated for many years](http://bsonspec.org/spec.html), so this library has dropped support for it. Use the `ignoreUndefined` option (for example, from the [driver](http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect) ) to instead remove `undefined` keys. |