Skip to content

Commit 5e35d1a

Browse files
authored
fix: Reduce floating point precision required of extended json implementations (#369)
Updated the outputted extended json format for the BSON Double type to conform to the reduced precision standard for cross language compatibility NODE-2431
1 parent 5cda40f commit 5e35d1a

35 files changed

+258
-338
lines changed

Diff for: lib/double.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,17 @@ class Double {
4949
return { $numberDouble: `-${this.value.toFixed(1)}` };
5050
}
5151

52-
return {
53-
$numberDouble: Number.isInteger(this.value) ? this.value.toFixed(1) : this.value.toString()
54-
};
52+
let $numberDouble;
53+
if (Number.isInteger(this.value)) {
54+
$numberDouble = this.value.toFixed(1);
55+
if ($numberDouble.length >= 13) {
56+
$numberDouble = this.value.toExponential(13).toUpperCase();
57+
}
58+
} else {
59+
$numberDouble = this.value.toString();
60+
}
61+
62+
return { $numberDouble };
5563
}
5664

5765
/**

Diff for: test/node/bson_corpus_tests.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ const skipBSON = {
5858

5959
const skipExtendedJSON = {
6060
'Timestamp with high-order bit set on both seconds and increment':
61-
'Current BSON implementation of timestamp/long cannot hold these values - 1 too large.',
62-
'1.23456789012345677E+18': 'NODE-2519',
63-
'-1.23456789012345677E+18': 'NODE-2519'
61+
'Current BSON implementation of timestamp/long cannot hold these values - 1 too large.'
6462
};
6563

6664
const corpus = require('./tools/bson_corpus_test_loader');

Diff for: test/node/specs/bson-corpus/README.md

-96
This file was deleted.

Diff for: test/node/specs/bson-corpus/array.json

100755100644
+8-2
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,22 @@
1414
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}]}"
1515
},
1616
{
17-
"description": "Single Element Array with index set incorrectly",
17+
"description": "Single Element Array with index set incorrectly to empty string",
1818
"degenerate_bson": "130000000461000B00000010000A0000000000",
1919
"canonical_bson": "140000000461000C0000001030000A0000000000",
2020
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}]}"
2121
},
2222
{
23-
"description": "Single Element Array with index set incorrectly",
23+
"description": "Single Element Array with index set incorrectly to ab",
2424
"degenerate_bson": "150000000461000D000000106162000A0000000000",
2525
"canonical_bson": "140000000461000C0000001030000A0000000000",
2626
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}]}"
27+
},
28+
{
29+
"description": "Multi Element Array with duplicate indexes",
30+
"degenerate_bson": "1b000000046100130000001030000a000000103000140000000000",
31+
"canonical_bson": "1b000000046100130000001030000a000000103100140000000000",
32+
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}, {\"$numberInt\": \"20\"}]}"
2733
}
2834
],
2935
"decodeErrors": [

Diff for: test/node/specs/bson-corpus/binary.json

100755100644
File mode changed.

Diff for: test/node/specs/bson-corpus/boolean.json

100755100644
File mode changed.

Diff for: test/node/specs/bson-corpus/code.json

100755100644
File mode changed.

Diff for: test/node/specs/bson-corpus/code_w_scope.json

100755100644
File mode changed.

Diff for: test/node/specs/bson-corpus/datetime.json

100755100644
+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
"description" : "Y10K",
2626
"canonical_bson" : "1000000009610000DC1FD277E6000000",
2727
"canonical_extjson" : "{\"a\":{\"$date\":{\"$numberLong\":\"253402300800000\"}}}"
28+
},
29+
{
30+
"description": "leading zero ms",
31+
"canonical_bson": "10000000096100D1D6D6CC3B01000000",
32+
"relaxed_extjson": "{\"a\" : {\"$date\" : \"2012-12-24T12:15:30.001Z\"}}",
33+
"canonical_extjson": "{\"a\" : {\"$date\" : {\"$numberLong\" : \"1356351330001\"}}}"
2834
}
2935
],
3036
"decodeErrors": [

Diff for: test/node/specs/bson-corpus/dbpointer.json

100755100644
File mode changed.

Diff for: test/node/specs/bson-corpus/dbref.json

100755100644
File mode changed.

Diff for: test/node/specs/bson-corpus/decimal128-1.json

100755100644
File mode changed.

Diff for: test/node/specs/bson-corpus/decimal128-2.json

100755100644
File mode changed.

Diff for: test/node/specs/bson-corpus/decimal128-3.json

100755100644
File mode changed.

0 commit comments

Comments
 (0)