benjamine / jsondiffpatch

Diff & patch JavaScript objects
MIT License
4.83k stars 472 forks source link

Expected null, returns delta on array containing object #18

Closed roboncode closed 11 years ago

roboncode commented 11 years ago
var jsonStr = '{"o":[{"id":1}]}';
var a = JSON.parse(jsonStr);
var b = JSON.parse(jsonStr);
var d = jsondiffpatch.diff(a, b);
console.log(d); // expected null
benjamine commented 11 years ago

Hi Rob, this behavior might not seem intuitive but is by design, when jsondiffpatch finds arrays on both sides, the way to diff arrays is not trivial, elements in the array require an objectHash function to be matched, as there's no simple way to do that for every case. If you don't specify a function to compare items between both versions of the array === operator is used, which for this example clearly is not enough. You can make this work as you expect by telling jsondiffpatch that you want to compare elements by an id field, or by it's full JSON representation:

            jsondiffpatch.config.objectHash = function(obj) {
                return obj._id || obj.id || obj.name || JSON.stringify(obj);
            };

if you do this you'll get no diff, you can test that on the DEMO page, which uses exactly that objectHash function.

I thought in making the default smarter but I'm not sure I should, using id is not general enough and JSON.stringify might be too heavy for a default too.