Skip to content

Commit

Permalink
feat(ext-json): add extended JSON codecs directly to BSON types
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Oct 1, 2018
1 parent 23811c7 commit 10e5f00
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/binary.js
Expand Up @@ -274,6 +274,32 @@ class Binary {
toString(format) {
return this.buffer != null ? this.buffer.slice(0, this.position).toString(format) : '';
}

/**
* @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);
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions lib/code.js
Expand Up @@ -22,6 +22,24 @@ class Code {
toJSON() {
return { scope: this.scope, code: this.code };
}

/**
* @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);
}
}

Object.defineProperty(Code.prototype, '_bsontype', { value: 'Code' });
Expand Down
23 changes: 23 additions & 0 deletions lib/db_ref.js
Expand Up @@ -42,6 +42,29 @@ class DBRef {
if (this.db != null) o.$db = this.db;
return o;
}

/**
* @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);
}
}

Object.defineProperty(DBRef.prototype, '_bsontype', { value: 'DBRef' });
Expand Down
14 changes: 14 additions & 0 deletions lib/decimal128.js
Expand Up @@ -788,5 +788,19 @@ Decimal128.prototype.toJSON = function() {
return { $numberDecimal: this.toString() };
};

/**
* @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;
17 changes: 17 additions & 0 deletions lib/double.js
Expand Up @@ -29,6 +29,23 @@ class Double {
toJSON() {
return this.value;
}

/**
* @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));
}
}

Object.defineProperty(Double.prototype, '_bsontype', { value: 'Double' });
Expand Down
15 changes: 15 additions & 0 deletions lib/int_32.js
Expand Up @@ -29,6 +29,21 @@ class Int32 {
toJSON() {
return this.value;
}

/**
* @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);
}
}

Object.defineProperty(Int32.prototype, '_bsontype', { value: 'Int32' });
Expand Down
16 changes: 16 additions & 0 deletions lib/long.js
@@ -1,5 +1,21 @@
'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;
14 changes: 14 additions & 0 deletions lib/max_key.js
Expand Up @@ -9,6 +9,20 @@ class MaxKey {
* @return {MaxKey} A MaxKey instance
*/
constructor() {}

/**
* @ignore
*/
toExtendedJSON() {
return { $maxKey: 1 };
}

/**
* @ignore
*/
static fromExtendedJSON() {
return new MaxKey();
}
}

Object.defineProperty(MaxKey.prototype, '_bsontype', { value: 'MaxKey' });
Expand Down
14 changes: 14 additions & 0 deletions lib/min_key.js
Expand Up @@ -9,6 +9,20 @@ class MinKey {
* @return {MinKey} A MinKey instance
*/
constructor() {}

/**
* @ignore
*/
toExtendedJSON() {
return { $minKey: 1 };
}

/**
* @ignore
*/
static fromExtendedJSON() {
return new MinKey();
}
}

Object.defineProperty(MinKey.prototype, '_bsontype', { value: 'MinKey' });
Expand Down
15 changes: 15 additions & 0 deletions lib/objectid.js
Expand Up @@ -351,6 +351,21 @@ class ObjectId {

return false;
}

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

/**
* @ignore
*/
static fromExtendedJSON(doc) {
return new ObjectId(doc.$oid);
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions lib/regexp.js
Expand Up @@ -38,6 +38,26 @@ class BSONRegExp {
}
}
}

/**
* @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('')
);
}
}

Object.defineProperty(BSONRegExp.prototype, '_bsontype', { value: 'BSONRegExp' });
Expand Down
14 changes: 14 additions & 0 deletions lib/symbol.js
Expand Up @@ -42,6 +42,20 @@ class Symbol {
toJSON() {
return this.value;
}

/**
* @ignore
*/
toExtendedJSON() {
return { $symbol: this.value };
}

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

Object.defineProperty(Symbol.prototype, '_bsontype', { value: 'Symbol' });
Expand Down
14 changes: 14 additions & 0 deletions lib/timestamp.js
Expand Up @@ -74,6 +74,20 @@ class Timestamp extends Long {
static fromString(str, opt_radix) {
return new Timestamp(Long.fromString(str, opt_radix));
}

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

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

Object.defineProperty(Timestamp.prototype, '_bsontype', { value: 'Timestamp' });
Expand Down

0 comments on commit 10e5f00

Please sign in to comment.