josephburnett / jd

JSON diff and patch
MIT License
1.86k stars 51 forks source link

Patches for zero-length arrays to add items misconfigured #6

Closed jkomoros closed 6 years ago

jkomoros commented 6 years ago

Contents of a.json:

{
    "array": []
}

Contents of b.json:

{
    "array": [3,4,5]
}

Execute: jd a.json b.json > diff.patch

Contents of diff.patch:

@ ["array",2]
+ 5
@ ["array",1]
+ 4
@ ["array",0]
+ 3

Execute: jd -p diff.patch a.json

Result (error): Addition beyond the terminal elemtn of an array.

Expected result: {"array":[3,4,5]}

However, if you manually edit the contents of diff.patch to:

@ ["array",0]
+ 3
@ ["array",1]
+ 4
@ ["array",2]
+ 5

Then it works as expected.

It seems like the diff for this case outputs the array indexes in reverse order.

It's entirely possible I'm holding it wrong. Sorry for the noise if so!

josephburnett commented 6 years ago

You are correct. It's emitting the patch chunks in the wrong order such that they cannot be applied. Good catch!

josephburnett commented 6 years ago

@jkomoros, thanks for the report. I've submitted a change to fix this.