| 1 | /* |
| 2 | * Copyright (c) 2011, 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 | import java.util.List; |
| 20 | import java.util.function.Function; |
| 21 | import java.util.stream.Collectors; |
| 22 | import java.util.stream.Stream; |
| 23 | |
| 24 | /** |
| 25 | * {@code JsonArray} represents an immutable JSON array |
| 26 | * (an ordered sequence of zero or more values). |
| 27 | * It also provides an unmodifiable list view of the values in the array. |
| 28 | * |
| 29 | * <p>A {@code JsonArray} object can be created by reading JSON data from |
| 30 | * an input source or it can be built from scratch using an array builder |
| 31 | * object. |
| 32 | * |
| 33 | * <p>The following example demonstrates how to create a {@code JsonArray} |
| 34 | * object from an input source using the method {@link JsonReader#readArray()}: |
| 35 | * <pre><code> |
| 36 | * JsonReader jsonReader = Json.createReader(...); |
| 37 | * JsonArray array = jsonReader.readArray(); |
| 38 | * jsonReader.close(); |
| 39 | * </code></pre> |
| 40 | * |
| 41 | * <p>The following example demonstrates how to build an empty JSON array |
| 42 | * using the class {@link JsonArrayBuilder}: |
| 43 | * <pre><code> |
| 44 | * JsonArray array = Json.createArrayBuilder().build(); |
| 45 | * </code></pre> |
| 46 | * |
| 47 | * <p>The example code below demonstrates how to create the following JSON array: |
| 48 | * <pre><code> |
| 49 | * [ |
| 50 | * { "type": "home", "number": "212 555-1234" }, |
| 51 | * { "type": "fax", "number": "646 555-4567" } |
| 52 | * ] |
| 53 | * </code></pre> |
| 54 | * <pre><code> |
| 55 | * JsonArray value = Json.createArrayBuilder() |
| 56 | * .add(Json.createObjectBuilder() |
| 57 | * .add("type", "home") |
| 58 | * .add("number", "212 555-1234")) |
| 59 | * .add(Json.createObjectBuilder() |
| 60 | * .add("type", "fax") |
| 61 | * .add("number", "646 555-4567")) |
| 62 | * .build(); |
| 63 | * </code></pre> |
| 64 | * |
| 65 | * <p>The following example demonstrates how to write a {@code JsonArray} object |
| 66 | * as JSON data: |
| 67 | * <pre><code> |
| 68 | * JsonArray arr = ...; |
| 69 | * JsonWriter writer = Json.createWriter(...) |
| 70 | * writer.writeArray(arr); |
| 71 | * writer.close(); |
| 72 | * </code></pre> |
| 73 | * |
| 74 | * <p>The values in a {@code JsonArray} can be of the following types: |
| 75 | * {@link JsonObject}, {@link JsonArray}, |
| 76 | * {@link JsonString}, {@link JsonNumber}, {@link JsonValue#TRUE}, |
| 77 | * {@link JsonValue#FALSE}, and {@link JsonValue#NULL}. |
| 78 | * {@code JsonArray} provides various accessor methods to access the values |
| 79 | * in an array. |
| 80 | * |
| 81 | * <p>The following example shows how to obtain the home phone number |
| 82 | * "212 555-1234" from the array built in the previous example: |
| 83 | * <pre><code> |
| 84 | * JsonObject home = array.getJsonObject(0); |
| 85 | * String number = home.getString("number"); |
| 86 | * </code></pre> |
| 87 | * |
| 88 | * <p>{@code JsonArray} instances are list objects that provide read-only |
| 89 | * access to the values in the JSON array. Any attempt to modify the list, |
| 90 | * whether directly or using its collection views, results in an |
| 91 | * {@code UnsupportedOperationException}. |
| 92 | */ |
| 93 | public interface JsonArray extends JsonStructure, List<JsonValue> { |
| 94 | |
| 95 | /** |
| 96 | * Returns the object value at the specified position in this array. |
| 97 | * This is a convenience method for {@code (JsonObject)get(index)}. |
| 98 | * |
| 99 | * @param index index of the value to be returned |
| 100 | * @return the value at the specified position in this array |
| 101 | * @throws IndexOutOfBoundsException if the index is out of range |
| 102 | * @throws ClassCastException if the value at the specified position is not |
| 103 | * assignable to the JsonObject type |
| 104 | */ |
| 105 | JsonObject getJsonObject(int index); |
| 106 | |
| 107 | /** |
| 108 | * Returns the array value at the specified position in this array. |
| 109 | * This is a convenience method for {@code (JsonArray)get(index)}. |
| 110 | * |
| 111 | * @param index index of the value to be returned |
| 112 | * @return the value at the specified position in this array |
| 113 | * @throws IndexOutOfBoundsException if the index is out of range |
| 114 | * @throws ClassCastException if the value at the specified position is not |
| 115 | * assignable to the JsonArray type |
| 116 | */ |
| 117 | JsonArray getJsonArray(int index); |
| 118 | |
| 119 | /** |
| 120 | * Returns the number value at the specified position in this array. |
| 121 | * This is a convenience method for {@code (JsonNumber)get(index)}. |
| 122 | * |
| 123 | * @param index index of the value to be returned |
| 124 | * @return the value at the specified position in this array |
| 125 | * @throws IndexOutOfBoundsException if the index is out of range |
| 126 | * @throws ClassCastException if the value at the specified position is not |
| 127 | * assignable to the JsonNumber type |
| 128 | */ |
| 129 | JsonNumber getJsonNumber(int index); |
| 130 | |
| 131 | /** |
| 132 | * Returns the string value at ths specified position in this array. |
| 133 | * This is a convenience method for {@code (JsonString)get(index)}. |
| 134 | * |
| 135 | * @param index index of the value to be returned |
| 136 | * @return the value at the specified position in this array |
| 137 | * @throws IndexOutOfBoundsException if the index is out of range |
| 138 | * @throws ClassCastException if the value at the specified position is not |
| 139 | * assignable to the JsonString type |
| 140 | */ |
| 141 | JsonString getJsonString(int index); |
| 142 | |
| 143 | /** |
| 144 | * Returns a list view of the specified type for the array. This method |
| 145 | * does not verify if there is a value of wrong type in the array. Providing |
| 146 | * this typesafe view dynamically may cause a program fail with a |
| 147 | * {@code ClassCastException}, if there is a value of wrong type in this |
| 148 | * array. Unfortunately, the exception can occur at any time after this |
| 149 | * method returns. |
| 150 | * |
| 151 | * @param <T> The type of the List for the array |
| 152 | * @param clazz a JsonValue type |
| 153 | * @return a list view of the specified type |
| 154 | */ |
| 155 | <T extends JsonValue> List<T> getValuesAs(Class<T> clazz); |
| 156 | |
| 157 | /** |
| 158 | * Returns a list view for the array. The value and the type of the elements |
| 159 | * in the list is specified by the {@code func} argument. |
| 160 | * <p>This method can be used to obtain a list of the unwrapped types, such as |
| 161 | * <pre>{@code |
| 162 | * List<String> strings = ary1.getValuesAs(JsonString::getString); |
| 163 | * List<Integer> ints = ary2.getValuesAs(JsonNumber::intValue); |
| 164 | * } </pre> |
| 165 | * or a list of simple projections, such as |
| 166 | * <pre> {@code |
| 167 | * List<Integer> stringsizes = ary1.getValueAs((JsonString v)->v.getString().length(); |
| 168 | * } </pre> |
| 169 | * @param <K> The element type (must be a subtype of JsonValue) of this JsonArray. |
| 170 | * @param <T> The element type of the returned List |
| 171 | * @param func The function that maps the elements of this JsonArray to the target elements. |
| 172 | * @return A List of the specified values and type. |
| 173 | * @throws ClassCastException if the {@code JsonArray} contains a value of wrong type |
| 174 | * |
| 175 | * @since 1.1 |
| 176 | */ |
| 177 | default <T, K extends JsonValue> List<T> getValuesAs(Function<K, T> func) { |
| 178 | @SuppressWarnings("unchecked") |
| 179 | Stream<K> stream = (Stream<K>) stream(); |
| 180 | return stream.map(func).collect(Collectors.toList()); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * A convenience method for |
| 185 | * {@code getJsonString(index).getString()}. |
| 186 | * |
| 187 | * @param index index of the {@code JsonString} value |
| 188 | * @return the String value at the specified position |
| 189 | * @throws IndexOutOfBoundsException if the index is out of range |
| 190 | * @throws ClassCastException if the value at the specified position is not |
| 191 | * assignable to {@code JsonString} |
| 192 | */ |
| 193 | String getString(int index); |
| 194 | |
| 195 | /** |
| 196 | * Returns the {@code String} value of {@code JsonString} at the specified |
| 197 | * position in this JSON array values. If {@code JsonString} is found, |
| 198 | * its {@link jakarta.json.JsonString#getString()} is returned. Otherwise, |
| 199 | * the specified default value is returned. |
| 200 | * |
| 201 | * @param index index of the {@code JsonString} value |
| 202 | * @param defaultValue the String to return if the {@code JsonValue} at the |
| 203 | * specified position is not a {@code JsonString} |
| 204 | * @return the String value at the specified position in this array, |
| 205 | * or the specified default value |
| 206 | */ |
| 207 | String getString(int index, String defaultValue); |
| 208 | |
| 209 | /** |
| 210 | * A convenience method for |
| 211 | * {@code getJsonNumber(index).intValue()}. |
| 212 | * |
| 213 | * @param index index of the {@code JsonNumber} value |
| 214 | * @return the int value at the specified position |
| 215 | * @throws IndexOutOfBoundsException if the index is out of range |
| 216 | * @throws ClassCastException if the value at the specified position is not |
| 217 | * assignable to {@code JsonNumber} |
| 218 | */ |
| 219 | int getInt(int index); |
| 220 | |
| 221 | /** |
| 222 | * Returns the int value of the {@code JsonNumber} at the specified position. |
| 223 | * If the value at that position is a {@code JsonNumber}, |
| 224 | * this method returns {@link jakarta.json.JsonNumber#intValue()}. Otherwise |
| 225 | * this method returns the specified default value. |
| 226 | * |
| 227 | * @param index index of the {@code JsonNumber} value |
| 228 | * @param defaultValue the int value to return if the {@code JsonValue} at |
| 229 | * the specified position is not a {@code JsonNumber} |
| 230 | * @return the int value at the specified position in this array, |
| 231 | * or the specified default value |
| 232 | */ |
| 233 | int getInt(int index, int defaultValue); |
| 234 | |
| 235 | /** |
| 236 | * Returns the boolean value at the specified position. |
| 237 | * If the value at the specified position is {@code JsonValue.TRUE} |
| 238 | * this method returns {@code true}. If the value at the specified position |
| 239 | * is {@code JsonValue.FALSE} this method returns {@code false}. |
| 240 | * |
| 241 | * @param index index of the JSON boolean value |
| 242 | * @return the boolean value at the specified position |
| 243 | * @throws IndexOutOfBoundsException if the index is out of range |
| 244 | * @throws ClassCastException if the value at the specified position is not |
| 245 | * {@code JsonValue.TRUE} or {@code JsonValue.FALSE} |
| 246 | */ |
| 247 | boolean getBoolean(int index); |
| 248 | |
| 249 | /** |
| 250 | * Returns the boolean value at the specified position. |
| 251 | * If the value at the specified position is {@code JsonValue.TRUE} |
| 252 | * this method returns {@code true}. If the value at the specified position |
| 253 | * is {@code JsonValue.FALSE} this method returns {@code false}. |
| 254 | * Otherwise this method returns the specified default value. |
| 255 | * |
| 256 | * @param index index of the JSON boolean value |
| 257 | * @param defaultValue the boolean value to return if the {@code JsonValue} |
| 258 | * at the specified position is neither TRUE nor FALSE |
| 259 | * @return the boolean value at the specified position, |
| 260 | * or the specified default value |
| 261 | */ |
| 262 | boolean getBoolean(int index, boolean defaultValue); |
| 263 | |
| 264 | /** |
| 265 | * Returns {@code true} if the value at the specified location in this |
| 266 | * array is {@code JsonValue.NULL}. |
| 267 | * |
| 268 | * @param index index of the JSON null value |
| 269 | * @return return true if the value at the specified location is |
| 270 | * {@code JsonValue.NULL}, otherwise false |
| 271 | * @throws IndexOutOfBoundsException if the index is out of range |
| 272 | */ |
| 273 | boolean isNull(int index); |
| 274 | } |