zjsonpatch icon indicating copy to clipboard operation
zjsonpatch copied to clipboard

bug in replace array in json

Open mohamadk opened this issue 8 years ago • 3 comments

hi, thanks for this great library. when I'm getting different between this two jsons

   "{\"b\": [1,2,4,9]}"

and

  "{\"b\": [6,2,4,9]}"

the difference value currectly return operation replace in location 0.

but if I get diff this two jsons

 "{\"b\": [2,2,5,6]}"

and

  "{\"b\": [4,2,5,6]}"

it returns add and remove operation in 0 and 2 positions. I think the reason of this bug is because of two number 2 .

mohamadk avatar Aug 04 '16 10:08 mohamadk

Agreed, thanks for pointing out the issue. Clearly, the diffs generated are not optimized in this case. I will fix this once i get some space to work on this project, Moreover, i would be glad to accept PR for same.

vishwakarma avatar Aug 04 '16 16:08 vishwakarma

Hi, I've looked at the code a bit and I think the underlying issue is that REPLACE operations are not collapsed. Take the following arrays:

source: [1, 2, 3, 4, 5] target: [5, 1, 4, 3, 2] lcs: [1, 2] OR [1, 4] or [1, 3] would also be valid

You will get, REMOVE 1, REPLACE 2 5, ADD 1, REPLACE 3, 4, REPLACE 4, 3, REMOVE 5, ADD 2

To reduce this into moves you would need the following: REMOVE 1, ADD 1 -> MOVE 1 REMOVE 5, REPLACE 2 5, ADD, 2 -> MOVE 5 REPLACE 3, 4, REPLACE 4, 3 -> MOVE 3

Obviously you need to keep track of coordinates as well. The optimization that is currently in place only collapses REMOVE and ADD into MOVE, it doesn't take into account these chains of replacements.

Sadly I won't have enough time to code this myself right now, but thought it might help if someone else wanted to tackle it.

nebevay avatar Oct 26 '16 10:10 nebevay

What about this bug? It's been created more than 3 years ago

aantoniuk-gl avatar Nov 29 '19 18:11 aantoniuk-gl