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

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.1K
decreased by-6.31%
Maintainers
1
Weekly downloads
 
Created
Source

MongoDB Extended JSON Library

The MongoDB Extended JSON Library allows you to turn a MongoDB Document into Extended JSON.

https://github.com/mongodb/specifications/blob/master/source/extended-json.rst

Usage with MongoDB driver

To use this library with the driver there are a couple of way.

Override the toJSON of driver BSON types

This lets you override the toJSON functions on the MongoDB driver to ensure JSON.stringify returns an Extended JSON document. Let's look at an example.

var ExtJSON = require('mongodb-extjson');
var mongodb = ExtJSON.extend(require('mongodb'));
var Int32 = mongodb.Int32;

var doc = {
  int32: new Int32(10),
}

console.log(JSON.stringify(doc, null, 2));

Use library directly

Let's look at how we can serialize a document.

Serialize a document

Serialize a document using ExtJSON.prototype.serialize.

var ExtJSON = require('mongodb-extjson');
var mongodb = require('mongodb');
var Int32 = mongodb.Int32;
var extJSON = new ExtJSON(mongodb);

var doc = {
  int32: new Int32(10),
}

console.log(ExtJSON.serialize(doc, null, 2));

Deserialize a document

Serialize a document using ExtJSON.prototype.deseralize(string, options). The method supports the option strict.

strict = true, will return BSON type objects for all values.
strict = false, will attempt to return native JS types where possible.
var ExtJSON = require('mongodb-extjson');
var mongodb = require('mongodb');
var Int32 = mongodb.Int32;
var extJSON = new ExtJSON(mongodb);

var doc = {
  int32: new Int32(10),
}

// Serialize the document
var text = ExtJSON.serialize(doc, null, 2);
// Deserialize using strict mode (returning BSON type objects)
console.dir(ExtJSON.deserialize(text, {strict: true}));
// Deserialize using strict mode (converting to native JS types where possible)
console.dir(ExtJSON.deserialize(text, {strict: true}));

Usage with Builtin BSON types

This allows you to use this library with full types in a Browser

Serialize a document

Serialize a document using ExtJSON.prototype.serialize.

var ExtJSON = require('mongodb-extjson');
var Int32 = ExtJSON.BSON.Int32;
var extJSON = new ExtJSON();

var doc = {
  int32: new Int32(10),
}

console.log(ExtJSON.serialize(doc, null, 2));

Deserialize a document

Serialize a document using ExtJSON.prototype.deseralize(string, options). The method supports the option strict.

strict = true, will return BSON type objects for all values.
strict = false, will attempt to return native JS types where possible.
var ExtJSON = require('mongodb-extjson');
var Int32 = ExtJSON.BSON.Int32;
var extJSON = new ExtJSON();

var doc = {
  int32: new Int32(10),
}

// Serialize the document
var text = ExtJSON.serialize(doc, null, 2);
// Deserialize using strict mode (returning BSON type objects)
console.dir(ExtJSON.deserialize(text, {strict: true}));
// Deserialize using strict mode (converting to native JS types where possible)
console.dir(ExtJSON.deserialize(text, {strict: true}));

Keywords

FAQs

Package last updated on 01 Mar 2017

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