Skip to content
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
1 change: 1 addition & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 9.1.0 (prerelease)

- [#697](https://github.com/kpdecker/jsdiff/pull/697) *`diffJson` now correctly handles JSON objects containing a key named `__proto__`*. (Previously, the returned diff would be as if the `__proto__` key did not exist on either of the objects being diffed.)
- [#700](https://github.com/kpdecker/jsdiff/pull/700) *`diffJson` now correctly handles JSON objects containing a non-callable property named `toJSON`* - i.e. it gives such a property no special behaviour whatsoever, just as `JSON.stringify` doesn't. Previously, such properties caused an error to be thrown. (*Callable* `toJSON` properties continue to get the same special behaviour that `JSON.stringify` gives them.)

## 9.0.0

Expand Down
2 changes: 1 addition & 1 deletion src/diff/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function canonicalize(
return canonicalizedObj;
}

if (obj && obj.toJSON) {
if (obj && typeof obj.toJSON === 'function') {
obj = obj.toJSON();
}

Expand Down
32 changes: 32 additions & 0 deletions test/diff/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,38 @@ describe('diff/json', function() {
{ count: 1, value: '}', removed: false, added: false }
]);
});

it('handles custom toJSON methods like JSON.stringify does', function() {
const x = {
toJSON: () => 'aaa'
};
const y = {
toJSON: () => 'bbb'
};

expect(diffJson({ foo: x }, {foo: y})).to.eql([
{ count: 1, value: '{\n', removed: false, added: false },
{ count: 1, value: ' "foo": "aaa"\n', added: false, removed: true },
{ count: 1, value: ' "foo": "bbb"\n', added: true, removed: false },
{ count: 1, value: '}', removed: false, added: false }
]);
});

it('treats non-callable toJSON properties as normal properties (like JSON.stringify does)', function() {
const x = {
toJSON: 'aaa'
};
const y = {
toJSON: 'bbb'
};

expect(diffJson(x, y)).to.eql([
{ count: 1, value: '{\n', removed: false, added: false },
{ count: 1, value: ' "toJSON": "aaa"\n', added: false, removed: true },
{ count: 1, value: ' "toJSON": "bbb"\n', added: true, removed: false },
{ count: 1, value: '}', removed: false, added: false }
]);
});
});

describe('#canonicalize', function() {
Expand Down