| 1 | /* |
| 2 | * Copyright (c) 2015, 2021 Oracle and/or its affiliates. All rights reserved. |
| 3 | * |
| 4 | * This program and the accompanying materials are made available under the |
| 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | * http://www.eclipse.org/legal/epl-2.0. |
| 7 | * |
| 8 | * This Source Code may also be made available under the following Secondary |
| 9 | * Licenses when the conditions for such availability set forth in the |
| 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | * version 2 with the GNU Classpath Exception, which is available at |
| 12 | * https://www.gnu.org/software/classpath/license.html. |
| 13 | * |
| 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | */ |
| 16 | |
| 17 | package jakarta.json; |
| 18 | |
| 19 | /** |
| 20 | * <p>This interface represents an immutable implementation of a JSON Patch |
| 21 | * as defined by <a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>. |
| 22 | * </p> |
| 23 | * <p>A {@code JsonPatch} can be instantiated with {@link Json#createPatch(JsonArray)} |
| 24 | * by specifying the patch operations in a JSON Patch. Alternately, it |
| 25 | * can also be constructed with a {@link JsonPatchBuilder}. |
| 26 | * </p> |
| 27 | * The following illustrates both approaches. |
| 28 | * <p>1. Construct a JsonPatch with a JSON Patch. |
| 29 | * <pre>{@code |
| 30 | * JsonArray contacts = ... // The target to be patched |
| 31 | * JsonArray patch = ... ; // JSON Patch |
| 32 | * JsonPatch jsonpatch = Json.createPatch(patch); |
| 33 | * JsonArray result = jsonpatch.apply(contacts); |
| 34 | * } </pre> |
| 35 | * 2. Construct a JsonPatch with JsonPatchBuilder. |
| 36 | * <pre>{@code |
| 37 | * JsonPatchBuilder builder = Json.createPatchBuilder(); |
| 38 | * JsonArray result = builder.add("/John/phones/office", "1234-567") |
| 39 | * .remove("/Amy/age") |
| 40 | * .build() |
| 41 | * .apply(contacts); |
| 42 | * } </pre> |
| 43 | * |
| 44 | * @see <a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a> |
| 45 | * |
| 46 | * @since 1.1 |
| 47 | */ |
| 48 | public interface JsonPatch { |
| 49 | |
| 50 | /** |
| 51 | * This enum represents the list of valid JSON Patch operations |
| 52 | * as defined by <a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>. |
| 53 | * |
| 54 | * @see <a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a> |
| 55 | */ |
| 56 | enum Operation { |
| 57 | |
| 58 | /** |
| 59 | * "add" operation. |
| 60 | */ |
| 61 | ADD("add"), |
| 62 | |
| 63 | /** |
| 64 | * "remove" operation. |
| 65 | */ |
| 66 | REMOVE("remove"), |
| 67 | |
| 68 | /** |
| 69 | * "replace" operation. |
| 70 | */ |
| 71 | REPLACE("replace"), |
| 72 | |
| 73 | /** |
| 74 | * "move" operation. |
| 75 | */ |
| 76 | MOVE("move"), |
| 77 | |
| 78 | /** |
| 79 | * "copy" operation. |
| 80 | */ |
| 81 | COPY("copy"), |
| 82 | |
| 83 | /** |
| 84 | * "test" operation. |
| 85 | */ |
| 86 | TEST("test"); |
| 87 | |
| 88 | /** Operation name */ |
| 89 | private final String operationName; |
| 90 | |
| 91 | /** |
| 92 | * Create an enum constant with given {@code operationName}. |
| 93 | * @param operationName the operation name for the enum constant |
| 94 | */ |
| 95 | private Operation(String operationName) { |
| 96 | this.operationName = operationName; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns enum constant name as lower case string. |
| 101 | * |
| 102 | * @return lower case name of the enum constant |
| 103 | */ |
| 104 | public String operationName() { |
| 105 | return operationName; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns the enum constant with the specified name. |
| 110 | * |
| 111 | * @param operationName {@code operationName} to convert to the enum constant. |
| 112 | * @return the enum constant for given {@code operationName} |
| 113 | * @throws JsonException if given {@code operationName} is not recognized |
| 114 | */ |
| 115 | public static Operation fromOperationName(String operationName) { |
| 116 | for (Operation op : values()) { |
| 117 | if (op.operationName().equalsIgnoreCase(operationName)) { |
| 118 | return op; |
| 119 | } |
| 120 | } |
| 121 | throw new JsonException("Illegal value for the operationName of the JSON patch operation: " + operationName); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Applies the patch operations to the specified {@code target}. |
| 127 | * The target is not modified by the patch. |
| 128 | * |
| 129 | * @param <T> the target type, must be a subtype of {@link JsonStructure} |
| 130 | * @param target the target to apply the patch operations |
| 131 | * @return the transformed target after the patch |
| 132 | * @throws JsonException if the supplied JSON Patch is malformed or if |
| 133 | * it contains references to non-existing members |
| 134 | */ |
| 135 | <T extends JsonStructure> T apply(T target); |
| 136 | |
| 137 | /** |
| 138 | * Returns the {@code JsonPatch} as {@code JsonArray}. |
| 139 | * |
| 140 | * @return this {@code JsonPatch} as {@code JsonArray} |
| 141 | */ |
| 142 | JsonArray toJsonArray(); |
| 143 | |
| 144 | } |