| 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 Pointer |
| 21 | * as defined by <a href="http://tools.ietf.org/html/rfc6901">RFC 6901</a>. |
| 22 | * </p> |
| 23 | * <p> A JSON Pointer, when applied to a target {@link JsonValue}, |
| 24 | * defines a reference location in the target.</p> |
| 25 | * <p> An empty JSON Pointer string defines a reference to the target itself.</p> |
| 26 | * <p> If the JSON Pointer string is non-empty, it must be a sequence |
| 27 | * of '/' prefixed tokens, and the target must either be a {@link JsonArray} |
| 28 | * or {@link JsonObject}. If the target is a {@code JsonArray}, the pointer |
| 29 | * defines a reference to an array element, and the last token specifies the index. |
| 30 | * If the target is a {@link JsonObject}, the pointer defines a reference to a |
| 31 | * name/value pair, and the last token specifies the name. |
| 32 | * </p> |
| 33 | * <p> The method {@link #getValue getValue()} returns the referenced value. |
| 34 | * Methods {@link #add add()}, {@link #replace replace()}, |
| 35 | * and {@link #remove remove()} execute operations specified in |
| 36 | * <a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>. </p> |
| 37 | * |
| 38 | * @see <a href="http://tools.ietf.org/html/rfc6901">RFC 6901</a> |
| 39 | * @see <a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a> |
| 40 | * |
| 41 | * @since 1.1 |
| 42 | */ |
| 43 | public interface JsonPointer { |
| 44 | |
| 45 | /** |
| 46 | * Adds or replaces a value at the referenced location in the specified |
| 47 | * {@code target} with the specified {@code value}. |
| 48 | * <ol> |
| 49 | * <li>If the reference is the target (empty JSON Pointer string), |
| 50 | * the specified {@code value}, which must be the same type as |
| 51 | * specified {@code target}, is returned.</li> |
| 52 | * <li>If the reference is an array element, the specified {@code value} is inserted |
| 53 | * into the array, at the referenced index. The value currently at that location, and |
| 54 | * any subsequent values, are shifted to the right (adds one to the indices). |
| 55 | * Index starts with 0. If the reference is specified with a "-", or if the |
| 56 | * index is equal to the size of the array, the value is appended to the array.</li> |
| 57 | * <li>If the reference is a name/value pair of a {@code JsonObject}, and the |
| 58 | * referenced value exists, the value is replaced by the specified {@code value}. |
| 59 | * If the value does not exist, a new name/value pair is added to the object.</li> |
| 60 | * </ol> |
| 61 | * |
| 62 | * @param <T> the target type, must be a subtype of {@link JsonValue} |
| 63 | * @param target the target referenced by this {@code JsonPointer} |
| 64 | * @param value the value to be added |
| 65 | * @return the transformed {@code target} after the value is added. |
| 66 | * @throws NullPointerException if {@code target} is {@code null} |
| 67 | * @throws JsonException if the reference is an array element and |
| 68 | * the index is out of range ({@code index < 0 || index > array size}), |
| 69 | * or if the pointer contains references to non-existing objects or arrays. |
| 70 | */ |
| 71 | <T extends JsonStructure> T add(T target, JsonValue value); |
| 72 | |
| 73 | /** |
| 74 | * Removes the value at the reference location in the specified {@code target}. |
| 75 | * |
| 76 | * @param <T> the target type, must be a subtype of {@link JsonValue} |
| 77 | * @param target the target referenced by this {@code JsonPointer} |
| 78 | * @return the transformed {@code target} after the value is removed. |
| 79 | * @throws NullPointerException if {@code target} is {@code null} |
| 80 | * @throws JsonException if the referenced value does not exist, |
| 81 | * or if the reference is the target. |
| 82 | */ |
| 83 | <T extends JsonStructure> T remove(T target); |
| 84 | |
| 85 | /** |
| 86 | * Replaces the value at the referenced location in the specified |
| 87 | * {@code target} with the specified {@code value}. |
| 88 | * |
| 89 | * @param <T> the target type, must be a subtype of {@link JsonValue} |
| 90 | * @param target the target referenced by this {@code JsonPointer} |
| 91 | * @param value the value to be stored at the referenced location |
| 92 | * @return the transformed {@code target} after the value is replaced. |
| 93 | * @throws NullPointerException if {@code target} is {@code null} |
| 94 | * @throws JsonException if the referenced value does not exist, |
| 95 | * or if the reference is the target. |
| 96 | */ |
| 97 | <T extends JsonStructure> T replace(T target, JsonValue value); |
| 98 | |
| 99 | /** |
| 100 | * Returns {@code true} if there is a value at the referenced location in the specified {@code target}. |
| 101 | * |
| 102 | * @param target the target referenced by this {@code JsonPointer} |
| 103 | * @return {@code true} if this pointer points to a value in a specified {@code target}. |
| 104 | */ |
| 105 | boolean containsValue(JsonStructure target); |
| 106 | |
| 107 | /** |
| 108 | * Returns the value at the referenced location in the specified {@code target}. |
| 109 | * |
| 110 | * @param target the target referenced by this {@code JsonPointer} |
| 111 | * @return the referenced value in the target. |
| 112 | * @throws NullPointerException if {@code target} is null |
| 113 | * @throws JsonException if the referenced value does not exist |
| 114 | */ |
| 115 | JsonValue getValue(JsonStructure target); |
| 116 | |
| 117 | /** |
| 118 | * Returns the string representation of this JSON Pointer. |
| 119 | * The value to be returned is an empty string or a sequence of '{@code /}' prefixed tokens. |
| 120 | * |
| 121 | * @return the valid escaped JSON Pointer string. |
| 122 | */ |
| 123 | @Override |
| 124 | String toString(); |
| 125 | } |