MongoDB Extended JSON library for Stitch
npm install mongodb-stitch-extjson
The MongoDB Extended JSON Library allows you to convert MongoDB documents to Extended JSON, and vice versa. See the Extended JSON specification here.
EJSON.stringify(value, reducer, indents, options). The reducer and indents arguments are analogous to JSON.stringify's replacer and spaces arguments, respectively (see documentation.) options currently supports a single option, relaxed; with options = {relaxed: true}, the returned object will be in the more readable "relaxed" extended JSON format.
``js
let EJSON = require('mongodb-stitch-extjson'),
Int32 = require('mongodb').Int32;
var doc = { int32: new Int32(10) };
// prints '{"int32":{"$numberInt":"10"}}'
console.log(EJSON.stringify(doc));
// prints '{"int32":10}'
console.log(EJSON.stringify(doc, {relaxed: true}));
`
`js
let EJSON = require('mongodb-stitch-extjson'),
Int32 = EJSON.BSON.Int32;
var doc = { int32: new Int32(10) };
// prints '{"int32":{"$numberInt":"10"}}'
console.log(EJSON.stringify(doc));
`
. This method supports the option
strict. By default, strict is true; if strict is set to false, the parser will attempt to return native JS types where possible, rather than BSON types (i.e. return a Number instead of a BSON.Int32 object, etc.) `js
let EJSON = require('mongodb-stitch-extjson');var text = '{"int32":{"$numberInt":"10"}}';
// prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } }
console.log(EJSON.parse(text));
// prints { int32: 10 }
console.log(EJSON.parse(text, {strict: false}));
`
Usage With Other BSON Parsers
Although we include the pure Javascript BSON parser by default, you can also use a different BSON parser with this library, such as bson-ext. For example:
`js
let EJSON = require('mongodb-stitch-extjson'),
BSON = require('bson-ext'),
Int32 = BSON.Int32;// set BSON module to be bson-ext
EJSON.setBSONModule(BSON);
var doc = { int32: new Int32(10) };
// prints '{"int32":{"$numberInt":"10"}}'
console.log(EJSON.stringify(doc));
var text = '{"int32":{"$numberInt":"10"}}';
// prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } }
console.log(EJSON.parse(text));
`[travis_img]: https://api.travis-ci.org/edaniels/mongodb-extjson.svg?branch=master
[travis_url]: https://travis-ci.org/edaniels/mongodb-extjson
[npm_img]: https://img.shields.io/npm/v/mongodb-extjson.svg
[npm_url]: https://www.npmjs.org/package/mongodb-extjson
FAQ
#### What are the various files in dist?
*
ejson.bundle.js is a bundled up version of the library that is suitable for inclusion in an HTML page via a