jsondiffpatch.net icon indicating copy to clipboard operation
jsondiffpatch.net copied to clipboard

Array detectMove

Open akrobet opened this issue 7 years ago • 8 comments

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 ] }

akrobet avatar Mar 28 '17 12:03 akrobet

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 avatar Apr 09 '17 20:04 arbitertl

@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.

wbish avatar Apr 12 '17 20:04 wbish

I'd be happy to have a go at this.

Did you have any specific approach in mind, or shall I just forge ahead?

majdisorder avatar Jun 02 '18 13:06 majdisorder

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!

wbish avatar Jun 02 '18 18:06 wbish

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.

majdisorder avatar Jun 02 '18 21:06 majdisorder

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?

majdisorder avatar Jun 04 '18 14:06 majdisorder

@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.

butlerpeter avatar Sep 12 '18 15:09 butlerpeter

Is there any update on this?

We need to ignore order when diffing arrays, but don't want to sort each array first

sebasijan avatar Dec 24 '21 12:12 sebasijan