Skip to content

Commit e0b3f21

Browse files
committed
Merge pull request #12 from rwatler/refactored
Alternative fix to issue #11 using the refactored diff implementation.
2 parents 3e7bca3 + 9435520 commit e0b3f21

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

src/main/java/com/github/fge/jsonpatch/diff/Diff.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,12 @@ static Diff simpleDiff(final DiffOperation operation,
5151
* "Stateless" removal of a given node from an array given a base path (the
5252
* immediate parent of an array) and an array index; as the name suggests,
5353
* this factory method is called only when a node is removed from the tail
54-
* of a target array; in other words, the source node has extra elements,
55-
* and the only relevant information for generating the removal operation is
56-
* the index in the source array.
54+
* of a target array; in other words, the source node has extra elements.
5755
*/
5856
static Diff tailArrayRemove(final JsonPointer basePath, final int index,
59-
final JsonNode victim)
57+
final int removeIndex, final JsonNode victim)
6058
{
61-
return new Diff(DiffOperation.REMOVE, basePath, index, index,
59+
return new Diff(DiffOperation.REMOVE, basePath, index, removeIndex,
6260
victim.deepCopy());
6361
}
6462

src/main/java/com/github/fge/jsonpatch/diff/IndexedJsonArray.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,9 @@ boolean isEmpty()
6868
{
6969
return index >= size;
7070
}
71+
72+
int size()
73+
{
74+
return size;
75+
}
7176
}

src/main/java/com/github/fge/jsonpatch/diff/JsonDiff.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ private static void postLCS(final List<Diff> diffs, final JsonPointer path,
397397
target.shift();
398398
}
399399
addRemaining(diffs, path, target);
400-
removeRemaining(diffs, path, source);
400+
removeRemaining(diffs, path, source, target.size());
401401
}
402402

403403
private static void addRemaining(final List<Diff> diffs,
@@ -415,16 +415,16 @@ private static void addRemaining(final List<Diff> diffs,
415415
}
416416

417417
private static void removeRemaining(final List<Diff> diffs,
418-
final JsonPointer path, final IndexedJsonArray array)
418+
final JsonPointer path, final IndexedJsonArray array,
419+
final int removeIndex)
419420
{
420-
final int startingIndex = array.getIndex();
421-
422421
Diff diff;
423422
JsonNode node;
424423

425424
while (!array.isEmpty()) {
426425
node = array.getElement();
427-
diff = Diff.tailArrayRemove(path, startingIndex, node);
426+
diff = Diff.tailArrayRemove(path, array.getIndex(),
427+
removeIndex, node);
428428
diffs.add(diff);
429429
array.shift();
430430
}

0 commit comments

Comments
 (0)