Skip to content
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
45 changes: 33 additions & 12 deletions src/extended_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ function parse(text: string, options?: EJSONParseOptions): any {
});
}

/* eslint-disable @typescript-eslint/no-explicit-any */
Comment thread
tadjik1 marked this conversation as resolved.
/**
* Converts a BSON document to an Extended JSON string, optionally replacing values if a replacer
* function is specified or optionally including only the specified properties if a replacer array
Expand All @@ -464,34 +465,54 @@ function parse(text: string, options?: EJSONParseOptions): any {
*
* // prints '{"int32":10}'
* console.log(EJSON.stringify(doc));
*
* // prints '{"int32":{"$numberInt":"10"}}' with 2 space indentation
* console.log(EJSON.stringify(doc, { relaxed: false }, 2));
* ```
*/
function stringify(
Comment thread
tadjik1 marked this conversation as resolved.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any,
replacer?:
replacer?: (number | string)[] | ((this: any, key: string, value: any) => any) | null,
space?: string | number,
options?: EJSONSerializeOptions
): string;
function stringify(
value: any,
replacer?: (number | string)[] | ((this: any, key: string, value: any) => any) | null,
options?: EJSONSerializeOptions
): string;
function stringify(value: any, options?: EJSONSerializeOptions, space?: string | number): string;
function stringify(
value: any,
replacerOrOptions?:
| (number | string)[]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| ((this: any, key: string, value: any) => any)
| null
| EJSONSerializeOptions,
space?: string | number,
spaceOrOptions?: string | number | EJSONSerializeOptions,
options?: EJSONSerializeOptions
): string {
if (space != null && typeof space === 'object') {
options = space;
space = 0;
/* eslint-enable @typescript-eslint/no-explicit-any */

if (spaceOrOptions != null && typeof spaceOrOptions === 'object') {
options = spaceOrOptions;
spaceOrOptions = undefined;
}
if (replacer != null && typeof replacer === 'object' && !Array.isArray(replacer)) {
options = replacer;
replacer = undefined;
space = 0;
Comment thread
tadjik1 marked this conversation as resolved.
if (
replacerOrOptions != null &&
typeof replacerOrOptions === 'object' &&
!Array.isArray(replacerOrOptions)
) {
options = replacerOrOptions;
replacerOrOptions = undefined;
}

const serializeOptions = Object.assign({ relaxed: true, legacy: false }, options, {
seenObjects: [{ propertyName: '(root)', obj: null }]
});

const doc = serializeValue(value, serializeOptions);
return JSON.stringify(doc, replacer as Parameters<JSON['stringify']>[1], space);
return JSON.stringify(doc, replacerOrOptions as Parameters<JSON['stringify']>[1], spaceOrOptions);
}

/**
Expand Down
161 changes: 160 additions & 1 deletion test/node/extended_json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const EJSON = BSON.EJSON;
import * as vm from 'node:vm';
import { expect } from 'chai';
import { BSONVersionError, BSONRuntimeError } from '../../src';
import { BSONError } from '../register-bson';
import { BSONError, EJSONSerializeOptions } from '../register-bson';

// BSON types
const Binary = BSON.Binary;
Expand Down Expand Up @@ -733,4 +733,163 @@ describe('Extended JSON', function () {
expect(() => EJSON.stringify(input)).to.throw(BSONError);
});
});

context('stringify: Parameter signature combinations', function () {
Comment thread
chdanielmueller marked this conversation as resolved.
const testDoc = {
objectId: ObjectId.createFromHexString('111111111111111111111111'),
int32Number: 300,
name: 'test'
};

it('should work with (value) - only value parameter', function () {
const result = EJSON.stringify(testDoc);
expect(result).to.equal(
'{"objectId":{"$oid":"111111111111111111111111"},"int32Number":300,"name":"test"}'
);
});

it('should work with (value, null, space) - replacer null, space number', function () {
const result = EJSON.stringify(testDoc, null, 2);
expect(result).to.equal(`{
"objectId": {
"$oid": "111111111111111111111111"
},
"int32Number": 300,
"name": "test"
}`);
});

it('should work with (value, null, space, options) - replacer null, space, options', function () {
const result = EJSON.stringify(testDoc, null, 2, { relaxed: false });
expect(result).to.equal(`{
"objectId": {
"$oid": "111111111111111111111111"
},
"int32Number": {
"$numberInt": "300"
},
"name": "test"
}`);
});

it('should work with (value, array, space) - replacer array, space', function () {
const result = EJSON.stringify(testDoc, ['objectId', '$oid', 'name'], 2);
expect(result).to.equal(`{
"objectId": {
"$oid": "111111111111111111111111"
},
"name": "test"
}`);
});

it('should work with (value, array, space, options) - replacer array, space, options', function () {
const result = EJSON.stringify(testDoc, ['objectId', '$oid'], 2, { relaxed: false });
expect(result).to.equal(`{
"objectId": {
"$oid": "111111111111111111111111"
}
}`);
});

it('should work with (value, function, space) - replacer function, space', function () {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const replacer = function (key: string, value: any) {
return key === 'name' ? undefined : value;
};
const result = EJSON.stringify(testDoc, replacer, 2);
expect(result).to.equal(`{
"objectId": {
"$oid": "111111111111111111111111"
},
"int32Number": 300
}`);
});

it('should work with (value, function, space, options) - replacer function, space, options', function () {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const replacer = function (key: string, value: any) {
return key === 'name' ? undefined : value;
};
const result = EJSON.stringify(testDoc, replacer, 2, { relaxed: false });
expect(result).to.equal(`{
"objectId": {
"$oid": "111111111111111111111111"
},
"int32Number": {
"$numberInt": "300"
}
}`);
});

it('should work with (value, null, options) - replacer null, options', function () {
const result = EJSON.stringify(testDoc, null, { relaxed: false });
expect(result).to.equal(
'{"objectId":{"$oid":"111111111111111111111111"},"int32Number":{"$numberInt":"300"},"name":"test"}'
);
});

it('should work with (value, array, options) - replacer array, options', function () {
const result = EJSON.stringify(testDoc, ['objectId', '$oid'], { relaxed: false });
expect(result).to.equal('{"objectId":{"$oid":"111111111111111111111111"}}');
});

it('should work with (value, function, options) - replacer function, options', function () {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const replacer = function (key: string, value: any) {
return key === 'int32Number' ? undefined : value;
};
const result = EJSON.stringify(testDoc, replacer, { relaxed: false });
expect(result).to.equal('{"objectId":{"$oid":"111111111111111111111111"},"name":"test"}');
});

it('should work with (value, options) - value and options only', function () {
const result = EJSON.stringify(testDoc, { relaxed: false });
expect(result).to.equal(
'{"objectId":{"$oid":"111111111111111111111111"},"int32Number":{"$numberInt":"300"},"name":"test"}'
);
});

it('should work with (value, options) - value and options only (options with typo)', function () {
const result = EJSON.stringify(testDoc, {
relaxed: false,
igroneUndefined: true
} as unknown as EJSONSerializeOptions);
expect(result).to.equal(
'{"objectId":{"$oid":"111111111111111111111111"},"int32Number":{"$numberInt":"300"},"name":"test"}'
);
});

it('should work with (value, options, space) - value, options, space (second overload)', function () {
const result = EJSON.stringify(testDoc, { relaxed: false }, 2);
expect(result).to.equal(`{
"objectId": {
"$oid": "111111111111111111111111"
},
"int32Number": {
"$numberInt": "300"
},
"name": "test"
}`);
});

it('should work with string space parameter', function () {
const result = EJSON.stringify(testDoc, null, '\t', { relaxed: false });
expect(result).to.equal(`{
Comment thread
chdanielmueller marked this conversation as resolved.
\t"objectId": {
\t\t"$oid": "111111111111111111111111"
\t},
\t"int32Number": {
\t\t"$numberInt": "300"
\t},
\t"name": "test"
}`);
});

it('should work with space 0 (no formatting)', function () {
const result = EJSON.stringify(testDoc, null, 0, { relaxed: false });
expect(result).to.equal(
'{"objectId":{"$oid":"111111111111111111111111"},"int32Number":{"$numberInt":"300"},"name":"test"}'
);
});
});
});
Loading