source: josm/trunk/src/javax/json/JsonObject.java@ 7608

Last change on this file since 7608 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: 11.1 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.Map;
44
45/**
46 * {@code JsonObject} class represents an immutable JSON object value
47 * (an unordered collection of zero or more name/value pairs).
48 * It also provides unmodifiable map view to the JSON object
49 * name/value mappings.
50 *
51 * <p>A JsonObject instance can be created from an input source using
52 * {@link JsonReader#readObject()}. For example:
53 * <pre><code>
54 * JsonReader jsonReader = Json.createReader(...);
55 * JsonObject object = jsonReader.readObject();
56 * jsonReader.close();
57 * </code></pre>
58 *
59 * It can also be built from scratch using a {@link JsonObjectBuilder}.
60 *
61 * <p>For example 1: An empty JSON object can be built as follows:
62 * <pre><code>
63 * JsonObject object = Json.createObjectBuilder().build();
64 * </code></pre>
65 *
66 * For example 2: The following JSON
67 * <pre><code>
68 * {
69 * "firstName": "John", "lastName": "Smith", "age": 25,
70 * "address" : {
71 * "streetAddress": "21 2nd Street",
72 * "city": "New York",
73 * "state": "NY",
74 * "postalCode": "10021"
75 * },
76 * "phoneNumber": [
77 * { "type": "home", "number": "212 555-1234" },
78 * { "type": "fax", "number": "646 555-4567" }
79 * ]
80 * }
81 * </code></pre>
82 * can be built using :
83 * <pre><code>
84 * JsonObject value = Json.createObjectBuilder()
85 * .add("firstName", "John")
86 * .add("lastName", "Smith")
87 * .add("age", 25)
88 * .add("address", Json.createObjectBuilder()
89 * .add("streetAddress", "21 2nd Street")
90 * .add("city", "New York")
91 * .add("state", "NY")
92 * .add("postalCode", "10021"))
93 * .add("phoneNumber", Json.createArrayBuilder()
94 * .add(Json.createObjectBuilder()
95 * .add("type", "home")
96 * .add("number", "212 555-1234"))
97 * .add(Json.createObjectBuilder()
98 * .add("type", "fax")
99 * .add("number", "646 555-4567")))
100 * .build();
101 * </code></pre>
102 *
103 * {@code JsonObject} can be written to JSON as follows:
104 * <pre><code>
105 * JsonWriter writer = ...
106 * JsonObject obj = ...;
107 * writer.writeObject(obj);
108 * </code></pre>
109 *
110 * {@code JsonObject} values can be {@link JsonObject}, {@link JsonArray},
111 * {@link JsonString}, {@link JsonNumber}, {@link JsonValue#TRUE},
112 * {@link JsonValue#FALSE}, {@link JsonValue#NULL}. These values can be
113 * accessed using various accessor methods.
114 *
115 * <p>In the above example 2, "John" can be got using
116 * <pre><code>
117 * String firstName = object.getString("firstName");
118 * </code></pre>
119 *
120 * This map object provides read-only access to the JSON object data,
121 * and attempts to modify the map, whether direct or via its collection
122 * views, result in an {@code UnsupportedOperationException}.
123 *
124 * <p>The map object's iteration ordering is based on the order in which
125 * name/value pairs are added to the corresponding builder or the order
126 * in which name/value pairs appear in the corresponding stream.
127 *
128 * @author Jitendra Kotamraju
129 */
130public interface JsonObject extends JsonStructure, Map<String, JsonValue> {
131
132 /**
133 * Returns the array value to which the specified name is mapped.
134 * This is a convenience method for {@code (JsonArray)get(name)} to
135 * get the value.
136 *
137 * @param name the name whose associated value is to be returned
138 * @return the array value to which the specified name is mapped, or
139 * {@code null} if this object contains no mapping for the name
140 * @throws ClassCastException if the value to which the specified name
141 * is mapped is not assignable to JsonArray type
142 */
143 JsonArray getJsonArray(String name);
144
145 /**
146 * Returns the object value to which the specified name is mapped.
147 * This is a convenience method for {@code (JsonObject)get(name)} to
148 * get the value.
149 *
150 * @param name the name whose associated value is to be returned
151 * @return the object value to which the specified name is mapped, or
152 * {@code null} if this object contains no mapping for the name
153 * @throws ClassCastException if the value to which the specified name
154 * is mapped is not assignable to JsonObject type
155 */
156 JsonObject getJsonObject(String name);
157
158 /**
159 * Returns the number value to which the specified name is mapped.
160 * This is a convenience method for {@code (JsonNumber)get(name)} to
161 * get the value.
162 *
163 * @param name the name whose associated value is to be returned
164 * @return the number value to which the specified name is mapped, or
165 * {@code null} if this object contains no mapping for the name
166 * @throws ClassCastException if the value to which the specified name
167 * is mapped is not assignable to JsonNumber type
168 */
169 JsonNumber getJsonNumber(String name);
170
171 /**
172 * Returns the string value to which the specified name is mapped.
173 * This is a convenience method for {@code (JsonString)get(name)} to
174 * get the value.
175 *
176 * @param name the name whose associated value is to be returned
177 * @return the string value to which the specified name is mapped, or
178 * {@code null} if this object contains no mapping for the name
179 * @throws ClassCastException if the value to which the specified name
180 * is mapped is not assignable to JsonString type
181 */
182 JsonString getJsonString(String name);
183
184 /**
185 * A convenience method for
186 * {@code getJsonString(name).getString()}
187 *
188 * @param name whose associated value is to be returned as String
189 * @return the String value to which the specified name is mapped
190 * @throws NullPointerException if the specified name doesn't have any
191 * mapping
192 * @throws ClassCastException if the value for specified name mapping
193 * is not assignable to JsonString
194 */
195 String getString(String name);
196
197 /**
198 * Returns the string value of the associated {@code JsonString} mapping
199 * for the specified name. If {@code JsonString} is found, then its
200 * {@link javax.json.JsonString#getString()} is returned. Otherwise,
201 * the specified default value is returned.
202 *
203 * @param name whose associated value is to be returned as String
204 * @param defaultValue a default value to be returned
205 * @return the string value of the associated mapping for the name,
206 * or the default value
207 */
208 String getString(String name, String defaultValue);
209
210 /**
211 * A convenience method for
212 * {@code getJsonNumber(name).intValue()}
213 *
214 * @param name whose associated value is to be returned as int
215 * @return the int value to which the specified name is mapped
216 * @throws NullPointerException if the specified name doesn't have any
217 * mapping
218 * @throws ClassCastException if the value for specified name mapping
219 * is not assignable to JsonNumber
220 */
221 int getInt(String name);
222
223 /**
224 * Returns the int value of the associated {@code JsonNumber} mapping
225 * for the specified name. If {@code JsonNumber} is found, then its
226 * {@link javax.json.JsonNumber#intValue()} is returned. Otherwise,
227 * the specified default value is returned.
228 *
229 * @param name whose associated value is to be returned as int
230 * @param defaultValue a default value to be returned
231 * @return the int value of the associated mapping for the name,
232 * or the default value
233 */
234 int getInt(String name, int defaultValue);
235
236 /**
237 * Returns the boolean value of the associated mapping for the specified
238 * name. If the associated mapping is JsonValue.TRUE, then returns true.
239 * If the associated mapping is JsonValue.FALSE, then returns false.
240 *
241 * @param name whose associated value is to be returned as boolean
242 * @return the boolean value to which the specified name is mapped
243 * @throws NullPointerException if the specified name doesn't have any
244 * mapping
245 * @throws ClassCastException if the value for specified name mapping
246 * is not assignable to JsonValue.TRUE or JsonValue.FALSE
247 */
248 boolean getBoolean(String name);
249
250 /**
251 * Returns the boolean value of the associated mapping for the specified
252 * name. If the associated mapping is JsonValue.TRUE, then returns true.
253 * If the associated mapping is JsonValue.FALSE, then returns false.
254 * Otherwise, the specified default value is returned.
255 *
256 * @param name whose associated value is to be returned as int
257 * @param defaultValue a default value to be returned
258 * @return the boolean value of the associated mapping for the name,
259 * or the default value
260 */
261 boolean getBoolean(String name, boolean defaultValue);
262
263 /**
264 * Returns {@code true} if the associated value for the specified name is
265 * {@code JsonValue.NULL}.
266 *
267 * @param name name whose associated value is checked
268 * @return return true if the associated value is {@code JsonValue.NUL},
269 * otherwise false
270 * @throws NullPointerException if the specified name doesn't have any
271 * mapping
272 */
273 boolean isNull(String name);
274
275}
Note: See TracBrowser for help on using the repository browser.