/extended-json

On spec MongoDB Extended JSON parse and stringify

Primary LanguageJavaScriptApache License 2.0Apache-2.0

mongodb-extended-json

MongoDB Extended JSON.

Example

var EJSON = require('mongodb-extended-json');
var BSON = require('bson');

var doc = {
  _id: BSON.ObjectID(),
  last_seen_at: new Date(),
  display_name: undefined
};

console.log('Doc', doc);
// > Doc { _id: 53c2ab5e4291b17b666d742a, last_seen_at: Sun Jul 13 2014 11:53:02 GMT-0400 (EDT), display_name: undefined }

console.log('JSON', JSON.stringify(doc));
// > JSON {"_id":"53c2ab5e4291b17b666d742a","last_seen_at":"2014-07-13T15:53:02.008Z"}

console.log('EJSON', EJSON.stringify(doc));
// > EJSON {"_id":{"$oid":"53c2ab5e4291b17b666d742a"},"last_seen_at":{"$date":1405266782008},"display_name":{"$undefined":true}}

// And likewise, EJSON.parse works just as you would expect.
EJSON.parse('{"_id":{"$oid":"53c2ab5e4291b17b666d742a"},"last_seen_at":{"$date":1405266782008},"display_name":{"$undefined":true}}');
// { _id: 53c2ab5e4291b17b666d742a,
//   last_seen_at: Sun Jul 13 2014 11:53:02 GMT-0400 (EDT),
//  display_name: undefined }

Streams

var request = require('request');
var url = 'https://cdn.rawgit.com/imlucas/mongodb-extended-json/master/test/data.json';
var EJSON = require('./');
var util = require('util');
var es = require('event-stream');

request.get(url)
  .pipe(EJSON.createParseStream('*'))
  .pipe(es.through(function(doc){
    this.emit('data',
      util.format('_id `%s` has timestamp %s\n', doc._id, doc._id.getTimestamp()));
  }))
  .pipe(process.stdout);

// Prints out:
//
// _id `54a1e4c798c1120000bb8b0f` has timestamp Mon Dec 29 2014 18:33:27 GMT-0500 (EST)
// _id `54a1e42f94042000008ac2f3` has timestamp Mon Dec 29 2014 18:30:55 GMT-0500 (EST)
// _id `54a1e3e68f038b0000631fe2` has timestamp Mon Dec 29 2014 18:29:42 GMT-0500 (EST)
// _id `54a1e3bc124968000052af15` has timestamp Mon Dec 29 2014 18:29:00 GMT-0500 (EST)
// _id `54a1e3a8124968000052af14` has timestamp Mon Dec 29 2014 18:28:40 GMT-0500 (EST)
// _id `54a1e2117b93d000002ee9bb` has timestamp Mon Dec 29 2014 18:21:53 GMT-0500 (EST)
// _id `54a1e1d6360382000019c110` has timestamp Mon Dec 29 2014 18:20:54 GMT-0500 (EST)
// _id `54a1e12ea5a5bb0000561dda` has timestamp Mon Dec 29 2014 18:18:06 GMT-0500 (EST)
// _id `54a1e0c14f3dc50000ba1afc` has timestamp Mon Dec 29 2014 18:16:17 GMT-0500 (EST)
// _id `54a1df90edbfd100001943b3` has timestamp Mon Dec 29 2014 18:11:12 GMT-0500 (EST)