source: josm/trunk/src/javax/json/JsonArray.java@ 12942

Last change on this file since 12942 was 6756, checked in by Don-vip, 11 years ago

fix #9590 - replace org.json with GPL-compliant jsonp + remove mention of old world image removed in r1680

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