[6756] | 1 | /*
|
---|
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
---|
| 3 | *
|
---|
| 4 | * Copyright (c) 2011-2013 Oracle and/or its affiliates. All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * The contents of this file are subject to the terms of either the GNU
|
---|
| 7 | * General Public License Version 2 only ("GPL") or the Common Development
|
---|
| 8 | * and Distribution License("CDDL") (collectively, the "License"). You
|
---|
| 9 | * may not use this file except in compliance with the License. You can
|
---|
| 10 | * obtain a copy of the License at
|
---|
| 11 | * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
|
---|
| 12 | * or packager/legal/LICENSE.txt. See the License for the specific
|
---|
| 13 | * language governing permissions and limitations under the License.
|
---|
| 14 | *
|
---|
| 15 | * When distributing the software, include this License Header Notice in each
|
---|
| 16 | * file and include the License file at packager/legal/LICENSE.txt.
|
---|
| 17 | *
|
---|
| 18 | * GPL Classpath Exception:
|
---|
| 19 | * Oracle designates this particular file as subject to the "Classpath"
|
---|
| 20 | * exception as provided by Oracle in the GPL Version 2 section of the License
|
---|
| 21 | * file that accompanied this code.
|
---|
| 22 | *
|
---|
| 23 | * Modifications:
|
---|
| 24 | * If applicable, add the following below the License Header, with the fields
|
---|
| 25 | * enclosed by brackets [] replaced by your own identifying information:
|
---|
| 26 | * "Portions Copyright [year] [name of copyright owner]"
|
---|
| 27 | *
|
---|
| 28 | * Contributor(s):
|
---|
| 29 | * If you wish your version of this file to be governed by only the CDDL or
|
---|
| 30 | * only the GPL Version 2, indicate your decision by adding "[Contributor]
|
---|
| 31 | * elects to include this software in this distribution under the [CDDL or GPL
|
---|
| 32 | * Version 2] license." If you don't indicate a single choice of license, a
|
---|
| 33 | * recipient has the option to distribute your version of this file under
|
---|
| 34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to
|
---|
| 35 | * its licensees as provided above. However, if you add GPL Version 2 code
|
---|
| 36 | * and therefore, elected the GPL Version 2 license, then the option applies
|
---|
| 37 | * only if the new code is made subject to such option by the copyright
|
---|
| 38 | * holder.
|
---|
| 39 | */
|
---|
| 40 |
|
---|
| 41 | package javax.json.stream;
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | import java.io.Closeable;
|
---|
| 45 | import java.math.BigDecimal;
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * Provides forward, read-only access to JSON data in a streaming way. This
|
---|
| 49 | * is the most efficient way for reading JSON data. The class
|
---|
| 50 | * {@link javax.json.Json} contains methods to create parsers from input
|
---|
| 51 | * sources ({@link java.io.InputStream} and {@link java.io.Reader}).
|
---|
| 52 | *
|
---|
| 53 | * <p>
|
---|
| 54 | * The following example demonstrates how to create a parser from a string
|
---|
| 55 | * that contains an empty JSON array:
|
---|
| 56 | * <pre>
|
---|
| 57 | * <code>
|
---|
| 58 | * JsonParser parser = Json.createParser(new StringReader("[]"));
|
---|
| 59 | * </code>
|
---|
| 60 | * </pre>
|
---|
| 61 | *
|
---|
| 62 | * <p>
|
---|
| 63 | * The class {@link JsonParserFactory} also contains methods to create
|
---|
| 64 | * {@code JsonParser} instances. {@link JsonParserFactory} is preferred
|
---|
| 65 | * when creating multiple parser instances. A sample usage is shown
|
---|
| 66 | * in the following example:
|
---|
| 67 | * <pre>
|
---|
| 68 | * <code>
|
---|
| 69 | * JsonParserFactory factory = Json.createParserFactory();
|
---|
| 70 | * JsonParser parser1 = factory.createParser(...);
|
---|
| 71 | * JsonParser parser2 = factory.createParser(...);
|
---|
| 72 | * </code>
|
---|
| 73 | * </pre>
|
---|
| 74 | *
|
---|
| 75 | * <p>
|
---|
| 76 | * {@code JsonParser} parses JSON using the pull parsing programming model.
|
---|
| 77 | * In this model the client code controls the thread and calls the method
|
---|
| 78 | * {@code next()} to advance the parser to the next state after
|
---|
| 79 | * processing each element. The parser can generate the following events:
|
---|
| 80 | * {@code START_OBJECT}, {@code END_OBJECT}, {@code START_ARRAY},
|
---|
| 81 | * {@code END_ARRAY}, {@code KEY_NAME}, {@code VALUE_STRING},
|
---|
| 82 | * {@code VALUE_NUMBER}, {@code VALUE_TRUE}, {@code VALUE_FALSE},
|
---|
| 83 | * and {@code VALUE_NULL}.
|
---|
| 84 | *
|
---|
| 85 | * <p>
|
---|
| 86 | * <b>For example</b>, for an empty JSON object ({ }), the parser generates the event
|
---|
| 87 | * {@code START_OBJECT} with the first call to the method {@code next()} and the
|
---|
| 88 | * event {@code END_OBJECT} with the second call to the method {@code next()}.
|
---|
| 89 | * The following code demonstrates how to access these events:
|
---|
| 90 | *
|
---|
| 91 | * <pre>
|
---|
| 92 | * <code>
|
---|
| 93 | * Event event = parser.next(); // START_OBJECT
|
---|
| 94 | * event = parser.next(); // END_OBJECT
|
---|
| 95 | * </code>
|
---|
| 96 | * </pre>
|
---|
| 97 | *
|
---|
| 98 | * <p>
|
---|
| 99 | * <a id="JsonParserExample2"/>
|
---|
| 100 | * <p>
|
---|
| 101 | * <b>For example</b>, for the following JSON:
|
---|
| 102 | * <pre>
|
---|
| 103 | * {
|
---|
| 104 | * "firstName": "John", "lastName": "Smith", "age": 25,
|
---|
| 105 | * "phoneNumber": [
|
---|
| 106 | * { "type": "home", "number": "212 555-1234" },
|
---|
| 107 | * { "type": "fax", "number": "646 555-4567" }
|
---|
| 108 | * ]
|
---|
| 109 | * }
|
---|
| 110 | * </pre>
|
---|
| 111 | *
|
---|
| 112 | * <p>calls to the method {@code next()} result in parse events at the specified
|
---|
| 113 | * locations below (marked in bold):
|
---|
| 114 | *
|
---|
| 115 | * <p>
|
---|
| 116 | * <pre>
|
---|
| 117 | * {<B>START_OBJECT</B>
|
---|
| 118 | * "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>,
|
---|
| 119 | * "phoneNumber"<B>KEY_NAME</B> : [<B>START_ARRAY</B>
|
---|
| 120 | * {<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>,
|
---|
| 121 | * {<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>
|
---|
| 122 | * ]<B>END_ARRAY</B>
|
---|
| 123 | * }<B>END_OBJECT</B>
|
---|
| 124 | * </pre>
|
---|
| 125 | *
|
---|
| 126 | * <p>
|
---|
| 127 | * The methods {@code next()} and {@code hasNext()} enable iteration over
|
---|
| 128 | * parser events to process JSON data. {@code JsonParser} provides get methods
|
---|
| 129 | * to obtain the value at the current state of the parser. For example, the
|
---|
| 130 | * following code shows how to obtain the value "John" from the JSON above:
|
---|
| 131 | *
|
---|
| 132 | * <p>
|
---|
| 133 | * <pre>
|
---|
| 134 | * <code>
|
---|
| 135 | * Event event = parser.next(); // START_OBJECT
|
---|
| 136 | * event = parser.next(); // KEY_NAME
|
---|
| 137 | * event = parser.next(); // VALUE_STRING
|
---|
| 138 | * parser.getString(); // "John"
|
---|
| 139 | * </code>
|
---|
| 140 | * </pre>
|
---|
| 141 | *
|
---|
| 142 | * @see javax.json.Json
|
---|
| 143 | * @see JsonParserFactory
|
---|
| 144 | * @author Jitendra Kotamraju
|
---|
| 145 | */
|
---|
| 146 | public interface JsonParser extends /*Auto*/Closeable {
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * An event from {@code JsonParser}.
|
---|
| 150 | */
|
---|
| 151 | enum Event {
|
---|
| 152 | /**
|
---|
| 153 | * Start of a JSON array. The position of the parser is after '['.
|
---|
| 154 | */
|
---|
| 155 | START_ARRAY,
|
---|
| 156 | /**
|
---|
| 157 | * Start of a JSON object. The position of the parser is after '{'.
|
---|
| 158 | */
|
---|
| 159 | START_OBJECT,
|
---|
| 160 | /**
|
---|
| 161 | * Name in a name/value pair of a JSON object. The position of the parser
|
---|
| 162 | * is after the key name. The method {@link #getString} returns the key
|
---|
| 163 | * name.
|
---|
| 164 | */
|
---|
| 165 | KEY_NAME,
|
---|
| 166 | /**
|
---|
| 167 | * String value in a JSON array or object. The position of the parser is
|
---|
| 168 | * after the string value. The method {@link #getString}
|
---|
| 169 | * returns the string value.
|
---|
| 170 | */
|
---|
| 171 | VALUE_STRING,
|
---|
| 172 | /**
|
---|
| 173 | * Number value in a JSON array or object. The position of the parser is
|
---|
| 174 | * after the number value. {@code JsonParser} provides the following
|
---|
| 175 | * methods to access the number value: {@link #getInt},
|
---|
| 176 | * {@link #getLong}, and {@link #getBigDecimal}.
|
---|
| 177 | */
|
---|
| 178 | VALUE_NUMBER,
|
---|
| 179 | /**
|
---|
| 180 | * {@code true} value in a JSON array or object. The position of the
|
---|
| 181 | * parser is after the {@code true} value.
|
---|
| 182 | */
|
---|
| 183 | VALUE_TRUE,
|
---|
| 184 | /**
|
---|
| 185 | * {@code false} value in a JSON array or object. The position of the
|
---|
| 186 | * parser is after the {@code false} value.
|
---|
| 187 | */
|
---|
| 188 | VALUE_FALSE,
|
---|
| 189 | /**
|
---|
| 190 | * {@code null} value in a JSON array or object. The position of the
|
---|
| 191 | * parser is after the {@code null} value.
|
---|
| 192 | */
|
---|
| 193 | VALUE_NULL,
|
---|
| 194 | /**
|
---|
| 195 | * End of a JSON object. The position of the parser is after '}'.
|
---|
| 196 | */
|
---|
| 197 | END_OBJECT,
|
---|
| 198 | /**
|
---|
| 199 | * End of a JSON array. The position of the parser is after ']'.
|
---|
| 200 | */
|
---|
| 201 | END_ARRAY
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /**
|
---|
| 205 | * Returns {@code true} if there are more parsing states. This method returns
|
---|
| 206 | * {@code false} if the parser reaches the end of the JSON text.
|
---|
| 207 | *
|
---|
| 208 | * @return {@code true} if there are more parsing states.
|
---|
| 209 | * @throws javax.json.JsonException if an i/o error occurs (IOException
|
---|
| 210 | * would be cause of JsonException)
|
---|
| 211 | * @throws JsonParsingException if the parser encounters invalid JSON
|
---|
| 212 | * when advancing to next state.
|
---|
| 213 | */
|
---|
| 214 | boolean hasNext();
|
---|
| 215 |
|
---|
| 216 | /**
|
---|
| 217 | * Returns the event for the next parsing state.
|
---|
| 218 | *
|
---|
| 219 | * @throws javax.json.JsonException if an i/o error occurs (IOException
|
---|
| 220 | * would be cause of JsonException)
|
---|
| 221 | * @throws JsonParsingException if the parser encounters invalid JSON
|
---|
| 222 | * when advancing to next state.
|
---|
| 223 | * @throws java.util.NoSuchElementException if there are no more parsing
|
---|
| 224 | * states.
|
---|
| 225 | */
|
---|
| 226 | Event next();
|
---|
| 227 |
|
---|
| 228 | /**
|
---|
| 229 | * Returns a {@code String} for the name in a name/value pair,
|
---|
| 230 | * for a string value or a number value. This method should only be called
|
---|
| 231 | * when the parser state is {@link Event#KEY_NAME}, {@link Event#VALUE_STRING},
|
---|
| 232 | * or {@link Event#VALUE_NUMBER}.
|
---|
| 233 | *
|
---|
| 234 | * @return a name when the parser state is {@link Event#KEY_NAME}
|
---|
| 235 | * a string value when the parser state is {@link Event#VALUE_STRING}
|
---|
| 236 | * a number value when the parser state is {@link Event#VALUE_NUMBER}
|
---|
| 237 | * @throws IllegalStateException when the parser state is not
|
---|
| 238 | * {@code KEY_NAME}, {@code VALUE_STRING}, or {@code VALUE_NUMBER}
|
---|
| 239 | */
|
---|
| 240 | String getString();
|
---|
| 241 |
|
---|
| 242 | /**
|
---|
| 243 | * Returns true if the JSON number at the current parser state is a
|
---|
| 244 | * integral number. A {@link BigDecimal} may be used to store the value
|
---|
| 245 | * internally and this method semantics are defined using its
|
---|
| 246 | * {@code scale()}. If the scale is zero, then it is considered integral
|
---|
| 247 | * type. This integral type information can be used to invoke an
|
---|
| 248 | * appropriate accessor method to obtain a numeric value as in the
|
---|
| 249 | * following example:
|
---|
| 250 | *
|
---|
| 251 | * <pre>
|
---|
| 252 | * <code>
|
---|
| 253 | * JsonParser parser = ...
|
---|
| 254 | * if (parser.isIntegralNumber()) {
|
---|
| 255 | * parser.getInt(); // or other methods to get integral value
|
---|
| 256 | * } else {
|
---|
| 257 | * parser.getBigDecimal();
|
---|
| 258 | * }
|
---|
| 259 | * </code>
|
---|
| 260 | * </pre>
|
---|
| 261 | *
|
---|
| 262 | * @return true if this number is a integral number, otherwise false
|
---|
| 263 | * @throws IllegalStateException when the parser state is not
|
---|
| 264 | * {@code VALUE_NUMBER}
|
---|
| 265 | */
|
---|
| 266 | boolean isIntegralNumber();
|
---|
| 267 |
|
---|
| 268 | /**
|
---|
| 269 | * Returns a JSON number as an integer. The returned value is equal
|
---|
| 270 | * to {@code new BigDecimal(getString()).intValue()}. Note that
|
---|
| 271 | * this conversion can lose information about the overall magnitude
|
---|
| 272 | * and precision of the number value as well as return a result with
|
---|
| 273 | * the opposite sign. This method should only be called when the parser
|
---|
| 274 | * state is {@link Event#VALUE_NUMBER}.
|
---|
| 275 | *
|
---|
| 276 | * @return an integer for a JSON number
|
---|
| 277 | * @throws IllegalStateException when the parser state is not
|
---|
| 278 | * {@code VALUE_NUMBER}
|
---|
| 279 | * @see java.math.BigDecimal#intValue()
|
---|
| 280 | */
|
---|
| 281 | int getInt();
|
---|
| 282 |
|
---|
| 283 | /**
|
---|
| 284 | * Returns a JSON number as a long. The returned value is equal
|
---|
| 285 | * to {@code new BigDecimal(getString()).longValue()}. Note that this
|
---|
| 286 | * conversion can lose information about the overall magnitude and
|
---|
| 287 | * precision of the number value as well as return a result with
|
---|
| 288 | * the opposite sign. This method is only called when the parser state is
|
---|
| 289 | * {@link Event#VALUE_NUMBER}.
|
---|
| 290 | *
|
---|
| 291 | * @return a long for a JSON number
|
---|
| 292 | * @throws IllegalStateException when the parser state is not
|
---|
| 293 | * {@code VALUE_NUMBER}
|
---|
| 294 | * @see java.math.BigDecimal#longValue()
|
---|
| 295 | */
|
---|
| 296 | long getLong();
|
---|
| 297 |
|
---|
| 298 | /**
|
---|
| 299 | * Returns a JSON number as a {@code BigDecimal}. The {@code BigDecimal}
|
---|
| 300 | * is created using {@code new BigDecimal(getString())}. This
|
---|
| 301 | * method should only called when the parser state is
|
---|
| 302 | * {@link Event#VALUE_NUMBER}.
|
---|
| 303 | *
|
---|
| 304 | * @return a {@code BigDecimal} for a JSON number
|
---|
| 305 | * @throws IllegalStateException when the parser state is not
|
---|
| 306 | * {@code VALUE_NUMBER}
|
---|
| 307 | */
|
---|
| 308 | BigDecimal getBigDecimal();
|
---|
| 309 |
|
---|
| 310 | /**
|
---|
| 311 | * Return the location that corresponds to the parser's current state in
|
---|
| 312 | * the JSON input source. The location information is only valid in the
|
---|
| 313 | * current parser state (or until the parser is advanced to a next state).
|
---|
| 314 | *
|
---|
| 315 | * @return a non-null location corresponding to the current parser state
|
---|
| 316 | * in JSON input source
|
---|
| 317 | */
|
---|
| 318 | JsonLocation getLocation();
|
---|
| 319 |
|
---|
| 320 | /**
|
---|
| 321 | * getJsonValue(JsonObject.class) is valid in the START_OBJECT state and
|
---|
| 322 | * moves the cursor to END_OBJECT.
|
---|
| 323 | *
|
---|
| 324 | * getJsonValue(JsonArray.class) is valid in the START_ARRAY state
|
---|
| 325 | * and moves the cursor to END_ARRAY.
|
---|
| 326 | *
|
---|
| 327 | * getJsonValue(JsonString.class) is valid in the VALUE_STRING state.
|
---|
| 328 | *
|
---|
| 329 | * getJsonValue(JsonNumber.class) is valid in the VALUE_NUMBER state.
|
---|
| 330 | *
|
---|
| 331 | * @param clazz
|
---|
| 332 | * @return
|
---|
| 333 | *
|
---|
| 334 | public <T extends JsonValue> T getJsonValue(Class<T> clazz);
|
---|
| 335 | */
|
---|
| 336 |
|
---|
| 337 | /**
|
---|
| 338 | * Closes this parser and frees any resources associated with the
|
---|
| 339 | * parser. This method closes the underlying input source.
|
---|
| 340 | *
|
---|
| 341 | * @throws javax.json.JsonException if an i/o error occurs (IOException
|
---|
| 342 | * would be cause of JsonException)
|
---|
| 343 | */
|
---|
| 344 | @Override
|
---|
| 345 | void close();
|
---|
| 346 |
|
---|
| 347 | }
|
---|