Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NODE-2253: Support for v1 Extended JSON #339

Merged
merged 8 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ console.log(EJSON.parse(text));
| [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 |
| [options.legacy] | <code>boolean</code> | <code>true</code> | Output in Extended JSON v1 |

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
Expand Down
20 changes: 16 additions & 4 deletions lib/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,18 @@ class Binary {
/**
* @ignore
*/
toExtendedJSON() {
toExtendedJSON(options) {
const base64String = Buffer.isBuffer(this.buffer)
? this.buffer.toString('base64')
: Buffer.from(this.buffer).toString('base64');

const subType = Number(this.sub_type).toString(16);
if (options.legacy) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to ensure options exists, otherwise you'll have an error on trying to access legacy on it. Recommend:

  • typeof options !== 'undefined' && options.legacy
  • adding a options = options || {} above
  • make some helper function to check this, since its likely you are doing it many places

I realize this may be an existing issue prior to the changes introduced here, but this PR would necessarily introduce such a bug into every method now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mbroadst I can take care of this tmrw if @durran doesn't beat me to it and you give me push access to this repo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@imlucas added you as a collaborator

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Pushed

return {
$binary: base64String,
$type: subType.length === 1 ? '0' + subType : subType
};
}
return {
$binary: {
base64: base64String,
Expand All @@ -297,9 +303,15 @@ class Binary {
/**
* @ignore
*/
static fromExtendedJSON(doc) {
const type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0;
const data = Buffer.from(doc.$binary.base64, 'base64');
static fromExtendedJSON(doc, options) {
let data, type;
if (options.legacy) {
type = doc.$type ? parseInt(doc.$type, 16) : 0;
data = Buffer.from(doc.$binary, 'base64');
} else {
type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0;
data = Buffer.from(doc.$binary.base64, 'base64');
}
return new Binary(data, type);
}
}
Expand Down
6 changes: 5 additions & 1 deletion lib/db_ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ class DBRef {
/**
* @ignore
*/
toExtendedJSON() {
toExtendedJSON(options) {
let o = {
$ref: this.collection,
$id: this.oid
};

if (options.legacy) {
return o;
}

if (this.db) o.$db = this.db;
o = Object.assign(o, this.fields);
return o;
Expand Down
4 changes: 3 additions & 1 deletion lib/double.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class Double {
* @ignore
*/
toExtendedJSON(options) {
if (options && options.relaxed && isFinite(this.value)) return this.value;
if (options && (options.legacy || (options.relaxed && isFinite(this.value)))) {
return this.value;
}
return { $numberDouble: this.value.toString() };
}

Expand Down
21 changes: 16 additions & 5 deletions lib/extended_json.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ const keysToCodecs = {
$numberLong: Long,
$minKey: MinKey,
$maxKey: MaxKey,
$regex: BSONRegExp,
$regularExpression: BSONRegExp,
$timestamp: Timestamp
};

function deserializeValue(self, key, value, options) {
if (typeof value === 'number') {
if (options.relaxed) {
if (options.relaxed || options.legacy) {
return value;
}

Expand Down Expand Up @@ -69,9 +70,14 @@ function deserializeValue(self, key, value, options) {
const d = value.$date;
const date = new Date();

if (typeof d === 'string') date.setTime(Date.parse(d));
else if (Long.isLong(d)) date.setTime(d.toNumber());
else if (typeof d === 'number' && options.relaxed) date.setTime(d);
if (options.legacy) {
if (typeof d === 'number') date.setTime(d);
else if (typeof d === 'string') date.setTime(Date.parse(d));
} else {
if (typeof d === 'string') date.setTime(Date.parse(d));
else if (Long.isLong(d)) date.setTime(d.toNumber());
else if (typeof d === 'number' && options.relaxed) date.setTime(d);
}
return date;
}

Expand Down Expand Up @@ -233,6 +239,11 @@ function serializeValue(value, options) {
// is it in year range 1970-9999?
inRange = dateNum > -1 && dateNum < 253402318800000;

if (options.legacy) {
return options.relaxed && inRange
? { $date: value.getTime() }
: { $date: getISOString(value) };
}
return options.relaxed && inRange
? { $date: getISOString(value) }
: { $date: { $numberLong: value.getTime().toString() } };
Expand All @@ -258,7 +269,7 @@ function serializeValue(value, options) {
}

const rx = new BSONRegExp(value.source, flags);
return rx.toExtendedJSON();
return rx.toExtendedJSON(options);
}

if (value != null && typeof value === 'object') return serializeDocument(value, options);
Expand Down
2 changes: 1 addition & 1 deletion lib/int_32.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Int32 {
* @ignore
*/
toExtendedJSON(options) {
if (options && options.relaxed) return this.value;
if (options && (options.relaxed || options.legacy)) return this.value;
return { $numberInt: this.value.toString() };
}

Expand Down
26 changes: 21 additions & 5 deletions lib/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,39 @@ class BSONRegExp {
}
}

static parseOptions(options) {
return options
? options
.split('')
.sort()
.join('')
: '';
}

/**
* @ignore
*/
toExtendedJSON() {
toExtendedJSON(options) {
if (options.legacy) {
return { $regex: this.pattern, $options: this.options };
}
return { $regularExpression: { pattern: this.pattern, options: this.options } };
}

/**
* @ignore
*/
static fromExtendedJSON(doc) {
if (doc.$regex) {
// This is for $regex query operators that have extended json values.
if (doc.$regex._bsontype === 'BSONRegExp') {
return doc;
}
return new BSONRegExp(doc.$regex, BSONRegExp.parseOptions(doc.$options));
}
return new BSONRegExp(
doc.$regularExpression.pattern,
doc.$regularExpression.options
.split('')
.sort()
.join('')
BSONRegExp.parseOptions(doc.$regularExpression.options)
);
}
}
Expand Down
41 changes: 30 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading