diff --git a/packages/core/lib/segments/attributes/subsegment.js b/packages/core/lib/segments/attributes/subsegment.js index 4101662e..b46a52f6 100644 --- a/packages/core/lib/segments/attributes/subsegment.js +++ b/packages/core/lib/segments/attributes/subsegment.js @@ -432,13 +432,10 @@ Subsegment.prototype.toJSON = function toJSON() { * Returns the serialized subsegment JSON string, replacing any BigInts with strings. */ Subsegment.prototype.serialize = function serialize(object) { - return JSON.stringify(object ?? this, (_, value) => { - if (typeof value === 'bigint') { - return value.toString(); - } - - return value; - }); + return JSON.stringify( + object ?? this, + SegmentUtils.getJsonStringifyReplacer() + ); }; module.exports = Subsegment; diff --git a/packages/core/lib/segments/segment.js b/packages/core/lib/segments/segment.js index 7dc3f90a..b8f24533 100644 --- a/packages/core/lib/segments/segment.js +++ b/packages/core/lib/segments/segment.js @@ -439,13 +439,10 @@ Segment.prototype.toString = function toString() { }; Segment.prototype.serialize = function serialize(object) { - return JSON.stringify(object ?? this, (_, value) => { - if (typeof value === 'bigint') { - return value.toString(); - } - - return value; - }); + return JSON.stringify( + object ?? this, + SegmentUtils.getJsonStringifyReplacer() + ); }; module.exports = Segment; diff --git a/packages/core/lib/segments/segment_utils.d.ts b/packages/core/lib/segments/segment_utils.d.ts index 197eb823..fd3a51f6 100644 --- a/packages/core/lib/segments/segment_utils.d.ts +++ b/packages/core/lib/segments/segment_utils.d.ts @@ -17,3 +17,5 @@ export function setStreamingThreshold(threshold: number): void; export function getStreamingThreshold(): number; export function getHttpResponseData(res: http.ServerResponse): object; + +export function getJsonStringifyReplacer(): (key: string, value: any) => any; diff --git a/packages/core/lib/segments/segment_utils.js b/packages/core/lib/segments/segment_utils.js index eb2cbb78..03514b68 100644 --- a/packages/core/lib/segments/segment_utils.js +++ b/packages/core/lib/segments/segment_utils.js @@ -67,6 +67,14 @@ var utils = { ret.content_length = safeParseInt(res.headers['content-length']); } return ret; + }, + + getJsonStringifyReplacer: () => (_, value) => { + if (typeof value === 'bigint') { + return value.toString(); + } + + return value; } }; diff --git a/packages/core/test/unit/segments/segment_utils.test.js b/packages/core/test/unit/segments/segment_utils.test.js index fb0b747d..8b8cefff 100644 --- a/packages/core/test/unit/segments/segment_utils.test.js +++ b/packages/core/test/unit/segments/segment_utils.test.js @@ -43,4 +43,14 @@ describe('SegmentUtils', function() { assert.deepEqual(emptyRes, {}); }); }); + + describe('#getJsonStringifyReplacer', () => { + it('should stringify BigInts', () => { + const obj = {foo: 1n, bar: BigInt(2)}; + const replacer = SegmentUtils.getJsonStringifyReplacer(); + const result = JSON.stringify(obj, replacer); + + assert.equal(result, '{"foo":"1","bar":"2"}'); + }); + }); });