Detalhes do pacote

mongodb-extjson

mongodb-js8.1kApache-2.0depreciado2.1.5

This library has been merged into the 4.x version of the bson package, and is no longer being maintained as a standalone package

MongoDB Extended JSON library

mongodb, json, extended, extjson

readme (leia-me)

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

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

2.1.5 (2020-07-15)

Bug Fixes

  • DBRef missing ref key when extending lib (60f4f16)
  • EJSON Double precision and update spec tests (83c4213)

2.1.4 (2018-05-31)

Bug Fixes

  • binary: generate valid base64 for non-Buffer types (44afbb8)
  • parsing: support encoding and decoding RegExps (06691f6)

2.1.3 (2018-05-01)

Bug Fixes

  • date: fix date decode in relaxed mode (#13) (dc416aa)

2.1.2 (2018-04-06)

Features

  • bson: bump dependency on js-bson to track 2.x (f00bc9f)

2.1.1 (2018-04-01)

Features

  • browser: Added browser parameter in package.json pointing to transpiled dist (97b0feb)
  • double: supported relaxed mode for double parsing (0ee5016)
  • int32: support relaxed parsing of int32 types (c8513cb)
  • long: support relaxed parsing of long types (ac52ae5)
  • parsing: support relaxed parsing of extended json (92e06d3)

2.0.0 (2017-11-20)

Features

  • make compliant with extended JSON 2.0.0 spec and add corpus tests (7818d61)
  • binary: removing browser buffer support (dc556c8)

BREAKING CHANGES

  • Methods now exist on a global instance of ExtJSON. Whereas previously the library was consumed as such:

    const extJSON = require('mongodb-extjson');
    const EJSON = new extJSON();
    EJSON.parse(...);
    

    it is now consumed thusly:

    const EJSON = require('mongodb-extjson');
    EJSON.parse(...);
    
  • binary: Removes out-of-box support for Binary serialization on web platforms. Users will have to polyfill node Buffer type
  • Library is now compliant with Extended JSON v2.0.0, and as such may not be compatible with v1.0.0 Extended JSON objects.

1.0.5 (2017-05-24)

Bug Fixes

  • parse: properly support null values in parsed strings (b3f88f9), closes #2
  • serialize: properly serialize BSON types when used as values (66fe3d9), closes #1

1.0.4 (2017-03-15)

1.0.3 (2017-03-02)

1.0.2 (2017-03-01)

1.0.1 (2017-03-01)

1.0.0 (2017-03-01)