debitoor / safe-json-stringify

A wrapper for JSON.stringify that handles circular references and prevent defined getters from throwing errors.
https://www.npmjs.com/package/safe-json-stringify
56 stars 16 forks source link

Doesn't detect circular references correctly #5

Closed MikeRalphson closed 6 years ago

MikeRalphson commented 7 years ago

What it detects is object identity over objects it has seen before.

Consider the following example

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

const a = { test: 1 };
const b = { c: a, d: a};

console.log(sjs(b));
console.log(JSON.stringify(b));

The output is

{"c":{"test":1},"d":"[Circular]"}
{"c":{"test":1},"d":{"test":1}}

Whereas I would expect

{"c":{"test":1},"d":{"test":1}}
{"c":{"test":1},"d":{"test":1}}

It correctly identifies real circular references.

MikeRalphson commented 7 years ago

PR in progress.

ath88 commented 6 years ago

@MikeRalphson Any update?

MikeRalphson commented 6 years ago

My fix is here: https://github.com/MikeRalphson/safe-json-stringify/commit/754b8f5084ac4126dfc5f5c487d67336b461b486

It needs whitespace cleanup and I'm not sure if its the cleanest way of doing things (placement of the extra pops).

@gausby @jonatanpedersen any comments?

jonatanpedersen commented 6 years ago

@MikeRalphson feel free to create a pull request :)