Socket
Socket
Sign inDemoInstall

bson

Package Overview
Dependencies
Maintainers
4
Versions
162
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bson - npm Package Compare versions

Comparing version 4.0.0-rc1 to 4.0.0-rc2

lib/extended_json.js

26

lib/binary.js

@@ -277,2 +277,28 @@ 'use strict';

}
/**
* @ignore
*/
toExtendedJSON() {
const base64String = Buffer.isBuffer(this.buffer)
? this.buffer.toString('base64')
: Buffer.from(this.buffer).toString('base64');
const subType = Number(this.sub_type).toString(16);
return {
$binary: {
base64: base64String,
subType: subType.length === 1 ? '0' + subType : subType
}
};
}
/**
* @ignore
*/
static fromExtendedJSON(doc) {
const type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0;
const data = new Buffer(doc.$binary.base64, 'base64');
return new Binary(data, type);
}
}

@@ -279,0 +305,0 @@

7

lib/bson.js

@@ -19,2 +19,3 @@ 'use strict';

const constants = require('./constants');
const EJSON = require('./extended_json');

@@ -39,2 +40,3 @@ // Parts of the parser

*
* @method
* @param {number} size The desired size for the internal serialization buffer

@@ -239,3 +241,6 @@ */

// legacy support
ObjectID: ObjectId
ObjectID: ObjectId,
// Extended JSON
EJSON
});

@@ -25,2 +25,20 @@ 'use strict';

}
/**
* @ignore
*/
toExtendedJSON() {
if (this.scope) {
return { $code: this.code, $scope: this.scope };
}
return { $code: this.code };
}
/**
* @ignore
*/
static fromExtendedJSON(doc) {
return new Code(doc.$code, doc.$scope);
}
}

@@ -27,0 +45,0 @@

@@ -45,2 +45,25 @@ 'use strict';

}
/**
* @ignore
*/
toExtendedJSON() {
let o = {
$ref: this.collection,
$id: this.oid
};
if (this.db) o.$db = this.db;
o = Object.assign(o, this.fields);
return o;
}
/**
* @ignore
*/
static fromExtendedJSON(doc) {
var copy = Object.assign({}, doc);
['$ref', '$id', '$db'].forEach(k => delete copy[k]);
return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
}
}

@@ -47,0 +70,0 @@

@@ -791,3 +791,17 @@ 'use strict';

/**
* @ignore
*/
Decimal128.prototype.toExtendedJSON = function() {
return { $numberDecimal: this.toString() };
};
/**
* @ignore
*/
Decimal128.fromExtendedJSON = function(doc) {
return Decimal128.fromString(doc.$numberDecimal);
};
Object.defineProperty(Decimal128.prototype, '_bsontype', { value: 'Decimal128' });
module.exports = Decimal128;

@@ -32,2 +32,19 @@ 'use strict';

}
/**
* @ignore
*/
toExtendedJSON(options) {
if (options && options.relaxed && isFinite(this.value)) return this.value;
return { $numberDouble: this.value.toString() };
}
/**
* @ignore
*/
static fromExtendedJSON(doc, options) {
return options && options.relaxed
? parseFloat(doc.$numberDouble)
: new Double(parseFloat(doc.$numberDouble));
}
}

@@ -34,0 +51,0 @@

@@ -32,2 +32,17 @@ 'use strict';

}
/**
* @ignore
*/
toExtendedJSON(options) {
if (options && options.relaxed) return this.value;
return { $numberInt: this.value.toString() };
}
/**
* @ignore
*/
static fromExtendedJSON(doc, options) {
return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);
}
}

@@ -34,0 +49,0 @@

'use strict';
const Long = require('long');
/**
* @ignore
*/
Long.prototype.toExtendedJSON = function(options) {
if (options && options.relaxed) return this.toNumber();
return { $numberLong: this.toString() };
};
/**
* @ignore
*/
Long.fromExtendedJSON = function(doc, options) {
const result = Long.fromString(doc.$numberLong);
return options && options.relaxed ? result.toNumber() : result;
};
Object.defineProperty(Long.prototype, '_bsontype', { value: 'Long' });
module.exports = Long;

@@ -12,2 +12,16 @@ 'use strict';

constructor() {}
/**
* @ignore
*/
toExtendedJSON() {
return { $maxKey: 1 };
}
/**
* @ignore
*/
static fromExtendedJSON() {
return new MaxKey();
}
}

@@ -14,0 +28,0 @@

@@ -12,2 +12,16 @@ 'use strict';

constructor() {}
/**
* @ignore
*/
toExtendedJSON() {
return { $minKey: 1 };
}
/**
* @ignore
*/
static fromExtendedJSON() {
return new MinKey();
}
}

@@ -14,0 +28,0 @@

@@ -354,2 +354,17 @@ 'use strict';

}
/**
* @ignore
*/
toExtendedJSON() {
if (this.toHexString) return { $oid: this.toHexString() };
return { $oid: this.toString('hex') };
}
/**
* @ignore
*/
static fromExtendedJSON(doc) {
return new ObjectId(doc.$oid);
}
}

