0

I want to override JSON.stringify to use the json-stringify-safe module to avoid getting circular error.

Here is my code :

(function () {
    const getSerialize = require('json-stringify-safe');
    const stringifyCore = JSON.stringify;
    JSON.stringify = (obj, replacer, spaces, cycleReplacer) => {
        return stringifyCore.apply(this, [obj, getSerialize(replacer, cycleReplacer), spaces]);
    };
}());

I also tried :

const getSerialize = require('json-stringify-safe');

const stringifyCore = JSON.stringify;
JSON.stringify = function (obj, replacer, spaces, cycleReplacer) {
    return stringifyCore(obj, getSerialize(replacer, cycleReplacer), spaces);
};

I get an error :

Maximum call stack size exceeded

The solution eplanied here does not work for me.. Overriding JSON.stringify causing error

Any ideas?

Simax77
  • 11
  • 3
  • You probably don't want to use arrow functions if you need to use `this`. – Jeff Mercado Nov 27 '20 at 02:36
  • in my second code, I'm not using arrow function and not even this – Simax77 Nov 27 '20 at 02:46
  • What is the goal of this implementation? I'm looking at the [readme](https://github.com/moll/json-stringify-safe) and it looks like it should be a drop in replacement. Looks like you're trying to do something else. – Jeff Mercado Nov 27 '20 at 02:53
  • You probably meant to call the `getSerialize` function from the function. Perhaps you need to do `const {getSerialize} = require(...);` – Jeff Mercado Nov 27 '20 at 02:54
  • The goal of this implementation is to continue using JSON.stringify in my application, instead of changing all references to the stringify function from json-stringify-safe module – Simax77 Nov 27 '20 at 03:01
  • The uses of const {getSerialize} = require(...); fixed the issue. But now I have another error.. it looks like it's not replacing the original stringify function... I get the 'Converting Circular structure to JSON' error. Is this applying on all my application or just in my actual file? – Simax77 Nov 27 '20 at 03:06

3 Answers3

0

First problem is solved by changing the requires for getSerialize function to :

const {getSerialize} = require('json-stringify-safe');

Now, I have another problem, I want this to be applied in all my application, but it doesn't seems to work since I continue to get this error :

Converting circular structure to Json

Simax77
  • 11
  • 3
0

I think this is what you want.

(function () {
    const {getSerialize} = require('json-stringify-safe');
    const _stringify = JSON.stringify;
    JSON.stringify = (obj, replacer, spaces, cycleReplacer) =>
        _stringify(obj, getSerialize(replacer, cycleReplacer), spaces);
}());

The point of the library is to use it instead of the base JSON.stringify() function. And judging by the code, it internally uses JSON.stringify already so we'll want to bypass that.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
0

Ooh Just figured out that I was not int the right file.. I was trying this on webpack.config.js (This is an AngularJS project). I moved it in my app.js file and it works.

Here is my solution in App.js

(function () {
    const {getSerialize} = require('json-stringify-safe');
    
    const stringifyCore = JSON.stringify;
    JSON.stringify = function (obj, replacer, spaces, cycleReplacer) {
        return stringifyCore(obj, getSerialize(replacer, cycleReplacer), spaces);
    };
}());
Simax77
  • 11
  • 3