mirror of https://github.com/fantasticit/think.git
18 lines
341 B
TypeScript
18 lines
341 B
TypeScript
export const safeJSONParse = (str, defaultValue = {}) => {
|
|
if (typeof str === 'object') return str;
|
|
|
|
try {
|
|
return JSON.parse(str);
|
|
} catch (e) {
|
|
return defaultValue;
|
|
}
|
|
};
|
|
|
|
export const safeJSONStringify = (obj, defaultValue = '{}') => {
|
|
try {
|
|
return JSON.stringify(obj);
|
|
} catch (e) {
|
|
return defaultValue;
|
|
}
|
|
};
|