jsondiffpatch.net
jsondiffpatch.net copied to clipboard
Array detectMove
Hi, Any chance you could add a detectMove parameter for array comparison, so reordered arrays with matching elements are not detected as a difference? i.e. these two objects would match?
{'name':'John Doe','positions':[ 1, 2, 3 ] } {'name':'John Doe','positions':[ 1, 3, 2 ] }
I might be wrong, but this is not the purpose of that library.
Though you can easily achieve that by first bringing JSON to some canonical form (e.g. sort all arrays in your case).
@arbitertl I think it makes sense to add this. jsondiffpatch.net is just a port of benjamine/jsondiffpatch after all and that library does support this feature. If any of you guys want to write the code, feel free, if not I'll eventually get around to it when I have more free time.
I'd be happy to have a go at this.
Did you have any specific approach in mind, or shall I just forge ahead?
Hey @dimi3tron, thanks for volunteering! I don't have specific requirements, but as a guiding principle, I would really love it if you could implement in a way that mimics the default behavior of the original library. Thanks!
Okay. I'm going by https://github.com/benjamine/jsondiffpatch/blob/master/docs/arrays.md. It pretty much describes what I had in mind anyway.
I implemented the basic detectmove algorithm. I works fine for arrays of primitive types and strings. I haven't gotten around to implementing objecthash, so arrays of objects are still returned as arrays of diffed objects.
I'd have to look at the original code, but I suspect some refactoring will need to be done to get this in place. Should we go by the same definition of objecthash as the orginal project? It shouldn't be too hard to elaborate and use some .net features to get a better hash.
How do I go about committing my changes?
@dimi3tron Do you have your changes available in a repository somewhere?
This library looks to be ideal for a project I'm working on but I really need the array move detection.
Is there any update on this?
We need to ignore order when diffing arrays, but don't want to sort each array first