diff --git a/lib/ext_json.js b/lib/ext_json.js index 692920c..dc78353 100644 --- a/lib/ext_json.js +++ b/lib/ext_json.js @@ -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, @@ -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) { @@ -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; @@ -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);