| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package org.eclipse.parsson; |
| 18 |
|
| 19 |
import jakarta.json.Json; |
| 20 |
import jakarta.json.JsonArray; |
| 21 |
import jakarta.json.JsonException; |
| 22 |
import jakarta.json.JsonObject; |
| 23 |
import jakarta.json.JsonPatch; |
| 24 |
import jakarta.json.JsonPatchBuilder; |
| 25 |
import jakarta.json.JsonPointer; |
| 26 |
import jakarta.json.JsonString; |
| 27 |
import jakarta.json.JsonStructure; |
| 28 |
import jakarta.json.JsonValue; |
| 29 |
import jakarta.json.JsonValue.ValueType; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public class JsonPatchImpl implements JsonPatch { |
| 58 |
|
| 59 |
private final JsonArray patch; |
| 60 |
private final JsonContext jsonContext; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public JsonPatchImpl(JsonArray patch, JsonContext jsonContext) { |
| 67 |
this.patch = patch; |
| 68 |
this.jsonContext = jsonContext; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
@Override |
| 78 |
public boolean equals(Object obj) { |
| 79 |
if (this == obj) |
| 80 |
return true; |
| 81 |
if (obj == null || obj.getClass() != JsonPatchImpl.class) |
| 82 |
return false; |
| 83 |
return patch.equals(((JsonPatchImpl)obj).patch); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
@Override |
| 92 |
public int hashCode() { |
| 93 |
return patch.hashCode(); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
@Override |
| 101 |
public String toString() { |
| 102 |
return patch.toString(); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
@Override |
| 115 |
public JsonStructure apply(JsonStructure target) { |
| 116 |
|
| 117 |
JsonStructure result = target; |
| 118 |
|
| 119 |
for (JsonValue operation: patch) { |
| 120 |
if (operation.getValueType() != ValueType.OBJECT) { |
| 121 |
throw new JsonException(JsonMessages.PATCH_MUST_BE_ARRAY()); |
| 122 |
} |
| 123 |
result = apply(result, (JsonObject) operation); |
| 124 |
} |
| 125 |
return result; |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public JsonArray toJsonArray() { |
| 130 |
return patch; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
static JsonArray diff(JsonStructure source, JsonStructure target, JsonContext jsonContext) { |
| 141 |
return (new DiffGenerator(jsonContext)).diff(source, target); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
private JsonStructure apply(JsonStructure target, JsonObject operation) { |
| 151 |
|
| 152 |
JsonPointer pointer = getPointer(operation, "path"); |
| 153 |
JsonPointer from; |
| 154 |
JsonString op = operation.getJsonString("op"); |
| 155 |
if (op == null) { |
| 156 |
throw new JsonException(JsonMessages.PATCH_OPERATION_MISSING()); |
| 157 |
} |
| 158 |
switch (Operation.fromOperationName(op.getString())) { |
| 159 |
case ADD: |
| 160 |
return pointer.add(target, getValue(operation)); |
| 161 |
case REPLACE: |
| 162 |
return pointer.replace(target, getValue(operation)); |
| 163 |
case REMOVE: |
| 164 |
return pointer.remove(target); |
| 165 |
case COPY: |
| 166 |
from = getPointer(operation, "from"); |
| 167 |
return pointer.add(target, from.getValue(target)); |
| 168 |
case MOVE: |
| 169 |
|
| 170 |
String dest = operation.getString("path"); |
| 171 |
String src = operation.getString("from"); |
| 172 |
if (dest.startsWith(src) && src.length() < dest.length()) { |
| 173 |
throw new JsonException(JsonMessages.PATCH_MOVE_PROPER_PREFIX(src, dest)); |
| 174 |
} |
| 175 |
from = getPointer(operation, "from"); |
| 176 |
|
| 177 |
if (!from.containsValue(target)) { |
| 178 |
throw new JsonException(JsonMessages.PATCH_MOVE_TARGET_NULL(src)); |
| 179 |
} |
| 180 |
if (pointer.equals(from)) { |
| 181 |
|
| 182 |
return target; |
| 183 |
} |
| 184 |
return pointer.add(from.remove(target), from.getValue(target)); |
| 185 |
case TEST: |
| 186 |
if (! getValue(operation).equals(pointer.getValue(target))) { |
| 187 |
throw new JsonException(JsonMessages.PATCH_TEST_FAILED(operation.getString("path"), getValue(operation).toString())); |
| 188 |
} |
| 189 |
return target; |
| 190 |
default: |
| 191 |
throw new JsonException(JsonMessages.PATCH_ILLEGAL_OPERATION(operation.getString("op"))); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
private JsonPointer getPointer(JsonObject operation, String member) { |
| 196 |
JsonString pointerString = operation.getJsonString(member); |
| 197 |
if (pointerString == null) { |
| 198 |
missingMember(operation.getString("op"), member); |
| 199 |
} |
| 200 |
return new JsonPointerImpl(pointerString.getString(), jsonContext); |
| 201 |
} |
| 202 |
|
| 203 |
private JsonValue getValue(JsonObject operation) { |
| 204 |
JsonValue value = operation.get("value"); |
| 205 |
if (value == null) { |
| 206 |
missingMember(operation.getString("op"), "value"); |
| 207 |
} |
| 208 |
return value; |
| 209 |
} |
| 210 |
|
| 211 |
private void missingMember(String op, String member) { |
| 212 |
throw new JsonException(JsonMessages.PATCH_MEMBER_MISSING(op, member)); |
| 213 |
} |
| 214 |
|
| 215 |
static class DiffGenerator { |
| 216 |
private JsonPatchBuilder builder; |
| 217 |
private final JsonContext jsonContext; |
| 218 |
|
| 219 |
DiffGenerator(JsonContext jsonContext) { |
| 220 |
this.jsonContext = jsonContext; |
| 221 |
} |
| 222 |
|
| 223 |
JsonArray diff(JsonStructure source, JsonStructure target) { |
| 224 |
builder = new JsonPatchBuilderImpl(jsonContext); |
| 225 |
diff("", source, target); |
| 226 |
return builder.build().toJsonArray(); |
| 227 |
} |
| 228 |
|
| 229 |
private void diff(String path, JsonValue source, JsonValue target) { |
| 230 |
if (source.equals(target)) { |
| 231 |
return; |
| 232 |
} |
| 233 |
ValueType s = source.getValueType(); |
| 234 |
ValueType t = target.getValueType(); |
| 235 |
if (s == ValueType.OBJECT && t == ValueType.OBJECT) { |
| 236 |
diffObject(path, (JsonObject) source, (JsonObject) target); |
| 237 |
} else if (s == ValueType.ARRAY && t == ValueType.ARRAY) { |
| 238 |
diffArray(path, (JsonArray) source, (JsonArray) target); |
| 239 |
} else { |
| 240 |
builder.replace(path, target); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
private void diffObject(String path, JsonObject source, JsonObject target) { |
| 245 |
source.forEach((key, value) -> { |
| 246 |
if (target.containsKey(key)) { |
| 247 |
diff(path + '/' + Json.encodePointer(key), value, target.get(key)); |
| 248 |
} else { |
| 249 |
builder.remove(path + '/' + Json.encodePointer(key)); |
| 250 |
} |
| 251 |
}); |
| 252 |
target.forEach((key, value) -> { |
| 253 |
if (! source.containsKey(key)) { |
| 254 |
builder.add(path + '/' + Json.encodePointer(key), value); |
| 255 |
} |
| 256 |
}); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
private void diffArray(String path, JsonArray source, JsonArray target) { |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
int m = source.size(); |
| 271 |
int n = target.size(); |
| 272 |
int [][] c = new int[m+1][n+1]; |
| 273 |
for (int i = 0; i < m+1; i++) |
| 274 |
c[i][0] = 0; |
| 275 |
for (int i = 0; i < n+1; i++) |
| 276 |
c[0][i] = 0; |
| 277 |
for (int i = 0; i < m; i++) { |
| 278 |
for (int j = 0; j < n; j++) { |
| 279 |
if (source.get(i).equals(target.get(j))) { |
| 280 |
c[i+1][j+1] = ((c[i][j]) & ~1) + 3; |
| 281 |
|
| 282 |
} else { |
| 283 |
c[i+1][j+1] = Math.max(c[i+1][j], c[i][j+1]) & ~1; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
emit(path, source, target, c, m, n); |
| 289 |
} |
| 290 |
|
| 291 |
private void emit(final String path, |
| 292 |
final JsonArray source, |
| 293 |
final JsonArray target, |
| 294 |
final int[][] c, |
| 295 |
final int i, |
| 296 |
final int j) { |
| 297 |
if (i == 0) { |
| 298 |
if (j > 0) { |
| 299 |
emit(path, source, target, c, i, j - 1); |
| 300 |
builder.add(path + '/' + (j - 1), target.get(j - 1)); |
| 301 |
} |
| 302 |
} else if (j == 0) { |
| 303 |
if (i > 0) { |
| 304 |
builder.remove(path + '/' + (i - 1)); |
| 305 |
emit(path, source, target, c, i - 1, j); |
| 306 |
} |
| 307 |
} else if ((c[i][j] & 1) == 1) { |
| 308 |
emit(path, source, target, c, i - 1, j - 1); |
| 309 |
} else { |
| 310 |
final int f = c[i][j-1] >> 1; |
| 311 |
final int g = c[i-1][j] >> 1; |
| 312 |
if (f > g) { |
| 313 |
emit(path, source, target, c, i, j - 1); |
| 314 |
builder.add(path + '/' + (j - 1), target.get(j - 1)); |
| 315 |
} else if (f < g) { |
| 316 |
builder.remove(path + '/' + (i - 1)); |
| 317 |
emit(path, source, target, c, i - 1, j); |
| 318 |
} else { |
| 319 |
diff(path + '/' + (i - 1), source.get(i - 1), |
| 320 |
target.get(j - 1)); |
| 321 |
emit(path, source, target, c, i - 1, j - 1); |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|