I have tried to convert a JS object into JSON.
JSON.stringify({a:1, toJSON: function(){}})
Native JSON stringify is not working as expected. JSON stringify executes toJSON function in JS object internally. I have overwritten native code as follows,
// Adding try catch for non JSON support browsers.
try{
_jsonStringify = JSON.stringify;
JSON.stringify = function(object){
var fnCopy = object.toJSON;
object.toJSON = undefined;
var result = _jsonStringify(object);
object.toJSON = fnCopy;
return result;
};
}catch(e){}
It is working fine. is there any other better way to do this?. is there any specific reason in native code execute toJSON function in input object?