|
1 | 1 | # BSON parser
|
2 | 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 [rollup](https://rollupjs.org/) and the current version is pre-compiled in the `dist` 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). |
| 3 | +BSON is short for "Binary JSON," and is the binary-encoded serialization of JSON-like documents. |
| 4 | +You can learn more about it in [the specification](http://bsonspec.org). |
8 | 5 |
|
9 | 6 | ### Table of Contents
|
10 | 7 | - [Usage](#usage)
|
@@ -32,106 +29,43 @@ npm install
|
32 | 29 | npm run build
|
33 | 30 | ```
|
34 | 31 |
|
35 |
| -### Node (no bundling) |
36 |
| -A simple example of how to use BSON in `Node.js`: |
| 32 | +### Node.js or Bundling Usage |
37 | 33 |
|
38 |
| -```js |
39 |
| -const BSON = require('bson'); |
40 |
| -const Long = BSON.Long; |
| 34 | +When using a bundler or Node.js you can import bson using the package name: |
41 | 35 |
|
42 |
| -// Serialize a document |
43 |
| -const doc = { long: Long.fromNumber(100) }; |
44 |
| -const data = BSON.serialize(doc); |
45 |
| -console.log('data:', data); |
| 36 | +```js |
| 37 | +import { BSON, EJSON, ObjectId } from 'bson'; |
| 38 | +// or: |
| 39 | +// const { BSON, EJSON, ObjectId } = require('bson'); |
46 | 40 |
|
47 |
| -// Deserialize the resulting Buffer |
48 |
| -const doc_2 = BSON.deserialize(data); |
49 |
| -console.log('doc_2:', doc_2); |
| 41 | +const bytes = BSON.serialize({ _id: new ObjectId() }); |
| 42 | +console.log(bytes); |
| 43 | +const doc = BSON.deserialize(bytes); |
| 44 | +console.log(EJSON.stringify(doc)); |
| 45 | +// {"_id":{"$oid":"..."}} |
50 | 46 | ```
|
51 | 47 |
|
52 |
| -### Browser (no bundling) |
| 48 | +### Browser Usage |
53 | 49 |
|
54 |
| -If you are not using a bundler like webpack, you can include `dist/bson.bundle.js` using a script tag. It includes polyfills for built-in node types like `Buffer`. |
| 50 | +If you are working directly in the browser without a bundler please use the `.mjs` bundle like so: |
55 | 51 |
|
56 | 52 | ```html
|
57 |
| -<script src="./dist/bson.bundle.js"></script> |
58 |
| - |
59 |
| -<script> |
60 |
| - function start() { |
61 |
| - // Get the Long type |
62 |
| - const Long = BSON.Long; |
63 |
| -
|
64 |
| - // Serialize a document |
65 |
| - const doc = { long: Long.fromNumber(100) } |
66 |
| - const data = BSON.serialize(doc); |
67 |
| - console.log('data:', data); |
68 |
| -
|
69 |
| - // De serialize it again |
70 |
| - const doc_2 = BSON.deserialize(data); |
71 |
| - console.log('doc_2:', doc_2); |
72 |
| - } |
| 53 | +<script type="module"> |
| 54 | + import { BSON, EJSON, ObjectId } from './lib/bson.mjs'; |
| 55 | +
|
| 56 | + const bytes = BSON.serialize({ _id: new ObjectId() }); |
| 57 | + console.log(bytes); |
| 58 | + const doc = BSON.deserialize(bytes); |
| 59 | + console.log(EJSON.stringify(doc)); |
| 60 | + // {"_id":{"$oid":"..."}} |
73 | 61 | </script>
|
74 | 62 | ```
|
75 | 63 |
|
76 |
| -### Using webpack |
77 |
| - |
78 |
| -If using webpack, you can use your normal import/require syntax of your project to pull in the `bson` library. |
79 |
| - |
80 |
| -ES6 Example: |
81 |
| - |
82 |
| -```js |
83 |
| -import { Long, serialize, deserialize } from 'bson'; |
84 |
| - |
85 |
| -// Serialize a document |
86 |
| -const doc = { long: Long.fromNumber(100) }; |
87 |
| -const data = serialize(doc); |
88 |
| -console.log('data:', data); |
89 |
| - |
90 |
| -// De serialize it again |
91 |
| -const doc_2 = deserialize(data); |
92 |
| -console.log('doc_2:', doc_2); |
93 |
| -``` |
94 |
| - |
95 |
| -ES5 Example: |
96 |
| - |
97 |
| -```js |
98 |
| -const BSON = require('bson'); |
99 |
| -const Long = BSON.Long; |
100 |
| - |
101 |
| -// Serialize a document |
102 |
| -const doc = { long: Long.fromNumber(100) }; |
103 |
| -const data = BSON.serialize(doc); |
104 |
| -console.log('data:', data); |
105 |
| - |
106 |
| -// Deserialize the resulting Buffer |
107 |
| -const doc_2 = BSON.deserialize(data); |
108 |
| -console.log('doc_2:', doc_2); |
109 |
| -``` |
110 |
| - |
111 |
| -Depending on your settings, webpack will under the hood resolve to one of the following: |
112 |
| - |
113 |
| -- `dist/bson.browser.esm.js` If your project is in the browser and using ES6 modules (Default for `webworker` and `web` targets) |
114 |
| -- `dist/bson.browser.umd.js` If your project is in the browser and not using ES6 modules |
115 |
| -- `dist/bson.esm.js` If your project is in Node.js and using ES6 modules (Default for `node` targets) |
116 |
| -- `lib/bson.js` (the normal include path) If your project is in Node.js and not using ES6 modules |
117 |
| - |
118 |
| -For more information, see [this page on webpack's `resolve.mainFields`](https://webpack.js.org/configuration/resolve/#resolvemainfields) and [the `package.json` for this project](./package.json#L52) |
119 |
| - |
120 |
| -### Usage with Angular |
121 |
| - |
122 |
| -Starting with Angular 6, Angular CLI removed the shim for `global` and other node built-in variables (original comment [here](https://github.com/angular/angular-cli/issues/9827#issuecomment-386154063)). If you are using BSON with Angular, you may need to add the following shim to your `polyfills.ts` file: |
123 |
| - |
124 |
| -```js |
125 |
| -// In polyfills.ts |
126 |
| -(window as any).global = window; |
127 |
| -``` |
128 |
| - |
129 |
| -- [Original Comment by Angular CLI](https://github.com/angular/angular-cli/issues/9827#issuecomment-386154063) |
130 |
| -- [Original Source for Solution](https://stackoverflow.com/a/50488337/4930088) |
131 |
| - |
132 | 64 | ## Installation
|
133 | 65 |
|
134 |
| -`npm install bson` |
| 66 | +```sh |
| 67 | +npm install bson |
| 68 | +``` |
135 | 69 |
|
136 | 70 | ## Documentation
|
137 | 71 |
|
|
0 commit comments