mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-20 08:47:44 +00:00
Fix circular reference JSON serialization in telemetry logging (#4150)
This commit is contained in:
32
packages/core/src/utils/safeJsonStringify.ts
Normal file
32
packages/core/src/utils/safeJsonStringify.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Safely stringifies an object to JSON, handling circular references by replacing them with [Circular].
|
||||
*
|
||||
* @param obj - The object to stringify
|
||||
* @param space - Optional space parameter for formatting (defaults to no formatting)
|
||||
* @returns JSON string with circular references replaced by [Circular]
|
||||
*/
|
||||
export function safeJsonStringify(
|
||||
obj: unknown,
|
||||
space?: string | number,
|
||||
): string {
|
||||
const seen = new WeakSet();
|
||||
return JSON.stringify(
|
||||
obj,
|
||||
(key, value) => {
|
||||
if (typeof value === 'object' && value !== null) {
|
||||
if (seen.has(value)) {
|
||||
return '[Circular]';
|
||||
}
|
||||
seen.add(value);
|
||||
}
|
||||
return value;
|
||||
},
|
||||
space,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user