| 1 | /* |
| 2 | * Copyright (c) 2012, 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.io.Closeable; |
| 20 | |
| 21 | /** |
| 22 | * Reads a JSON {@link JsonObject object} or an {@link JsonArray array} |
| 23 | * structure from an input source. |
| 24 | * |
| 25 | * <p>The class {@link jakarta.json.Json} contains methods to create readers from |
| 26 | * input sources ({@link java.io.InputStream} and {@link java.io.Reader}). |
| 27 | * |
| 28 | * <p> |
| 29 | * The following example demonstrates how to read an empty JSON array from |
| 30 | * a string: |
| 31 | * <pre> |
| 32 | * <code> |
| 33 | * JsonReader jsonReader = Json.createReader(new StringReader("[]")); |
| 34 | * JsonArray array = jsonReader.readArray(); |
| 35 | * jsonReader.close(); |
| 36 | * </code> |
| 37 | * </pre> |
| 38 | * |
| 39 | * <p> |
| 40 | * The class {@link JsonReaderFactory} also contains methods to create |
| 41 | * {@code JsonReader} instances. A factory instance can be used to create |
| 42 | * multiple reader instances with the same configuration. This the preferred |
| 43 | * way to create multiple instances. A sample usage is shown in the following |
| 44 | * example: |
| 45 | * <pre> |
| 46 | * <code> |
| 47 | * JsonReaderFactory factory = Json.createReaderFactory(config); |
| 48 | * JsonReader reader1 = factory.createReader(...); |
| 49 | * JsonReader reader2 = factory.createReader(...); |
| 50 | * </code> |
| 51 | * </pre> |
| 52 | */ |
| 53 | public interface JsonReader extends /*Auto*/Closeable { |
| 54 | |
| 55 | /** |
| 56 | * Returns a JSON array or object that is represented in |
| 57 | * the input source. This method needs to be called |
| 58 | * only once for a reader instance. |
| 59 | * |
| 60 | * @return a JSON object or array |
| 61 | * @throws JsonException if a JSON object or array cannot |
| 62 | * be created due to i/o error (IOException would be |
| 63 | * cause of JsonException) |
| 64 | * @throws jakarta.json.stream.JsonParsingException if a JSON object or array |
| 65 | * cannot be created due to incorrect representation |
| 66 | * @throws IllegalStateException if read, readObject, readArray, |
| 67 | * readValue or close method is already called |
| 68 | */ |
| 69 | JsonStructure read(); |
| 70 | |
| 71 | /** |
| 72 | * Returns a JSON object that is represented in |
| 73 | * the input source. This method needs to be called |
| 74 | * only once for a reader instance. |
| 75 | * |
| 76 | * @return a JSON object |
| 77 | * @throws JsonException if a JSON object cannot |
| 78 | * be created due to i/o error (IOException would be |
| 79 | * cause of JsonException) |
| 80 | * @throws jakarta.json.stream.JsonParsingException if a JSON object cannot |
| 81 | * be created due to incorrect representation |
| 82 | * @throws IllegalStateException if read, readObject, readArray, |
| 83 | * readValue or close method is already called |
| 84 | */ |
| 85 | JsonObject readObject(); |
| 86 | |
| 87 | /** |
| 88 | * Returns a JSON array that is represented in |
| 89 | * the input source. This method needs to be called |
| 90 | * only once for a reader instance. |
| 91 | * |
| 92 | * @return a JSON array |
| 93 | * @throws JsonException if a JSON array cannot |
| 94 | * be created due to i/o error (IOException would be |
| 95 | * cause of JsonException) |
| 96 | * @throws jakarta.json.stream.JsonParsingException if a JSON array cannot |
| 97 | * be created due to incorrect representation |
| 98 | * @throws IllegalStateException if read, readObject, readArray, |
| 99 | * readValue or close method is already called |
| 100 | */ |
| 101 | JsonArray readArray(); |
| 102 | |
| 103 | /** |
| 104 | * Returns a JSON value that is represented in |
| 105 | * the input source. This method needs to be called |
| 106 | * only once for a reader instance. |
| 107 | * |
| 108 | * @return a JSON value |
| 109 | * @throws JsonException if a JSON value |
| 110 | * be created due to i/o error (IOException would be |
| 111 | * cause of JsonException) |
| 112 | * @throws jakarta.json.stream.JsonParsingException if a JSON value |
| 113 | * cannot be created due to incorrect representation |
| 114 | * @throws IllegalStateException if read, readObject, readArray, |
| 115 | * readValue or close method is already called |
| 116 | * |
| 117 | * @since 1.1 |
| 118 | */ |
| 119 | default JsonValue readValue() { |
| 120 | throw new UnsupportedOperationException(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Closes this reader and frees any resources associated with the |
| 125 | * reader. This method closes the underlying input source. |
| 126 | * |
| 127 | * @throws JsonException if an i/o error occurs (IOException would be |
| 128 | * cause of JsonException) |
| 129 | */ |
| 130 | @Override |
| 131 | void close(); |
| 132 | |
| 133 | } |