think/packages/client/src/helpers/json.ts

18 lines
341 B
TypeScript
Raw Normal View History

2022-02-20 11:51:55 +00:00
export const safeJSONParse = (str, defaultValue = {}) => {
if (typeof str === 'object') return str;
2022-02-20 11:51:55 +00:00
try {
return JSON.parse(str);
} catch (e) {
return defaultValue;
}
};
2022-04-25 13:12:26 +00:00
export const safeJSONStringify = (obj, defaultValue = '{}') => {
try {
return JSON.stringify(obj);
} catch (e) {
return defaultValue;
}
};