@@ -356,0 +371,0 @@

@@ -41,2 +41,22 @@ 'use strict';

}
/**
* @ignore
*/
toExtendedJSON() {
return { $regularExpression: { pattern: this.pattern, options: this.options } };
}
/**
* @ignore
*/
static fromExtendedJSON(doc) {
return new BSONRegExp(
doc.$regularExpression.pattern,
doc.$regularExpression.options
.split('')
.sort()
.join('')
);
}
}

@@ -43,0 +63,0 @@

@@ -45,2 +45,16 @@ 'use strict';

}
/**
* @ignore
*/
toExtendedJSON() {
return { $symbol: this.value };
}
/**
* @ignore
*/
static fromExtendedJSON(doc) {
return new Symbol(doc.$symbol);
}
}

@@ -47,0 +61,0 @@

@@ -77,2 +77,16 @@ 'use strict';

}
/**
* @ignore
*/
toExtendedJSON() {
return { $timestamp: { t: this.high, i: this.low } };
}
/**
* @ignore
*/
static fromExtendedJSON(doc) {
return new Timestamp(doc.$timestamp.i, doc.$timestamp.t);
}
}

@@ -79,0 +93,0 @@

4

package.json

@@ -14,3 +14,3 @@ {

],
"version": "4.0.0-rc1",
"version": "4.0.0-rc2",
"author": "Christian Amor Kvalheim <christkv@gmail.com>",

@@ -64,3 +64,3 @@ "license": "Apache-2.0",

"scripts": {
"docs": "jsdoc2md --heading-depth 3 --template tools/README.hbs --plugin dmd-clear --files lib/bson.js > README.md",
"docs": "jsdoc2md --heading-depth 3 --template tools/README.hbs --plugin dmd-clear --files lib/bson.js lib/extended_json.js > README.md",
"test": "npm run-script test-node && npm run-script test-browser",

@@ -67,0 +67,0 @@ "test-node": "mocha ./test/node",

@@ -62,2 +62,9 @@ # BSON parser

### Objects
<dl>
<dt><a href="#EJSON">EJSON</a> : <code>object</code></dt>
<dd></dd>
</dl>
### Functions

@@ -86,2 +93,91 @@

<a name="EJSON"></a>
### EJSON
* [EJSON](#EJSON)
* [.parse(text, [options])](#EJSON.parse)
* [.stringify(value, [replacer], [space], [options])](#EJSON.stringify)
* [.serialize(bson, [options])](#EJSON.serialize)
* [.deserialize(ejson, [options])](#EJSON.deserialize)
<a name="EJSON.parse"></a>
#### *EJSON*.parse(text, [options])
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| text | <code>string</code> | | |
| [options] | <code>object</code> | | Optional settings |
| [options.relaxed] | <code>boolean</code> | <code>true</code> | Attempt to return native JS types where possible, rather than BSON types (if true) |
Parse an Extended JSON string, constructing the JavaScript value or object described by that
string.
**Example**
```js
const EJSON = require('mongodb-extjson');
const text = '{ "int32": { "$numberInt": "10" } }';
// prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } }
console.log(EJSON.parse(text, { relaxed: false }));
// prints { int32: 10 }
console.log(EJSON.parse(text));
```
<a name="EJSON.stringify"></a>
#### *EJSON*.stringify(value, [replacer], [space], [options])
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| value | <code>object</code> | | The value to convert to extended JSON |
| [replacer] | <code>function</code> \| <code>array</code> | | A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string |
| [space] | <code>string</code> \| <code>number</code> | | A String or Number object that's used to insert white space into the output JSON string for readability purposes. |
| [options] | <code>object</code> | | Optional settings |
| [options.relaxed] | <code>boolean</code> | <code>true</code> | Enabled Extended JSON's `relaxed` mode |
Converts a BSON document to an Extended JSON string, optionally replacing values if a replacer
function is specified or optionally including only the specified properties if a replacer array
is specified.
**Example**
```js
const EJSON = require('mongodb-extjson');
const Int32 = require('mongodb').Int32;
const doc = { int32: new Int32(10) };
// prints '{"int32":{"$numberInt":"10"}}'
console.log(EJSON.stringify(doc, { relaxed: false }));
// prints '{"int32":10}'
console.log(EJSON.stringify(doc));
```
<a name="EJSON.serialize"></a>
#### *EJSON*.serialize(bson, [options])
| Param | Type | Description |
| --- | --- | --- |
| bson | <code>object</code> | The object to serialize |
| [options] | <code>object</code> | Optional settings passed to the `stringify` function |
Serializes an object to an Extended JSON string, and reparse it as a JavaScript object.
<a name="EJSON.deserialize"></a>
#### *EJSON*.deserialize(ejson, [options])
| Param | Type | Description |
| --- | --- | --- |
| ejson | <code>object</code> | The Extended JSON object to deserialize |
| [options] | <code>object</code> | Optional settings passed to the parse method |
Deserializes an Extended JSON object into a plain JavaScript object with native/BSON types
<a name="setInternalBufferSize"></a>

@@ -88,0 +184,0 @@

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

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