Debian Codegraph @

src/jakarta/json/JsonObject.java

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