Skip to content

Commit

Permalink
lib: remove visited objects from stack
Browse files Browse the repository at this point in the history
Adding visited objects in the stack was helpful
in detecting circular references. However, the
objects which were processed were never removed.
This causes unexpected issues in stringification.

Fixes: #24
PR-URL: #30
Reviewed-By: Harshitha K P <harshitha014@gmail.com>
Reviewed-By: Ravali Yatham <yatraval@in.ibm.com>
  • Loading branch information
gireeshpunathil committed Dec 1, 2020
1 parent fc2deec commit 5809572
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yieldable-json",
"version": "2.0.0",
"version": "2.0.1",
"main": "index.js",
"description": "An asynchronous yieldable version of JSON.stringify and JSON.parse",
"author": "Gireesh Punathil <gpunathi@in.ibm.com>",
Expand Down
23 changes: 23 additions & 0 deletions test/test-stringify-same-object-twice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const yj = require('../index');
const tap = require('tap');

const obj = {
name: 'Jacqueline Poole',
gender: 'female',
age: 40,
};

const master = {arr: [ { a: obj }, { b: obj} ] };

// Make sure presence of obj twice in the master
// object does not cause revisit issues while
// stringifying it - such as circular dependency

yj.stringifyAsync(obj, (err, str) => {
if (!err) {
tap.ok(true, 'Repeated object presence cause no issues');
} else
tap.fail(err);
});
2 changes: 2 additions & 0 deletions yieldable-stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ function * stringifyYield(field, container, replacer, space, intensity) {
? ': '
: ':') + val);
}
objStack = objStack.filter((v, i, a) => { return v !== value[key] });
}
objStack = objStack.filter((v, i, a) => { return v !== value });
}
return getResult(true);
default:
Expand Down

0 comments on commit 5809572

Please sign in to comment.