showing results for - "javascript stringify with functions"
Joseph
06 Sep 2016
1function stringifyWithFunctions(object) {
2  return JSON.stringify(object, (key, val) => {
3    if (typeof val === 'function') {
4      return `(${val})`; // make it a string, surround it by parenthesis to ensure we can revive it as an anonymous function
5    }
6    return val;
7  });
8};
9
10function parseWithFunctions(obj) {
11  return JSON.parse(obj, (k, v) => {
12    if (typeof v === 'string' && v.indexOf('function') >= 0) {
13      return eval(v);
14    }
15    return v;
16  });
17};
18