JSON.stringify(value)
JSON.stringify(value, replacer)
JSON.stringify(value, replacer, space)
function stringifyWithFunctions(object) {
return JSON.stringify(object, (key, val) => {
if (typeof val === 'function') {
return `(${val})`; // make it a string, surround it by parenthesis to ensure we can revive it as an anonymous function
}
return val;
});
};
function parseWithFunctions(obj) {
return JSON.parse(obj, (k, v) => {
if (typeof v === 'string' && v.indexOf('function') >= 0) {
return eval(v);
}
return v;
});
};
// It helps to turn JS object or value into string
// Syntax:
JSON.stringify(value);
JSON.stringify(value, replacer);
JSON.stringify(value, replacer, space);