| 1 | /* |
| 2 | * Copyright (c) 2015, 2023 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 org.eclipse.parsson; |
| 18 | |
| 19 | import jakarta.json.JsonArray; |
| 20 | import jakarta.json.JsonException; |
| 21 | import jakarta.json.JsonObject; |
| 22 | import jakarta.json.JsonPointer; |
| 23 | import jakarta.json.JsonStructure; |
| 24 | import jakarta.json.JsonValue; |
| 25 | import java.io.Serializable; |
| 26 | import java.util.function.BiFunction; |
| 27 | |
| 28 | /** |
| 29 | * <p>This class is an immutable representation of a JSON Pointer as specified in |
| 30 | * <a href="http://tools.ietf.org/html/rfc6901">RFC 6901</a>. |
| 31 | * </p> |
| 32 | * <p> A JSON Pointer, when applied to a target {@link JsonValue}, |
| 33 | * defines a reference location in the target.</p> |
| 34 | * <p> An empty JSON Pointer string defines a reference to the target itself.</p> |
| 35 | * <p> If the JSON Pointer string is non-empty, it must be a sequence |
| 36 | * of '/' prefixed tokens, and the target must either be a {@link JsonArray} |
| 37 | * or {@link JsonObject}. If the target is a {@code JsonArray}, the pointer |
| 38 | * defines a reference to an array element, and the last token specifies the index. |
| 39 | * If the target is a {@link JsonObject}, the pointer defines a reference to a |
| 40 | * name/value pair, and the last token specifies the name. |
| 41 | * </p> |
| 42 | * <p> The method {@link #getValue getValue()} returns the referenced value. |
| 43 | * The methods {@link #add add()}, {@link #replace replace()}, |
| 44 | * and {@link #remove remove()} executes the operations specified in |
| 45 | * <a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>. </p> |
| 46 | * |
| 47 | * @since 1.1 |
| 48 | */ |
| 49 | |
| 50 | public final class JsonPointerImpl implements JsonPointer, Serializable { |
| 51 | |
| 52 | private static final long serialVersionUID = -8123110179640843141L; |
| 53 | private final String[] tokens; |
| 54 | private final String jsonPointer; |
| 55 | private final JsonContext jsonContext; |
| 56 | |
| 57 | /** |
| 58 | * Constructs and initializes a JsonPointerImpl. |
| 59 | * @param jsonPointer the JSON Pointer string |
| 60 | * @throws NullPointerException if {@code jsonPointer} is {@code null} |
| 61 | * @throws JsonException if {@code jsonPointer} is not a valid JSON Pointer |
| 62 | */ |
| 63 | public JsonPointerImpl(String jsonPointer, JsonContext jsonContext) { |
| 64 | this.jsonPointer = jsonPointer; |
| 65 | this.jsonContext = jsonContext; |
| 66 | tokens = jsonPointer.split("/", -1); // keep the trailing blanks |
| 67 | if (! "".equals(tokens[0])) { |
| 68 | throw new JsonException(JsonMessages.POINTER_FORMAT_INVALID()); |
| 69 | } |
| 70 | for (int i = 1; i < tokens.length; i++) { |
| 71 | String token = tokens[i]; |
| 72 | StringBuilder reftoken = new StringBuilder(); |
| 73 | for (int j = 0; j < token.length(); j++) { |
| 74 | char ch = token.charAt(j); |
| 75 | if (ch == '~' && j < token.length() - 1) { |
| 76 | char ch1 = token.charAt(j+1); |
| 77 | if (ch1 == '0') { |
| 78 | ch = '~'; j++; |
| 79 | } else if (ch1 == '1') { |
| 80 | ch = '/'; j++; |
| 81 | } |
| 82 | } |
| 83 | reftoken.append(ch); |
| 84 | } |
| 85 | tokens[i] = reftoken.toString(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Compares this {@code JsonPointer} with another object. |
| 91 | * @param obj the object to compare this {@code JsonPointer} against |
| 92 | * @return true if the given object is a {@code JsonPointer} with the same |
| 93 | * reference tokens as this one, false otherwise. |
| 94 | */ |
| 95 | @Override |
| 96 | public boolean equals(Object obj) { |
| 97 | if (this == obj) |
| 98 | return true; |
| 99 | if (obj == null || obj.getClass() != JsonPointerImpl.class) |
| 100 | return false; |
| 101 | return jsonPointer.equals(((JsonPointerImpl)obj).jsonPointer); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns the hash code value for this {@code JsonPointer} object. |
| 106 | * The hash code of this object is defined by the hash codes of it's reference tokens. |
| 107 | * |
| 108 | * @return the hash code value for this {@code JsonPointer} object |
| 109 | */ |
| 110 | @Override |
| 111 | public int hashCode() { |
| 112 | return jsonPointer.hashCode(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns {@code true} if there is a value at the referenced location in the specified {@code target}. |
| 117 | * |
| 118 | * @param target the target referenced by this {@code JsonPointer} |
| 119 | * @return {@code true} if this pointer points to a value in a specified {@code target}. |
| 120 | */ |
| 121 | @Override |
| 122 | public boolean containsValue(JsonStructure target) { |
| 123 | NodeReference[] refs = getReferences(target); |
| 124 | return refs[0].contains(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns the value at the referenced location in the specified {@code target} |
| 129 | * |
| 130 | * @param target the target referenced by this {@code JsonPointer} |
| 131 | * @return the referenced value in the target. |
| 132 | * @throws NullPointerException if {@code target} is null |
| 133 | * @throws JsonException if the referenced value does not exist |
| 134 | */ |
| 135 | @Override |
| 136 | public JsonValue getValue(JsonStructure target) { |
| 137 | NodeReference[] refs = getReferences(target); |
| 138 | return refs[0].get(); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Adds or replaces a value at the referenced location in the specified |
| 143 | * {@code target} with the specified {@code value}. |
| 144 | * <ol> |
| 145 | * <li>If the reference is the target (empty JSON Pointer string), |
| 146 | * the specified {@code value}, which must be the same type as |
| 147 | * specified {@code target}, is returned.</li> |
| 148 | * <li>If the reference is an array element, the specified {@code value} is inserted |
| 149 | * into the array, at the referenced index. The value currently at that location, and |
| 150 | * any subsequent values, are shifted to the right (adds one to the indices). |
| 151 | * Index starts with 0. If the reference is specified with a "-", or if the |
| 152 | * index is equal to the size of the array, the value is appended to the array.</li> |
| 153 | * <li>If the reference is a name/value pair of a {@code JsonObject}, and the |
| 154 | * referenced value exists, the value is replaced by the specified {@code value}. |
| 155 | * If the value does not exist, a new name/value pair is added to the object.</li> |
| 156 | * </ol> |
| 157 | * |
| 158 | * @param target the target referenced by this {@code JsonPointer} |
| 159 | * @param value the value to be added |
| 160 | * @return the transformed {@code target} after the value is added. |
| 161 | * @throws NullPointerException if {@code target} is {@code null} |
| 162 | * @throws JsonException if the reference is an array element and |
| 163 | * the index is out of range ({@code index < 0 || index > array size}), |
| 164 | * or if the pointer contains references to non-existing objects or arrays. |
| 165 | */ |
| 166 | @Override |
| 167 | public JsonStructure add(JsonStructure target, JsonValue value) { |
| 168 | return execute(NodeReference::add, target, value); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Replaces the value at the referenced location in the specified |
| 173 | * {@code target} with the specified {@code value}. |
| 174 | * |
| 175 | * @param target the target referenced by this {@code JsonPointer} |
| 176 | * @param value the value to be stored at the referenced location |
| 177 | * @return the transformed {@code target} after the value is replaced. |
| 178 | * @throws NullPointerException if {@code target} is {@code null} |
| 179 | * @throws JsonException if the referenced value does not exist, |
| 180 | * or if the reference is the target. |
| 181 | */ |
| 182 | @Override |
| 183 | public JsonStructure replace(JsonStructure target, JsonValue value) { |
| 184 | return execute(NodeReference::replace, target, value); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Removes the value at the reference location in the specified {@code target} |
| 189 | * |
| 190 | * @param target the target referenced by this {@code JsonPointer} |
| 191 | * @return the transformed {@code target} after the value is removed. |
| 192 | * @throws NullPointerException if {@code target} is {@code null} |
| 193 | * @throws JsonException if the referenced value does not exist, |
| 194 | * or if the reference is the target. |
| 195 | */ |
| 196 | @Override |
| 197 | public JsonStructure remove(JsonStructure target) { |
| 198 | return execute((r,v)->r.remove(), target, null); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * {@inheritDoc} |
| 203 | */ |
| 204 | @Override |
| 205 | public String toString() { |
| 206 | return jsonPointer; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Executes the operation |
| 211 | * @param op a {code BiFunction} used to specify the operation to execute on |
| 212 | * the leaf node of the Json Pointer |
| 213 | * @param target the target JsonStructure for this JsonPointer |
| 214 | * @param value the JsonValue for add and replace, can be null for getvalue and remove |
| 215 | */ |
| 216 | private JsonStructure execute(BiFunction<NodeReference, JsonValue, JsonStructure> op, |
| 217 | JsonStructure target, JsonValue value) { |
| 218 | |
| 219 | NodeReference[] refs = getReferences(target); |
| 220 | JsonStructure result = op.apply(refs[0], value); |
| 221 | for (int i = 1; i < refs.length; i++) { |
| 222 | result = refs[i].replace(result); |
| 223 | } |
| 224 | return result; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Computes the {@code NodeReference}s for each node on the path of |
| 229 | * the JSON Pointer, in reverse order, starting from the leaf node |
| 230 | */ |
| 231 | private NodeReference[] getReferences(JsonStructure target) { |
| 232 | NodeReference[] references; |
| 233 | // First check if this is a reference to a JSON value tree |
| 234 | if (tokens.length == 1) { |
| 235 | references = new NodeReference[1]; |
| 236 | references[0] = NodeReference.of(target); |
| 237 | return references; |
| 238 | } |
| 239 | |
| 240 | references = new NodeReference[tokens.length-1]; |
| 241 | JsonValue value = target; |
| 242 | int s = tokens.length; |
| 243 | for (int i = 1; i < s; i++) { |
| 244 | // Start with index 1, skipping the "" token |
| 245 | switch (value.getValueType()) { |
| 246 | case OBJECT: |
| 247 | JsonObject object = (JsonObject) value; |
| 248 | references[s-i-1] = NodeReference.of(object, tokens[i], jsonContext); |
| 249 | if (i < s-1) { |
| 250 | value = object.get(tokens[i]); |
| 251 | if (value == null) { |
| 252 | // Except for the last name, the mapping must exist |
| 253 | throw new JsonException(JsonMessages.POINTER_MAPPING_MISSING(object, tokens[i])); |
| 254 | } |
| 255 | } |
| 256 | break; |
| 257 | case ARRAY: |
| 258 | int index = getIndex(tokens[i]); |
| 259 | JsonArray array = (JsonArray) value; |
| 260 | references[s-i-1] = NodeReference.of(array, index, jsonContext); |
| 261 | if (i < s-1 && index != -1) { |
| 262 | if (index >= array.size()) { |
| 263 | throw new JsonException(JsonMessages.NODEREF_ARRAY_INDEX_ERR(index, array.size())); |
| 264 | } |
| 265 | // The last array index in the path can have index value of -1 |
| 266 | // ("-" in the JSON pointer) |
| 267 | value = array.get(index); |
| 268 | } |
| 269 | break; |
| 270 | default: |
| 271 | throw new JsonException(JsonMessages.POINTER_REFERENCE_INVALID(value.getValueType())); |
| 272 | } |
| 273 | } |
| 274 | return references; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Computes the array index |
| 279 | * @param token the input string token |
| 280 | * @return the array index. -1 if the token is "-" |
| 281 | * @throws JsonException if the string token is not in correct format |
| 282 | */ |
| 283 | static private int getIndex(String token) { |
| 284 | if (token == null || token.length() == 0) { |
| 285 | throw new JsonException(JsonMessages.POINTER_ARRAY_INDEX_ERR(token)); |
| 286 | } |
| 287 | if (token.equals("-")) { |
| 288 | return -1; |
| 289 | } |
| 290 | if (token.equals("0")) { |
| 291 | return 0; |
| 292 | } |
| 293 | if (token.charAt(0) == '+' || token.charAt(0) == '-') { |
| 294 | throw new JsonException(JsonMessages.POINTER_ARRAY_INDEX_ERR(token)); |
| 295 | } |
| 296 | try { |
| 297 | return Integer.parseInt(token); |
| 298 | } catch (NumberFormatException ex) { |
| 299 | throw new JsonException(JsonMessages.POINTER_ARRAY_INDEX_ILLEGAL(token), ex); |
| 300 | } |
| 301 | } |
| 302 | } |