Skip to content
Closed
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
5 changes: 4 additions & 1 deletion src/extended_json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Binary } from './binary';
import type { Document } from './bson';
import { UUID } from './uuid';
import { Code } from './code';
import { DBRef, isDBRefLike } from './db_ref';
import { Decimal128 } from './decimal128';
Expand Down Expand Up @@ -32,7 +33,8 @@ type BSONType =
| ObjectId
| BSONRegExp
| BSONSymbol
| Timestamp;
| Timestamp
| UUID;

export function isBSONType(value: unknown): value is BSONType {
return (
Expand Down Expand Up @@ -247,6 +249,7 @@ function serializeValue(value: any, options: EJSONSerializeOptions): any {

const BSON_TYPE_MAPPINGS = {
Binary: (o: Binary) => new Binary(o.value(), o.sub_type),
UUID: (o: UUID) => new UUID(o.id),
Code: (o: Code) => new Code(o.code, o.scope),
DBRef: (o: DBRef) => new DBRef(o.collection || o.namespace, o.oid, o.db, o.fields), // "namespace" for 1.x library backwards compat
Decimal128: (o: Decimal128) => new Decimal128(o.bytes),
Expand Down
5 changes: 5 additions & 0 deletions src/uuid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Binary } from './binary';
import { bufferToUuidHexString, uuidHexStringToBuffer, uuidValidateString } from './uuid_utils';
import { isUint8Array, randomBytes } from './parser/utils';
import { BSONTypeError } from './error';
import type { EJSONOptions } from './extended_json';

/** @public */
export type UUIDExtended = {
Expand Down Expand Up @@ -204,6 +205,10 @@ export class UUID {
inspect(): string {
return `new UUID("${this.toHexString()}")`;
}

toExtendedJSON(options: EJSONOptions) {
return this.toBinary().toExtendedJSON(options);
}
}

Object.defineProperty(UUID.prototype, '_bsontype', { value: 'UUID' });
15 changes: 15 additions & 0 deletions test/node/extended_json_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const ObjectId = BSON.ObjectId;
const BSONRegExp = BSON.BSONRegExp;
const BSONSymbol = BSON.BSONSymbol;
const Timestamp = BSON.Timestamp;
const UUID = BSON.UUID;

// Several tests in this file can test interop between current library versions and library version 1.1.0, because
// between 1.1.0 and 4.0.0 there was a significant rewrite. To minimize maintenance issues of a hard dependency on
Expand Down Expand Up @@ -739,4 +740,18 @@ Converting circular structure to EJSON:
});
});
});

describe('UUID stringify', () => {
const uuid = new UUID();
const stringifiedPlainUUID = EJSON.stringify({ u: uuid });
it('should return same values for UUID.toBinary() and UUID', () => {
const stringifiedToBinary = EJSON.stringify({ u: uuid.toBinary() });
expect(stringifiedToBinary).to.deep.equal(stringifiedPlainUUID);
});
it('should serialize to correct subType', () => {
const stringifiedUUIDtoObject = JSON.parse(stringifiedPlainUUID);
const stringifiedBinaryNewUUIDSubType = '04';
expect(stringifiedUUIDtoObject.u.$binary.subType).to.equal(stringifiedBinaryNewUUIDSubType);
});
});
});