| 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 implementation of a JSON Merge Patch |
| 21 | * as defined by <a href="http://tools.ietf.org/html/rfc7396">RFC 7396</a>. |
| 22 | * </p> |
| 23 | * <p>A {@code JsonMergePatch} can be instantiated with {@link Json#createMergePatch(JsonValue)} |
| 24 | * by specifying the patch operations in a JSON Merge Patch or using {@link Json#createMergeDiff(JsonValue, JsonValue)} |
| 25 | * to create a JSON Merge Patch based on the difference between two {@code JsonValue}s. |
| 26 | * </p> |
| 27 | * The following illustrates both approaches. |
| 28 | * <p>1. Construct a JsonMergePatch with an existing JSON Merge Patch. |
| 29 | * <pre>{@code |
| 30 | * JsonValue contacts = ... ; // The target to be patched |
| 31 | * JsonValue patch = ... ; // JSON Merge Patch |
| 32 | * JsonMergePatch mergePatch = Json.createMergePatch(patch); |
| 33 | * JsonValue result = mergePatch.apply(contacts); |
| 34 | * } </pre> |
| 35 | * 2. Construct a JsonMergePatch from a difference between two {@code JsonValue}s. |
| 36 | * <pre>{@code |
| 37 | * JsonValue source = ... ; // The source object |
| 38 | * JsonValue target = ... ; // The modified object |
| 39 | * JsonMergePatch mergePatch = Json.createMergeDiff(source, target); // The diff between source and target in a Json Merge Patch format |
| 40 | * } </pre> |
| 41 | * |
| 42 | * @see <a href="http://tools.ietf.org/html/rfc7396">RFC 7396</a> |
| 43 | * |
| 44 | * @since 1.1 |
| 45 | */ |
| 46 | public interface JsonMergePatch { |
| 47 | |
| 48 | /** |
| 49 | * Applies the JSON Merge Patch to the specified {@code target}. |
| 50 | * The target is not modified by the patch. |
| 51 | * |
| 52 | * @param target the target to apply the merge patch |
| 53 | * @return the transformed target after the patch |
| 54 | */ |
| 55 | JsonValue apply(JsonValue target); |
| 56 | |
| 57 | /** |
| 58 | * Returns the {@code JsonMergePatch} as {@code JsonValue}. |
| 59 | * |
| 60 | * @return this {@code JsonMergePatch} as {@code JsonValue} |
| 61 | */ |
| 62 | JsonValue toJsonValue(); |
| 63 | } |