diffJson would call toJSON when it's truthy, even if it's not callable. This can cause diffJson to throw on valid JSON objects. JSON.stringify only calls toJSON if it's callable.
Reproduced on the latest v9.0.0. Happening to the latest master as well.
Reproduction:
https://github.com/aforemendude/bugs-reproduction/tree/main/jsdiff-non-callable-tojson
import { diffJson } from 'diff'
const oldObject = JSON.parse('{"toJSON":"old"}')
const newObject = JSON.parse('{"toJSON":"new"}')
try {
console.log(diffJson(oldObject, newObject))
} catch (error) {
console.log(`${error.name}: ${error.message}`)
}
Output:
TypeError: obj.toJSON is not a function
diffJsonwould calltoJSONwhen it's truthy, even if it's not callable. This can causediffJsonto throw on valid JSON objects.JSON.stringifyonly callstoJSONif it's callable.Reproduced on the latest
v9.0.0. Happening to the latestmasteras well.Reproduction:
https://github.com/aforemendude/bugs-reproduction/tree/main/jsdiff-non-callable-tojson
Output: