GeezerVison is showing github.com — Fixed issue #61 · javaee/jsonp@b32c08b
This repository was archived by the owner on Nov 7, 2024. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings impl/src/main/java/org/glassfish/json
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: impl/src/main/java/org/glassfish/json/JsonPatchImpl.java +36-26Lines changed: 36 additions & 26 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -289,32 +289,42 @@ private void diffArray(String path, JsonArray source, JsonArray target) { | 289 | 289 | } | 290 | 290 | } | 291 | 291 |
| 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 | + } | 318 | 328 | } | 319 | 329 | } | 320 | 330 | } | |
Collapse file Copy file name to clipboardExpand all lines: tests/src/test/resources/jsonpatchdiff.json +34-6Lines changed: 34 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | "original": [ 1, 2, 3 ], | 9 | 9 | "target": [ 1, 2, 3, 4, 5 ], | 10 | 10 | "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} | 13 | 13 | ] | 14 | 14 | }, | 15 | 15 | { | | 25 | 25 | "target": [1,7,3,4,8,5], | 26 | 26 | "expected": [ | 27 | 27 | { "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} | 30 | 30 | ] | 31 | 31 | }, | 32 | 32 | { | | 94 | 94 | "a": [ "b", 2, 3, 4 ] | 95 | 95 | }, | 96 | 96 | "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 } | 99 | 99 | ] | 100 | 100 | }, | 101 | 101 | { | | 128 | 128 | "expected": [ | 129 | 129 | { "op": "add", "path": "/d", "value": "c" } | 130 | 130 | ] | | 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 | + ] | 131 | 159 | } | 132 | 160 | ] |
|