Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent infinite recursions when using refsoption as false #27

Merged
merged 2 commits into from
Dec 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/cycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ exports.decycle = function decycle(object, options, replacer) {

var map = new WMap()

var hasCircular = Object.prototype.hasOwnProperty.call(options, 'circular');
var noCircularOption = !Object.prototype.hasOwnProperty.call(options, 'circular');
var withRefs = options.refs !== false;

return (function derez(_value, path, key) {

Expand Down Expand Up @@ -104,15 +105,21 @@ exports.decycle = function decycle(object, options, replacer) {
// If the value is an object or array, look to see if we have already
// encountered it. If so, return a $ref/path object.

if (typeof value === 'object' && options['refs']) {
if (typeof value === 'object') {
var foundPath = map.get(value);
if (foundPath) {
if (hasCircular && path.indexOf(foundPath) === 0) {
return typeof options.circular === 'function'?
if (noCircularOption && withRefs) {
return {$jsan: foundPath};
}
if (path.indexOf(foundPath) === 0) {
if (!noCircularOption) {
return typeof options.circular === 'function'?
options.circular(value, path, foundPath):
options.circular;
}
return {$jsan: foundPath};
}
return {$jsan: foundPath};
if (withRefs) return {$jsan: foundPath};
}
map.set(value, path);
}
Expand Down
1 change: 0 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ exports.stringify = function stringify(value, replacer, space, _options) {
'infinity': options
}
}
if (typeof options.refs === 'undefined') options.refs = true;

var decycled = cycle.decycle(value, options, replacer);
if (arguments.length === 1) {
Expand Down
10 changes: 10 additions & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ describe('jsan', function() {
assert.equal(jsan.stringify(obj, null, null, {refs: false}), '{"prop1":{"a":1},"prop2":{"prop3":{"a":1}}}');
});

it('works with refs option with circular references', function() {
var obj = {};
obj.self = obj;
obj.a = 1;
obj.b = {t: 1};
obj.c = obj.b;
assert.equal(jsan.stringify(obj, null, null, {refs: false}), '{"self":{"$jsan":"$"},"a":1,"b":{"t":1},"c":{"t":1}}');
assert.equal(jsan.stringify(obj, null, null, {refs: false, circular: 'Circular'}), '{"self":"Circular","a":1,"b":{"t":1},"c":{"t":1}}');
});

it('works on objects with "[", "\'", and "]" in the keys', function() {
var obj = {};
obj['["key"]'] = {};
Expand Down