| 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.stream; |
| 18 | |
| 19 | |
| 20 | import java.io.Closeable; |
| 21 | import java.math.BigDecimal; |
| 22 | import java.util.stream.Stream; |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import jakarta.json.JsonValue; |
| 26 | import jakarta.json.JsonObject; |
| 27 | import jakarta.json.JsonArray; |
| 28 | |
| 29 | /** |
| 30 | * Provides forward, read-only access to JSON data in a streaming way. This |
| 31 | * is the most efficient way for reading JSON data. |
| 32 | * This is the only way to parse and process JSON data that are too big to be loaded in memory. |
| 33 | * <p>The class |
| 34 | * {@link jakarta.json.Json} contains methods to create parsers from input |
| 35 | * sources ({@link java.io.InputStream} and {@link java.io.Reader}). |
| 36 | * |
| 37 | * <p> |
| 38 | * The following example demonstrates how to create a parser from a string |
| 39 | * that contains an empty JSON array: |
| 40 | * <pre> |
| 41 | * <code> |
| 42 | * JsonParser parser = Json.createParser(new StringReader("[]")); |
| 43 | * </code> |
| 44 | * </pre> |
| 45 | * |
| 46 | * <p> |
| 47 | * The class {@link JsonParserFactory} also contains methods to create |
| 48 | * {@code JsonParser} instances. {@link JsonParserFactory} is preferred |
| 49 | * when creating multiple parser instances. A sample usage is shown |
| 50 | * in the following example: |
| 51 | * <pre> |
| 52 | * <code> |
| 53 | * JsonParserFactory factory = Json.createParserFactory(); |
| 54 | * JsonParser parser1 = factory.createParser(...); |
| 55 | * JsonParser parser2 = factory.createParser(...); |
| 56 | * </code> |
| 57 | * </pre> |
| 58 | * |
| 59 | * <p> |
| 60 | * {@code JsonParser} parses JSON using the pull parsing programming model. |
| 61 | * In this model the client code controls the thread and calls the method |
| 62 | * {@code next()} to advance the parser to the next state after |
| 63 | * processing each element. The parser can generate the following events: |
| 64 | * {@code START_OBJECT}, {@code END_OBJECT}, {@code START_ARRAY}, |
| 65 | * {@code END_ARRAY}, {@code KEY_NAME}, {@code VALUE_STRING}, |
| 66 | * {@code VALUE_NUMBER}, {@code VALUE_TRUE}, {@code VALUE_FALSE}, |
| 67 | * and {@code VALUE_NULL}. |
| 68 | * |
| 69 | * <p> |
| 70 | * <b>For example</b>, for an empty JSON object ({ }), the parser generates the event |
| 71 | * {@code START_OBJECT} with the first call to the method {@code next()} and the |
| 72 | * event {@code END_OBJECT} with the second call to the method {@code next()}. |
| 73 | * The following code demonstrates how to access these events: |
| 74 | * |
| 75 | * <pre> |
| 76 | * <code> |
| 77 | * Event event = parser.next(); // START_OBJECT |
| 78 | * event = parser.next(); // END_OBJECT |
| 79 | * </code> |
| 80 | * </pre> |
| 81 | * |
| 82 | * <p> |
| 83 | * <b>For example</b>, for the following JSON: |
| 84 | * <pre> |
| 85 | * { |
| 86 | * "firstName": "John", "lastName": "Smith", "age": 25, |
| 87 | * "phoneNumber": [ |
| 88 | * { "type": "home", "number": "212 555-1234" }, |
| 89 | * { "type": "fax", "number": "646 555-4567" } |
| 90 | * ] |
| 91 | * } |
| 92 | * </pre> |
| 93 | * |
| 94 | * <p>calls to the method {@code next()} result in parse events at the specified |
| 95 | * locations below (marked in bold): |
| 96 | * |
| 97 | * <pre> |
| 98 | * {<B>START_OBJECT</B> |
| 99 | * "firstName"<B>KEY_NAME</B>: "John"<B>VALUE_STRING</B>, "lastName"<B>KEY_NAME</B>: "Smith"<B>VALUE_STRING</B>, "age"<B>KEY_NAME</B>: 25<B>VALUE_NUMBER</B>, |
| 100 | * "phoneNumber"<B>KEY_NAME</B> : [<B>START_ARRAY</B> |
| 101 | * {<B>START_OBJECT</B> "type"<B>KEY_NAME</B>: "home"<B>VALUE_STRING</B>, "number"<B>KEY_NAME</B>: "212 555-1234"<B>VALUE_STRING</B> }<B>END_OBJECT</B>, |
| 102 | * {<B>START_OBJECT</B> "type"<B>KEY_NAME</B>: "fax"<B>VALUE_STRING</B>, "number"<B>KEY_NAME</B>: "646 555-4567"<B>VALUE_STRING</B> }<B>END_OBJECT</B> |
| 103 | * ]<B>END_ARRAY</B> |
| 104 | * }<B>END_OBJECT</B> |
| 105 | * </pre> |
| 106 | * |
| 107 | * The methods {@link #next()} and {@link #hasNext()} enable iteration over |
| 108 | * parser events to process JSON data. {@code JsonParser} provides get methods |
| 109 | * to obtain the value at the current state of the parser. For example, the |
| 110 | * following code shows how to obtain the value "John" from the JSON above: |
| 111 | * |
| 112 | * <pre> |
| 113 | * <code> |
| 114 | * Event event = parser.next(); // START_OBJECT |
| 115 | * event = parser.next(); // KEY_NAME |
| 116 | * event = parser.next(); // VALUE_STRING |
| 117 | * parser.getString(); // "John" |
| 118 | * </code> |
| 119 | * </pre> |
| 120 | * |
| 121 | * Starting in version 1.1, it is possible to build a partial JSON object |
| 122 | * model from the stream, at the current parser position. |
| 123 | * The methods {@link #getArray} and {@link #getObject} can be used to read in |
| 124 | * a {@code JsonArray} or {@code JsonObject}. For example, the following code |
| 125 | * shows how to obtain the phoneNumber in a JsonArray, from the JSON above: |
| 126 | * |
| 127 | * <pre><code> |
| 128 | * while (parser.hasNext() { |
| 129 | * Event event = parser.next(); |
| 130 | * if (event == JsonParser.Event.KEY_NAME ) { |
| 131 | * String key = parser.getString(); |
| 132 | * event = parser.next(); |
| 133 | * if (key.equals("phoneNumber") { |
| 134 | * JsonArray phones = parser.getArray(); |
| 135 | * } |
| 136 | * } |
| 137 | * } |
| 138 | * </code></pre> |
| 139 | * |
| 140 | * The methods {@link #getArrayStream} and {@link #getObjectStream} can be used |
| 141 | * to get a stream of the elements of a {@code JsonArray} or {@code JsonObject}. |
| 142 | * For example, the following code shows another way to obtain John's phoneNumber |
| 143 | * in a {@code JsonArray} : |
| 144 | * |
| 145 | * <pre>{@code |
| 146 | * Event event = parser.next(); // START_OBJECT |
| 147 | * JsonArray phones = (JsonArray) |
| 148 | * parser.getObjectStream().filter(e->e.getKey().equals("phoneNumber")) |
| 149 | * .map(e->e.getValue()) |
| 150 | * .findFirst() |
| 151 | * .get(); |
| 152 | * }</pre> |
| 153 | * |
| 154 | * The methods {@link #skipArray} and {@link #skipObject} can be used to |
| 155 | * skip tokens and position the parser to {@code END_ARRAY} or |
| 156 | * {@code END_OBJECT}. |
| 157 | * <p> |
| 158 | * {@code JsonParser} can be used to parse sequence of JSON values that are not |
| 159 | * enclosed in a JSON array, e.g. { } { }. The following code demonstrates how |
| 160 | * to parse such sequence. |
| 161 | * <pre><code> |
| 162 | * JsonParser parser = Json.createParser(...); |
| 163 | * while (parser.hasNext) { |
| 164 | * parser.next(); // advance parser state |
| 165 | * JsonValue value = parser.getValue(); |
| 166 | * } |
| 167 | * </code></pre> |
| 168 | * |
| 169 | * @see jakarta.json.Json |
| 170 | * @see JsonParserFactory |
| 171 | */ |
| 172 | public interface JsonParser extends /*Auto*/Closeable { |
| 173 | |
| 174 | /** |
| 175 | * An event from {@code JsonParser}. |
| 176 | */ |
| 177 | enum Event { |
| 178 | /** |
| 179 | * Start of a JSON array. The position of the parser is after '['. |
| 180 | */ |
| 181 | START_ARRAY, |
| 182 | /** |
| 183 | * Start of a JSON object. The position of the parser is after '{'. |
| 184 | */ |
| 185 | START_OBJECT, |
| 186 | /** |
| 187 | * Name in a name/value pair of a JSON object. The position of the parser |
| 188 | * is after the key name. The method {@link #getString} returns the key |
| 189 | * name. |
| 190 | */ |
| 191 | KEY_NAME, |
| 192 | /** |
| 193 | * String value in a JSON array or object. The position of the parser is |
| 194 | * after the string value. The method {@link #getString} |
| 195 | * returns the string value. |
| 196 | */ |
| 197 | VALUE_STRING, |
| 198 | /** |
| 199 | * Number value in a JSON array or object. The position of the parser is |
| 200 | * after the number value. {@code JsonParser} provides the following |
| 201 | * methods to access the number value: {@link #getInt}, |
| 202 | * {@link #getLong}, and {@link #getBigDecimal}. |
| 203 | */ |
| 204 | VALUE_NUMBER, |
| 205 | /** |
| 206 | * {@code true} value in a JSON array or object. The position of the |
| 207 | * parser is after the {@code true} value. |
| 208 | */ |
| 209 | VALUE_TRUE, |
| 210 | /** |
| 211 | * {@code false} value in a JSON array or object. The position of the |
| 212 | * parser is after the {@code false} value. |
| 213 | */ |
| 214 | VALUE_FALSE, |
| 215 | /** |
| 216 | * {@code null} value in a JSON array or object. The position of the |
| 217 | * parser is after the {@code null} value. |
| 218 | */ |
| 219 | VALUE_NULL, |
| 220 | /** |
| 221 | * End of a JSON object. The position of the parser is after '}'. |
| 222 | */ |
| 223 | END_OBJECT, |
| 224 | /** |
| 225 | * End of a JSON array. The position of the parser is after ']'. |
| 226 | */ |
| 227 | END_ARRAY |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Returns {@code true} if there are more parsing states. This method returns |
| 232 | * {@code false} if the parser reaches the end of the JSON text. |
| 233 | * |
| 234 | * @return {@code true} if there are more parsing states. |
| 235 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 236 | * would be cause of JsonException) |
| 237 | * @throws JsonParsingException if the parser encounters invalid JSON |
| 238 | * when advancing to next state. |
| 239 | */ |
| 240 | boolean hasNext(); |
| 241 | |
| 242 | /** |
| 243 | * Returns the event for the next parsing state. |
| 244 | * |
| 245 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 246 | * would be cause of JsonException) |
| 247 | * @throws JsonParsingException if the parser encounters invalid JSON |
| 248 | * when advancing to next state. |
| 249 | * @throws java.util.NoSuchElementException if there are no more parsing |
| 250 | * states. |
| 251 | * @return the event for the next parsing state |
| 252 | */ |
| 253 | Event next(); |
| 254 | |
| 255 | /** |
| 256 | * Returns the event for the current parsing state. |
| 257 | * |
| 258 | * @return the event for the current parsing state |
| 259 | * |
| 260 | * @since 2.1 |
| 261 | */ |
| 262 | default public Event currentEvent() { |
| 263 | throw new UnsupportedOperationException(); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Returns a {@code String} for the name in a name/value pair, |
| 268 | * for a string value or a number value. This method should only be called |
| 269 | * when the parser state is {@link Event#KEY_NAME}, {@link Event#VALUE_STRING}, |
| 270 | * or {@link Event#VALUE_NUMBER}. |
| 271 | * |
| 272 | * @return a name when the parser state is {@link Event#KEY_NAME} |
| 273 | * a string value when the parser state is {@link Event#VALUE_STRING} |
| 274 | * a number value when the parser state is {@link Event#VALUE_NUMBER} |
| 275 | * @throws IllegalStateException when the parser state is not |
| 276 | * {@code KEY_NAME}, {@code VALUE_STRING}, or {@code VALUE_NUMBER} |
| 277 | */ |
| 278 | String getString(); |
| 279 | |
| 280 | /** |
| 281 | * Returns true if the JSON number at the current parser state is a |
| 282 | * integral number. A {@link BigDecimal} may be used to store the value |
| 283 | * internally and this method semantics are defined using its |
| 284 | * {@code scale()}. If the scale is zero, then it is considered integral |
| 285 | * type. This integral type information can be used to invoke an |
| 286 | * appropriate accessor method to obtain a numeric value as in the |
| 287 | * following example: |
| 288 | * |
| 289 | * <pre> |
| 290 | * <code> |
| 291 | * JsonParser parser = ... |
| 292 | * if (parser.isIntegralNumber()) { |
| 293 | * parser.getInt(); // or other methods to get integral value |
| 294 | * } else { |
| 295 | * parser.getBigDecimal(); |
| 296 | * } |
| 297 | * </code> |
| 298 | * </pre> |
| 299 | * |
| 300 | * @return true if this number is a integral number, otherwise false |
| 301 | * @throws IllegalStateException when the parser state is not |
| 302 | * {@code VALUE_NUMBER} |
| 303 | */ |
| 304 | boolean isIntegralNumber(); |
| 305 | |
| 306 | /** |
| 307 | * Returns a JSON number as an integer. The returned value is equal |
| 308 | * to {@code new BigDecimal(getString()).intValue()}. Note that |
| 309 | * this conversion can lose information about the overall magnitude |
| 310 | * and precision of the number value as well as return a result with |
| 311 | * the opposite sign. This method should only be called when the parser |
| 312 | * state is {@link Event#VALUE_NUMBER}. |
| 313 | * |
| 314 | * @return an integer for a JSON number |
| 315 | * @throws IllegalStateException when the parser state is not |
| 316 | * {@code VALUE_NUMBER} |
| 317 | * @see java.math.BigDecimal#intValue() |
| 318 | */ |
| 319 | int getInt(); |
| 320 | |
| 321 | /** |
| 322 | * Returns a JSON number as a long. The returned value is equal |
| 323 | * to {@code new BigDecimal(getString()).longValue()}. Note that this |
| 324 | * conversion can lose information about the overall magnitude and |
| 325 | * precision of the number value as well as return a result with |
| 326 | * the opposite sign. This method is only called when the parser state is |
| 327 | * {@link Event#VALUE_NUMBER}. |
| 328 | * |
| 329 | * @return a long for a JSON number |
| 330 | * @throws IllegalStateException when the parser state is not |
| 331 | * {@code VALUE_NUMBER} |
| 332 | * @see java.math.BigDecimal#longValue() |
| 333 | */ |
| 334 | long getLong(); |
| 335 | |
| 336 | /** |
| 337 | * Returns a JSON number as a {@code BigDecimal}. The {@code BigDecimal} |
| 338 | * is created using {@code new BigDecimal(getString())}. This |
| 339 | * method should only called when the parser state is |
| 340 | * {@link Event#VALUE_NUMBER}. |
| 341 | * |
| 342 | * @return a {@code BigDecimal} for a JSON number |
| 343 | * @throws IllegalStateException when the parser state is not |
| 344 | * {@code VALUE_NUMBER} |
| 345 | */ |
| 346 | BigDecimal getBigDecimal(); |
| 347 | |
| 348 | /** |
| 349 | * Return the location that corresponds to the parser's current state in |
| 350 | * the JSON input source. The location information is only valid in the |
| 351 | * current parser state (or until the parser is advanced to a next state). |
| 352 | * |
| 353 | * @return a non-null location corresponding to the current parser state |
| 354 | * in JSON input source |
| 355 | */ |
| 356 | JsonLocation getLocation(); |
| 357 | |
| 358 | /** |
| 359 | * Returns a {@code JsonObject} and advances the parser to the |
| 360 | * corresponding {@code END_OBJECT}. |
| 361 | * |
| 362 | * @return the {@code JsonObject} at the current parser position |
| 363 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 364 | * would be cause of JsonException) |
| 365 | * @throws IllegalStateException when the parser state is not |
| 366 | * {@code START_OBJECT} |
| 367 | * @throws JsonParsingException if the parser encounters invalid JSON |
| 368 | * when advancing to next state. |
| 369 | * @throws java.util.NoSuchElementException if there are no more parsing |
| 370 | * states. |
| 371 | * |
| 372 | * @since 1.1 |
| 373 | */ |
| 374 | default public JsonObject getObject() { |
| 375 | throw new UnsupportedOperationException(); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Returns a {@code JsonValue} at the current parser position. |
| 380 | * If the parser state is {@code START_ARRAY}, the behavior is |
| 381 | * the same as {@link #getArray}. If the parser state is |
| 382 | * {@code START_OBJECT}, the behavior is the same as |
| 383 | * {@link #getObject}. For all other cases, if applicable, the JSON value is |
| 384 | * read and returned. |
| 385 | * |
| 386 | * @return the {@code JsonValue} at the current parser position. |
| 387 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 388 | * would be cause of JsonException) |
| 389 | * @throws IllegalStateException when the parser state is |
| 390 | * {@code END_OBJECT} or {@code END_ARRAY} |
| 391 | * @throws JsonParsingException if the parser encounters invalid JSON |
| 392 | * when advancing to next state. |
| 393 | * @throws java.util.NoSuchElementException if there are no more parsing |
| 394 | * states. |
| 395 | * |
| 396 | * @since 1.1 |
| 397 | */ |
| 398 | default public JsonValue getValue() { |
| 399 | throw new UnsupportedOperationException(); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Returns a {@code JsonArray} and advance the parser to the |
| 404 | * the corresponding {@code END_ARRAY}. |
| 405 | * |
| 406 | * @return the {@code JsonArray} at the current parser position |
| 407 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 408 | * would be cause of JsonException) |
| 409 | * @throws IllegalStateException when the parser state is not |
| 410 | * {@code START_ARRAY} |
| 411 | * @throws JsonParsingException if the parser encounters invalid JSON |
| 412 | * when advancing to next state. |
| 413 | * @throws java.util.NoSuchElementException if there are no more parsing |
| 414 | * states. |
| 415 | * |
| 416 | * @since 1.1 |
| 417 | */ |
| 418 | default public JsonArray getArray() { |
| 419 | throw new UnsupportedOperationException(); |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Returns a stream of the {@code JsonArray} elements. |
| 424 | * The parser state must be {@code START_ARRAY}. |
| 425 | * The elements are read lazily, on an as-needed basis, as |
| 426 | * required by the stream operations. |
| 427 | * If the stream operations do not consume |
| 428 | * all of the array elements, {@link skipArray} can be used to |
| 429 | * skip the unprocessed array elements. |
| 430 | * |
| 431 | * @return a stream of elements of the {@code JsonArray} |
| 432 | * |
| 433 | * @throws IllegalStateException when the parser state is not |
| 434 | * {@code START_ARRAY} |
| 435 | * |
| 436 | * @since 1.1 |
| 437 | */ |
| 438 | default public Stream<JsonValue> getArrayStream() { |
| 439 | throw new UnsupportedOperationException(); |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Returns a stream of the {@code JsonObject}'s |
| 444 | * name/value pairs. The parser state must be {@code START_OBJECT}. |
| 445 | * The name/value pairs are read lazily, on an as-needed basis, as |
| 446 | * required by the stream operations. |
| 447 | * If the stream operations do not consume |
| 448 | * all of the object's name/value pairs, {@link skipObject} can be |
| 449 | * used to skip the unprocessed elements. |
| 450 | * |
| 451 | * @return a stream of name/value pairs of the {@code JsonObject} |
| 452 | * |
| 453 | * @throws IllegalStateException when the parser state is not |
| 454 | * {@code START_OBJECT} |
| 455 | * |
| 456 | * @since 1.1 |
| 457 | */ |
| 458 | default public Stream<Map.Entry<String,JsonValue>> getObjectStream() { |
| 459 | throw new UnsupportedOperationException(); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Returns a stream of {@code JsonValue} from a sequence of |
| 464 | * JSON values. The values are read lazily, on an as-needed basis, |
| 465 | * as needed by the stream operations. |
| 466 | * |
| 467 | * @return a Stream of {@code JsonValue} |
| 468 | * |
| 469 | * @throws IllegalStateException if the parser is in an array or object. |
| 470 | * |
| 471 | * @since 1.1 |
| 472 | */ |
| 473 | default public Stream<JsonValue> getValueStream() { |
| 474 | throw new UnsupportedOperationException(); |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Advance the parser to {@code END_ARRAY}. |
| 479 | * If the parser is in array context, i.e. it has previously |
| 480 | * encountered a {@code START_ARRAY} without encountering the |
| 481 | * corresponding {@code END_ARRAY}, the parser is advanced to |
| 482 | * the corresponding {@code END_ARRAY}. |
| 483 | * If the parser is not in any array context, nothing happens. |
| 484 | * |
| 485 | * @since 1.1 |
| 486 | */ |
| 487 | default public void skipArray() { |
| 488 | throw new UnsupportedOperationException(); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Advance the parser to {@code END_OBJECT}. |
| 493 | * If the parser is in object context, i.e. it has previously |
| 494 | * encountered a {@code START_OBJECT} without encountering the |
| 495 | * corresponding {@code END_OBJECT}, the parser is advanced to |
| 496 | * the corresponding {@code END_OBJECT}. |
| 497 | * If the parser is not in any object context, nothing happens. |
| 498 | * |
| 499 | * @since 1.1 |
| 500 | */ |
| 501 | default public void skipObject() { |
| 502 | throw new UnsupportedOperationException(); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Closes this parser and frees any resources associated with the |
| 507 | * parser. This method closes the underlying input source. |
| 508 | * |
| 509 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 510 | * would be cause of JsonException) |
| 511 | */ |
| 512 | @Override |
| 513 | void close(); |
| 514 | } |