This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(*): make compliant with extended JSON 2.0.0 spec and add corpus …
…tests
- Loading branch information
Showing
54 changed files
with
6,205 additions
and
2,983 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,81 @@ | ||
[![Build Status](https://travis-ci.org/christkv/mongodb-extjson.svg?branch=master)](https://travis-ci.org/christkv/mongodb-extjson) | ||
[![Build Status](https://travis-ci.org/mongodb-js/mongodb-extjson.svg?branch=master)](https://travis-ci.org/mongodb-js/mongodb-extjson) | ||
|
||
# MongoDB Extended JSON Library | ||
|
||
The MongoDB Extended JSON Library allows you to turn a MongoDB Document into Extended JSON. | ||
The MongoDB Extended JSON Library allows you to convert MongoDB documents to Extended JSON, and vice versa. See the Extended JSON specification [here](https://github.com/mongodb/specifications/blob/master/source/extended-json.rst). | ||
|
||
https://github.com/mongodb/specifications/blob/master/source/extended-json.rst | ||
## Usage with MongoDB Driver | ||
This library can be used along with the [MongoDB driver for Node.js](https://github.com/mongodb/node-mongodb-native) to convert MongoDB documents to extended JSON form. | ||
|
||
## Usage with MongoDB driver | ||
To use this library with the driver there are a couple of way. | ||
### 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify).) | ||
|
||
### 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. | ||
`options` currently supports a single option, `relaxed`; with `options = {relaxed: true}`, the returned object will be in the more readable "relaxed" extended JSON format. | ||
|
||
```js | ||
var ExtJSON = require('mongodb-extjson'); | ||
var mongodb = ExtJSON.extend(require('mongodb')); | ||
var Int32 = mongodb.Int32; | ||
let EJSON = require('mongodb-extjson'), | ||
Int32 = require('mongodb').Int32; | ||
|
||
var doc = { int32: new Int32(10) }; | ||
|
||
var doc = { | ||
int32: new Int32(10), | ||
} | ||
// prints '{"int32":{"$numberInt":"10"}}' | ||
console.log(EJSON.stringify(doc)); | ||
|
||
console.log(JSON.stringify(doc, null, 2)); | ||
// prints '{"int32":10}' | ||
console.log(EJSON.stringify(doc, {relaxed: true})); | ||
``` | ||
|
||
### Use library directly | ||
Let's look at how we can serialize a document. | ||
## Usage with MongoDB BSON Library | ||
Our [js-bson](https://github.com/mongodb/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 | ||
Serialize a document using `ExtJSON.prototype.stringify`. | ||
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. | ||
|
||
```js | ||
var ExtJSON = require('mongodb-extjson'); | ||
var mongodb = require('mongodb'); | ||
var Int32 = mongodb.Int32; | ||
var extJSON = new ExtJSON(mongodb); | ||
|
||
var doc = { | ||
int32: new Int32(10), | ||
} | ||
let EJSON = require('mongodb-extjson'), | ||
Int32 = EJSON.BSON.Int32; | ||
|
||
var doc = { int32: new Int32(10) }; | ||
|
||
console.log(extJSON.stringify(doc, null, 2)); | ||
// prints '{"int32":{"$numberInt":"10"}}' | ||
console.log(EJSON.stringify(doc)); | ||
``` | ||
|
||
### Deserialize a document | ||
Serialize a document using `ExtJSON.prototype.parse(string, options)`. The method supports the option `strict`. | ||
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)`. | ||
|
||
``` | ||
strict = true, will return BSON type objects for all values. | ||
strict = false, will attempt to return native JS types where possible. | ||
``` | ||
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.) | ||
|
||
```js | ||
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.stringify(doc, null, 2); | ||
// Deserialize using strict mode (returning BSON type objects) | ||
console.dir(extJSON.parse(text, {strict: true})); | ||
// Deserialize using strict mode (converting to native JS types where possible) | ||
console.dir(extJSON.parse(text, {strict: true})); | ||
``` | ||
let EJSON = require('mongodb-extjson'); | ||
|
||
## 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.stringify`. | ||
|
||
```js | ||
var ExtJSON = require('mongodb-extjson'); | ||
var Int32 = ExtJSON.BSON.Int32; | ||
var extJSON = new ExtJSON(); | ||
var text = '{"int32":{"$numberInt":"10"}}'; | ||
|
||
var doc = { | ||
int32: new Int32(10), | ||
} | ||
// prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } } | ||
console.log(EJSON.parse(text)); | ||
|
||
console.log(extJSON.stringify(doc, null, 2)); | ||
// prints { int32: 10 } | ||
console.log(EJSON.parse(text, {strict: false})); | ||
``` | ||
|
||
### Deserialize a document | ||
Serialize a document using `ExtJSON.prototype.parse(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. | ||
``` | ||
## 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](https://www.npmjs.com/package/bson-ext). For example: | ||
|
||
```js | ||
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.stringify(doc, null, 2); | ||
// Deserialize using strict mode (returning BSON type objects) | ||
console.dir(extJSON.parse(text, {strict: true})); | ||
// Deserialize using strict mode (converting to native JS types where possible) | ||
console.dir(extJSON.parse(text, {strict: true})); | ||
``` | ||
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)); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
var ExtJSON = require('./lib/ext_json'); | ||
ExtJSON.BSON = require('./lib/bson'); | ||
|
||
module.exports = ExtJSON; |
Oops, something went wrong.