* Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
*/
const removePrototype = (obj: any) => {
if (typeof obj !== 'object') {
return;
}
for (const key in obj) {
if (key === '__proto__' || key === 'constructor') {
delete obj[key];
} else if (typeof obj === 'object') {
removePrototype(obj[key]);
}
}
};
export const safeJSONParse = (str: any, defaultValue: any = null) => {
try {
const res = JSON.parse(str);
removePrototype(res);
return res;
} catch {
return defaultValue;
}
};