Most of them won't recommend persisting function(behavior) inside JSON object, that is intended to carry only data.
In case if you want to serialize the object with function, you need to override them, the below code would help you that.
This will help you to serialize function.
var json = function(obj){ return JSON.stringify(obj, function(key, value) {
if (typeof value === 'function') {
return value.toString();
} else {
return value;
}
})};
I've created an object rama for testing, the output of json(rama) would be
"{\"myName\":\"Ramasamy Kasiviswanathan\",\"myfunction\":\"function(input){\\nconsole.log('input', input);\\n}\"}"
Storing in localStorage,
localStorage.setItem('ramaLocal',json(rama));
Retrieving value from LocalStorage,
ramadeserialize = eval("JSON.parse(localStorage.getItem('ramaLocal'))");
O/P would be:
Object { myName: "Ramasamy Kasiviswanathan", myfunction: "function(input){\nconsole.log('input', input);\n}" }
Reference:
json.stringify does not process object methods