npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongodb-stitch-extjson

v1.0.0

Published

MongoDB Extended JSON library for Stitch

Downloads

16

Readme

MongoDB Stitch Extended JSON Library

The MongoDB Extended JSON Library allows you to convert MongoDB documents to Extended JSON, and vice versa. See the Extended JSON specification here.

Usage with MongoDB Driver

This library can be used along with the MongoDB driver for Node.js to convert MongoDB documents to extended JSON form.

Serialize a document

Serialize a document using 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.

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}));

Usage with MongoDB BSON Library

Our js-bson library is included as a dependency and used by default for Javascript representations of BSON types. See the next section for instructions on using it with a different BSON library.

Serialize a document

This works identically to the previous serialize example, but does not require including the MongoDB driver. The BSON types are all available under EJSON.BSON.

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));

Deserialize a document

The library also allows converting extended JSON strings to Javascript objects, using BSON type classes defined in js-bson. You can do this using EJSON.parse(string, options).

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.)

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:

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));

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 <script> tag.
  • ejson.esm.js is a rolled up version of the library that is suitable for interoperation with bundlers that work better with ES modules.
  • ejson.browser.esm.js is similar to ejson.esm.js but is ultimately intened for consumers producing browser bundles. It also pulls in any browser specific dependencies/code that may be needed.
  • ejson.browser.umd.js is similar to the source code of this library but is ultimately intened for consumers producing browser bundlers expecting a UMD format. It also pulls in any browser specific dependencies/code that may be needed.