Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 7, 2024. It is now read-only.

Commit b7bec3e

Browse files
wdonnem0mus
authored andcommitted
Fix for issue #61 (#74)
1 parent 7345139 commit b7bec3e

File tree

2 files changed

+70
-32
lines changed

2 files changed

+70
-32
lines changed

‎impl/src/main/java/org/glassfish/json/JsonPatchImpl.java

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -289,32 +289,42 @@ private void diffArray(String path, JsonArray source, JsonArray target) {
289289
}
290290
}
291291

292-
int i = m;
293-
int j = n;
294-
while (i > 0 || j > 0) {
295-
if (i == 0) {
296-
j--;
297-
builder.add(path + '/' + j, target.get(j));
298-
} else if (j == 0) {
299-
i--;
300-
builder.remove(path + '/' + i);
301-
} else if ((c[i][j] & 1) == 1) {
302-
i--; j--;
303-
} else {
304-
int f = c[i][j-1] >> 1;
305-
int g = c[i-1][j] >> 1;
306-
if (f > g) {
307-
j--;
308-
builder.add(path + '/' + j, target.get(j));
309-
} else if (f < g) {
310-
i--;
311-
builder.remove(path + '/' + i);
312-
} else { // f == g) {
313-
i--; j--;
314-
diff(path + '/' + i, source.get(i), target.get(j));
315-
}
316-
}
317-
}
292+
emit(path, source, target, c, m, n);
293+
}
294+
295+
private void emit(final String path,
296+
final JsonArray source,
297+
final JsonArray target,
298+
final int[][] c,
299+
final int i,
300+
final int j) {
301+
if (i == 0) {
302+
if (j > 0) {
303+
emit(path, source, target, c, i, j - 1);
304+
builder.add(path + '/' + (j - 1), target.get(j - 1));
305+
}
306+
} else if (j == 0) {
307+
if (i > 0) {
308+
builder.remove(path + '/' + (i - 1));
309+
emit(path, source, target, c, i - 1, j);
310+
}
311+
} else if ((c[i][j] & 1) == 1) {
312+
emit(path, source, target, c, i - 1, j - 1);
313+
} else {
314+
final int f = c[i][j-1] >> 1;
315+
final int g = c[i-1][j] >> 1;
316+
if (f > g) {
317+
emit(path, source, target, c, i, j - 1);
318+
builder.add(path + '/' + (j - 1), target.get(j - 1));
319+
} else if (f < g) {
320+
builder.remove(path + '/' + (i - 1));
321+
emit(path, source, target, c, i - 1, j);
322+
} else { // f == g) {
323+
diff(path + '/' + (i - 1), source.get(i - 1),
324+
target.get(j - 1));
325+
emit(path, source, target, c, i - 1, j - 1);
326+
}
327+
}
318328
}
319329
}
320330
}

‎tests/src/test/resources/jsonpatchdiff.json

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"original": [ 1, 2, 3 ],
99
"target": [ 1, 2, 3, 4, 5 ],
1010
"expected": [
11-
{"op":"add","path":"/4","value":5},
12-
{"op":"add","path":"/3","value":4}
11+
{"op":"add","path":"/3","value":4},
12+
{"op":"add","path":"/4","value":5}
1313
]
1414
},
1515
{
@@ -25,8 +25,8 @@
2525
"target": [1,7,3,4,8,5],
2626
"expected": [
2727
{ "op": "remove", "path": "/5"},
28-
{ "op": "add", "path": "/4", "value": 8},
29-
{ "op": "replace", "path": "/1", "value": 7}
28+
{ "op": "replace", "path": "/1", "value": 7},
29+
{ "op": "add", "path": "/4", "value": 8}
3030
]
3131
},
3232
{
@@ -94,8 +94,8 @@
9494
"a": [ "b", 2, 3, 4 ]
9595
},
9696
"expected": [
97-
{ "op": "add", "path": "/a/3", "value":4 },
98-
{ "op": "replace", "path": "/a/0", "value":"b" }
97+
{ "op": "replace", "path": "/a/0", "value":"b" },
98+
{ "op": "add", "path": "/a/3", "value":4 }
9999
]
100100
},
101101
{
@@ -128,5 +128,33 @@
128128
"expected": [
129129
{ "op": "add", "path": "/d", "value": "c" }
130130
]
131+
},
132+
{
133+
"original": [-1, 0, 1, 3, 4],
134+
"target": [5, 0],
135+
"expected": [
136+
{ "path" : "/4", "op" : "remove"},
137+
{ "path" : "/3", "op" : "remove"},
138+
{ "path" : "/2", "op" : "remove"},
139+
{ "value" : 5, "path" : "/0", "op" : "replace" }
140+
]
141+
},
142+
{
143+
"original": [0],
144+
"target": [0, 1, 2, 3, 4],
145+
"expected": [
146+
{ "path" : "/1", "value" : 1, "op" : "add" },
147+
{ "path" : "/2", "value" : 2, "op" : "add" },
148+
{ "value" : 3, "path" : "/3", "op" : "add" },
149+
{ "op" : "add", "path" : "/4", "value" : 4 }
150+
]
151+
},
152+
{
153+
"original": [0, 2, 4],
154+
"target": [0, 1, 2, 3, 4],
155+
"expected": [
156+
{ "path" : "/1", "value" : 1, "op" : "add" },
157+
{ "value" : 3, "op" : "add", "path" : "/3" }
158+
]
131159
}
132160
]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /