Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-4789): support Map stringification in EJSON #567

Merged
merged 5 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 12 additions & 1 deletion src/extended_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Long } from './long';
import { MaxKey } from './max_key';
import { MinKey } from './min_key';
import { ObjectId } from './objectid';
import { isDate, isRegExp } from './parser/utils';
import { isDate, isRegExp, isMap } from './parser/utils';
import { BSONRegExp } from './regexp';
import { BSONSymbol } from './symbol';
import { Timestamp } from './timestamp';
Expand Down Expand Up @@ -190,6 +190,17 @@ function getISOString(date: Date) {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function serializeValue(value: any, options: EJSONSerializeOptions): any {
if (value instanceof Map || isMap(value)) {
const obj: Record<string, unknown> = {};
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
for (const [k, v] of value.entries()) {
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
if (typeof k !== 'string') {
throw new BSONError('Can only serialize maps with string keys');
}
obj[k] = v;
}

return serializeValue(obj, options);
}
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
if ((typeof value === 'object' || typeof value === 'function') && value !== null) {
const index = options.seenObjects.findIndex(entry => entry.obj === value);
if (index !== -1) {
Expand Down
39 changes: 39 additions & 0 deletions test/node/extended_json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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';

// BSON types
const Binary = BSON.Binary;
Expand Down Expand Up @@ -583,4 +584,42 @@ describe('Extended JSON', function () {
})
).to.throw(BSONVersionError, /Unsupported BSON version/i);
});

context('Map objects', function () {
it('serializes an empty Map object', () => {
const input = new Map();
expect(EJSON.stringify(input)).to.equal('{}');
});

it('serializes a nested Map object', () => {
const input = new Map([
[
'a',
new Map([
['a', 100],
['b', 200]
])
]
]);

const str = EJSON.stringify(input);
expect(str).to.equal('{"a":{"a":100,"b":200}}');
});

it('serializes a Map with one string key', () => {
const input = new Map([['a', 100]]);

const str = EJSON.stringify(input);
expect(str).to.equal('{"a":100}');
});

it('throws BSONError when passed Map object with non-string keys', () => {
const input: Map<number, unknown> = new Map([
[1, 100],
[2, 200]
]);

expect(() => EJSON.stringify(input)).to.throw(BSONError);
});
});
});