Skip to content

Commit 37e8690

Browse files
authored
feat(NODE-4873): support EJSON stringify from BigInt to $numberLong (#547)
1 parent d9f0eaa commit 37e8690

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

src/extended_json.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ function serializeValue(value: any, options: EJSONSerializeOptions): any {
240240
return { $numberDouble: Object.is(value, -0) ? '-0.0' : value.toString() };
241241
}
242242

243+
if (typeof value === 'bigint') {
244+
if (!options.relaxed) {
245+
return { $numberLong: BigInt.asIntN(64, value).toString() };
246+
}
247+
return Number(BigInt.asIntN(64, value));
248+
}
249+
243250
if (value instanceof RegExp || isRegExp(value)) {
244251
let flags = value.flags;
245252
if (flags === undefined) {

test/node/bigint.test.ts

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BSON, BSONError } from '../register-bson';
1+
import { BSON, EJSON, BSONError } from '../register-bson';
22
import { bufferFromHexArray } from './tools/utils';
33
import { expect } from 'chai';
44
import { BSON_DATA_LONG } from '../../src/constants';
@@ -263,4 +263,103 @@ describe('BSON BigInt support', function () {
263263
expect(serializedMap).to.deep.equal(expectedSerialization);
264264
});
265265
});
266+
267+
describe('EJSON.stringify()', function () {
268+
context('canonical mode (relaxed=false)', function () {
269+
it('truncates bigint values when they are outside the range [BSON_INT64_MIN, BSON_INT64_MAX]', function () {
270+
const numbers = { a: 2n ** 64n + 1n, b: -(2n ** 64n) - 1n };
271+
const serialized = EJSON.stringify(numbers, { relaxed: false });
272+
expect(serialized).to.equal('{"a":{"$numberLong":"1"},"b":{"$numberLong":"-1"}}');
273+
});
274+
275+
it('truncates bigint values in the same way as BSON.serialize', function () {
276+
const number = { a: 0x1234_5678_1234_5678_9999n };
277+
const stringified = EJSON.stringify(number, { relaxed: false });
278+
const serialized = BSON.serialize(number);
279+
280+
const VALUE_OFFSET = 7;
281+
const dataView = BSONDataView.fromUint8Array(serialized);
282+
const serializedValue = dataView.getBigInt64(VALUE_OFFSET, true);
283+
const parsed = JSON.parse(stringified);
284+
285+
expect(parsed).to.have.property('a');
286+
expect(parsed['a']).to.have.property('$numberLong');
287+
expect(parsed.a.$numberLong).to.equal(0x5678_1234_5678_9999n.toString());
288+
289+
expect(parsed.a.$numberLong).to.equal(serializedValue.toString());
290+
});
291+
it('serializes bigint values to numberLong in canonical mode', function () {
292+
const number = { a: 2n };
293+
const serialized = EJSON.stringify(number, { relaxed: false });
294+
expect(serialized).to.equal('{"a":{"$numberLong":"2"}}');
295+
});
296+
});
297+
298+
context('relaxed mode (relaxed=true)', function () {
299+
it('truncates bigint values in the same way as BSON.serialize', function () {
300+
const number = { a: 0x1234_0000_1234_5678_9999n }; // Ensure that the truncated number can be exactly represented as a JS number
301+
const stringified = EJSON.stringify(number, { relaxed: true });
302+
const serializedDoc = BSON.serialize(number);
303+
304+
const VALUE_OFFSET = 7;
305+
const dataView = BSONDataView.fromUint8Array(serializedDoc);
306+
const parsed = JSON.parse(stringified);
307+
308+
expect(parsed).to.have.property('a');
309+
expect(parsed.a).to.equal(0x0000_1234_5678_9999);
310+
311+
expect(parsed.a).to.equal(Number(dataView.getBigInt64(VALUE_OFFSET, true)));
312+
});
313+
314+
it('serializes bigint values to Number', function () {
315+
const number = { a: 10000n };
316+
const serialized = EJSON.stringify(number, { relaxed: true });
317+
expect(serialized).to.equal('{"a":10000}');
318+
});
319+
320+
it('loses precision when serializing bigint values outside of range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]', function () {
321+
const numbers = { a: -(2n ** 53n) - 1n, b: 2n ** 53n + 2n };
322+
const serialized = EJSON.stringify(numbers, { relaxed: true });
323+
expect(serialized).to.equal('{"a":-9007199254740992,"b":9007199254740994}');
324+
});
325+
});
326+
327+
context('when passed bigint values that are 64 bits wide or less', function () {
328+
let parsed;
329+
330+
before(function () {
331+
const number = { a: 12345n };
332+
const serialized = EJSON.stringify(number, { relaxed: false });
333+
parsed = JSON.parse(serialized);
334+
});
335+
336+
it('passes loose equality checks with native bigint values', function () {
337+
// eslint-disable-next-line eqeqeq
338+
expect(parsed.a.$numberLong == 12345n).true;
339+
});
340+
341+
it('equals the result of BigInt.toString', function () {
342+
expect(parsed.a.$numberLong).to.equal(12345n.toString());
343+
});
344+
});
345+
346+
context('when passed bigint values that are more than 64 bits wide', function () {
347+
let parsed;
348+
349+
before(function () {
350+
const number = { a: 0x1234_5678_1234_5678_9999n };
351+
const serialized = EJSON.stringify(number, { relaxed: false });
352+
parsed = JSON.parse(serialized);
353+
});
354+
355+
it('fails loose equality checks with native bigint values', function () {
356+
// eslint-disable-next-line eqeqeq
357+
expect(parsed.a.$numberLong == 0x1234_5678_1234_5678_9999n).false;
358+
});
359+
360+
it('not equal to results of BigInt.toString', function () {
361+
expect(parsed.a.$numberLong).to.not.equal(0x1234_5678_1234_5678_9999n.toString());
362+
});
363+
});
364+
});
266365
});

0 commit comments

Comments
 (0)