mirror of
https://github.com/csunny/DB-GPT.git
synced 2026-07-17 18:28:42 +00:00
Co-authored-by: lusain <lusain1990@gmail.com> Co-authored-by: alan.cl <1165243776@qq.com> Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
13 lines
319 B
TypeScript
13 lines
319 B
TypeScript
/**
|
|
* Utility function to safely parse JSON strings.
|
|
* When parsing fails, it returns a default value or null.
|
|
*/
|
|
export function safeJsonParse<T>(jsonString: string, def: T): T {
|
|
try {
|
|
return JSON.parse(jsonString);
|
|
} catch (error) {
|
|
console.error('Failed to parse JSON:', error);
|
|
return def;
|
|
}
|
|
}
|