Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongodb-extjson

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongodb-extjson

MongoDB Extended JSON library

  • 2.1.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
950
decreased by-30.2%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 15 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc