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

16 lines
297 B
TypeScript
Raw Normal View History

2022-02-20 11:51:55 +00:00
export const safeJSONParse = (str, defaultValue = {}) => {
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;
}
};