Skip to content Skip to sidebar Skip to footer

Overriding Json.stringify Causing Error

I just created a function that return JSON.stringify like this: JSON.stringify = function (item, replacer, space) { return JSON.stringify(item, replacer, space); } and it cau

Solution 1:

It is a recursive function without any end condition. Either you can solve the problem with creating a local function:

var localStringify = function (item, replacer, space) {
    ...
}

Or override the original function considering the explanation given in this thread.

Solution 2:

Thanks all, I resolved creating a local variable with the original function

var originalStringify = JSON.stringify;

and then calling on my custom function

return originalStringify(item, replacer, space);

Now my full Json ignore override function is like this:

// JSON ignorevar original = JSON.stringify;

JSON.stringify = function (item, replacer, space) {
    if (angular.isObject(item)) {
        var newItem = angular.copy(item);
        var ignores = item.jsonIgnore || [];

        ignores.forEach(prop => {
            delete newItem[prop];
            delete newItem.jsonIgnore;
        });

        returnoriginal(newItem, replacer, space);
    }

    returnoriginal(item, replacer, space);
}

Post a Comment for "Overriding Json.stringify Causing Error"