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

NODE-2431: Reduce floating point precision required of extended json implementations #30

Merged
merged 10 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<a name="3.0.3"></a>
## [3.0.3](https://github.com/mongodb-js/mongodb-extjson/compare/v3.0.2...v3.0.3) (2018-07-13)


### Bug Fixes

* **breaking:** reverts breaking changes in v3.0.1 and v3.0.2 ([3fab274](https://github.com/mongodb-js/mongodb-extjson/commit/3fab274))



<a name="2.1.4"></a>
## [2.1.4](https://github.com/mongodb-js/mongodb-extjson/compare/v2.1.3...v2.1.4) (2018-05-31)

Expand Down
23 changes: 23 additions & 0 deletions etc/update-spec-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

# This script is used to fetch the latest JSON tests for the BSON spec.
# It puts the tests in the direcory $spec_root It should be run from the root of the repository.

set -o errexit
set -o nounset

if [ ! -d ".git" ]; then
echo "$0: This script must be run from the root of the repository" >&2
exit 1
fi

spec_root="test"

tmpdir=$(mktemp -d -t spec_testsXXXX)
curl -sL https://github.com/mongodb/specifications/archive/master.zip -o "$tmpdir/specs.zip"
unzip -d "$tmpdir" "$tmpdir/specs.zip" > /dev/null

mkdir -p "$spec_root/bson-corpus"
rsync -ah "$tmpdir/specifications-master/source/bson-corpus/tests/" "$spec_root/bson-corpus" --delete

rm -rf "$tmpdir"
22 changes: 20 additions & 2 deletions lib/bson/double.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
'use strict';

function toExtendedJSON(obj, options) {
if (options && options.relaxed && isFinite(obj.value)) return obj.value;
return { $numberDouble: obj.value.toString() };
if (options && (options.legacy || (options.relaxed && isFinite(obj.value)))) {
return obj.value;
}
// NOTE: JavaScript has +0 and -0, apparently to model limit calculations. If a user
// explicitly provided `-0` then we need to ensure the sign makes it into the output
if (Object.is(Math.sign(obj.value), -0)) {
return { $numberDouble: `-${obj.value.toFixed(1)}` };
}

var numberDouble;
if (Number.isInteger(obj.value)) {
numberDouble = obj.value.toFixed(1);
if (numberDouble.length >= 13) {
numberDouble = obj.value.toExponential(13).toUpperCase();
}
} else {
numberDouble = obj.value.toString();
}

return { $numberDouble: numberDouble };
}

function fromExtendedJSON(BSON, doc, options) {
Expand Down
127 changes: 102 additions & 25 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "mongodb-extjson",
"version": "2.1.4",
"version": "3.0.3",
"description": "MongoDB Extended JSON library",
"main": "index.js",
"browser": "dist/bundle.js",
"browser": "dist/mongodb-extjson.js",
"scripts": {
"test": "mocha test/",
"coverage": "istanbul cover _mocha -- --recursive -t --ui tdd test/",
Expand Down Expand Up @@ -41,14 +41,14 @@
"eslint-plugin-prettier": "^2.3.1",
"istanbul": "^0.4.5",
"mocha": "^3.4.1",
"mongodb": "^2.2.27",
"mongodb": "^3.5",
"prettier": "~1.12.0",
"rollup": "^0.56.2",
"rollup-plugin-babel": "^3.0.3",
"rollup-plugin-commonjs": "^8.3.0",
"rollup-plugin-node-resolve": "^3.0.3"
},
"dependencies": {
"bson": "mongodb/js-bson#3.0"
"bson": "^3.0.2"
}
}
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import babel from 'rollup-plugin-babel';
export default {
input: 'index.js',
output: {
file: 'dist/bundle.js',
file: 'dist/mongodb-extjson.js',
format: 'umd',
name: 'mongodb-extjson',
},
Expand Down
10 changes: 8 additions & 2 deletions test/bson-corpus/array.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}]}"
},
{
"description": "Single Element Array with index set incorrectly",
"description": "Single Element Array with index set incorrectly to empty string",
"degenerate_bson": "130000000461000B00000010000A0000000000",
"canonical_bson": "140000000461000C0000001030000A0000000000",
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}]}"
},
{
"description": "Single Element Array with index set incorrectly",
"description": "Single Element Array with index set incorrectly to ab",
"degenerate_bson": "150000000461000D000000106162000A0000000000",
"canonical_bson": "140000000461000C0000001030000A0000000000",
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}]}"
},
{
"description": "Multi Element Array with duplicate indexes",
"degenerate_bson": "1b000000046100130000001030000a000000103000140000000000",
"canonical_bson": "1b000000046100130000001030000a000000103100140000000000",
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}, {\"$numberInt\": \"20\"}]}"
}
],
"decodeErrors": [
Expand Down
Empty file modified test/bson-corpus/binary.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/boolean.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/code.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/code_w_scope.json
100755 → 100644
Empty file.
6 changes: 6 additions & 0 deletions test/bson-corpus/datetime.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
"description" : "Y10K",
"canonical_bson" : "1000000009610000DC1FD277E6000000",
"canonical_extjson" : "{\"a\":{\"$date\":{\"$numberLong\":\"253402300800000\"}}}"
},
{
"description": "leading zero ms",
"canonical_bson": "10000000096100D1D6D6CC3B01000000",
"relaxed_extjson": "{\"a\" : {\"$date\" : \"2012-12-24T12:15:30.001Z\"}}",
"canonical_extjson": "{\"a\" : {\"$date\" : {\"$numberLong\" : \"1356351330001\"}}}"
}
],
"decodeErrors": [
Expand Down
Empty file modified test/bson-corpus/dbpointer.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/dbref.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/decimal128-1.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/decimal128-2.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/decimal128-3.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/decimal128-4.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/decimal128-5.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/decimal128-6.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/decimal128-7.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/document.json
100755 → 100644
Empty file.
16 changes: 8 additions & 8 deletions test/bson-corpus/double.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@
"relaxed_extjson": "{\"d\" : -1.0001220703125}"
},
{
"description": "1.23456789012345677E+18",
"canonical_bson": "1000000001640081E97DF41022B14300",
"canonical_extjson": "{\"d\" : {\"$numberDouble\": \"1.23456789012345677E+18\"}}",
"relaxed_extjson": "{\"d\" : 1.23456789012345677E+18}"
"description": "1.2345678921232E+18",
"canonical_bson": "100000000164002a1bf5f41022b14300",
"canonical_extjson": "{\"d\" : {\"$numberDouble\": \"1.2345678921232E+18\"}}",
"relaxed_extjson": "{\"d\" : 1.2345678921232E+18}"
},
{
"description": "-1.23456789012345677E+18",
"canonical_bson": "1000000001640081E97DF41022B1C300",
"canonical_extjson": "{\"d\" : {\"$numberDouble\": \"-1.23456789012345677E+18\"}}",
"relaxed_extjson": "{\"d\" : -1.23456789012345677E+18}"
"description": "-1.2345678921232E+18",
"canonical_bson": "100000000164002a1bf5f41022b1c300",
"canonical_extjson": "{\"d\" : {\"$numberDouble\": \"-1.2345678921232E+18\"}}",
"relaxed_extjson": "{\"d\" : -1.2345678921232E+18}"
},
{
"description": "0.0",
Expand Down
Empty file modified test/bson-corpus/int32.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/int64.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/maxkey.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/minkey.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/multi-type-deprecated.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/multi-type.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/null.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/oid.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/regex.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/string.json
100755 → 100644
Empty file.
Empty file modified test/bson-corpus/symbol.json
100755 → 100644
Empty file.
10 changes: 7 additions & 3 deletions test/bson-corpus/top.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@
"parseErrors": [
{
"description" : "Bad $regularExpression (extra field)",
"string" : "{\"a\" : \"$regularExpression\": {\"pattern\": \"abc\", \"options\": \"\", \"unrelated\": true}}}"
"string" : "{\"a\" : {\"$regularExpression\": {\"pattern\": \"abc\", \"options\": \"\", \"unrelated\": true}}}"
},
{
"description" : "Bad $regularExpression (missing options field)",
"string" : "{\"a\" : \"$regularExpression\": {\"pattern\": \"abc\"}}}"
"string" : "{\"a\" : {\"$regularExpression\": {\"pattern\": \"abc\"}}}"
},
{
"description": "Bad $regularExpression (pattern is number, not string)",
Expand All @@ -85,7 +85,7 @@
},
{
"description" : "Bad $regularExpression (missing pattern field)",
"string" : "{\"a\" : \"$regularExpression\": {\"options\":\"ix\"}}}"
"string" : "{\"a\" : {\"$regularExpression\": {\"options\":\"ix\"}}}"
},
{
"description": "Bad $oid (number, not string)",
Expand Down Expand Up @@ -151,6 +151,10 @@
"description": "Bad $code (type is number, not string)",
"string": "{\"a\" : {\"$code\" : 42}}"
},
{
"description": "Bad $code (type is number, not string) when $scope is also present",
"string": "{\"a\" : {\"$code\" : 42, \"$scope\" : {}}}"
},
{
"description": "Bad $code (extra field)",
"string": "{\"a\" : {\"$code\" : \"\", \"unrelated\": true}}"
Expand Down
Empty file modified test/bson-corpus/undefined.json
100755 → 100644
Empty file.
Loading