Skip to content
This repository has been archived by the owner on Feb 3, 2022. It is now read-only.

Commit

Permalink
feat(parsing): support relaxed parsing of extended json
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Apr 1, 2018
1 parent 5e9fd41 commit 92e06d3
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions lib/ext_json.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ var keysToCodecs = {
$oid: codecs.ObjectID,
$binary: codecs.Binary,
$symbol: codecs.Symbol,
$numberInt: codecs.Int32,
$numberDecimal: codecs.Decimal128,
$numberDouble: codecs.Double,
$numberLong: codecs.Long,
$minKey: codecs.MinKey,
$maxKey: codecs.MaxKey,
Expand Down Expand Up @@ -66,7 +68,7 @@ function deserializeValue(self, key, value, options) {
var keys = Object.keys(value).filter(k => k.startsWith('$') && value[k] != null);
for (let i = 0; i < keys.length; i++) {
let c = keysToCodecs[keys[i]];
if (c) return c.fromExtendedJSON(BSON, value);
if (c) return c.fromExtendedJSON(BSON, value, options);
}

if (value.$date != null) {
Expand All @@ -85,18 +87,6 @@ function deserializeValue(self, key, value, options) {
return codecs.Code.fromExtendedJSON(BSON, value);
}

if (value.$numberDouble != null) {
return options.strict
? codecs.Double.fromExtendedJSON(BSON, value)
: parseFloat(value.$numberDouble);
}

if (value.$numberInt != null) {
return options.strict
? codecs.Int32.fromExtendedJSON(BSON, value)
: parseInt(value.$numberInt, 10);
}

if (value.$ref != null || value.$dbPointer != null) {
let v = value.$ref ? value : value.$dbPointer;

Expand All @@ -119,7 +109,11 @@ function deserializeValue(self, key, value, options) {

const parse = function(text, options) {
var self = this;
options = options || { strict: true };
options = options || { relaxed: false };

// relaxed implies not strict
if (typeof options.relaxed === 'boolean') options.strict = !options.relaxed;
if (typeof options.strict === 'boolean') options.relaxed = !options.strict;

return JSON.parse(text, function(key, value) {
return deserializeValue(self, key, value, options);
Expand Down

0 comments on commit 92e06d3

Please sign in to comment.