source: josm/trunk/src/javax/json/JsonObjectBuilder.java@ 14133

Last change on this file since 14133 was 13231, checked in by Don-vip, 7 years ago

see #15682 - upgrade to JSR 374 (JSON Processing) API 1.1.2

File size: 11.8 KB
Line 
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2013-2017 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://oss.oracle.com/licenses/CDDL+GPL-1.1
12 * or 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 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.math.BigDecimal;
44import java.math.BigInteger;
45
46/**
47 * A builder for creating {@link JsonObject} models from scratch. This
48 * interface initializes an empty JSON object model and provides methods to add
49 * name/value pairs to the object model and to return the resulting object.
50 * The methods in this class can be chained to add multiple name/value pairs
51 * to the object.
52 *
53 * <p>The class {@link javax.json.Json} contains methods to create the builder
54 * object. The example code below shows how to build an empty {@code JsonObject}
55 * instance.
56 * <pre>
57 * <code>
58 * JsonObject object = Json.createObjectBuilder().build();
59 * </code>
60 * </pre>
61 *
62 * <p>The class {@link JsonBuilderFactory} also contains methods to create
63 * {@code JsonObjectBuilder} instances. A factory instance can be used to create
64 * multiple builder instances with the same configuration. This the preferred
65 * way to create multiple instances.
66 *
67 * The example code below shows how to build a {@code JsonObject} model that
68 * represents the following JSON object:
69 *
70 * <pre>
71 * <code>
72 * {
73 * "firstName": "John", "lastName": "Smith", "age": 25,
74 * "address" : {
75 * "streetAddress": "21 2nd Street",
76 * "city": "New York",
77 * "state": "NY",
78 * "postalCode": "10021"
79 * },
80 * "phoneNumber": [
81 * { "type": "home", "number": "212 555-1234" },
82 * { "type": "fax", "number": "646 555-4567" }
83 * ]
84 * }
85 * </code>
86 * </pre>
87 *
88 * <p>The code to create the object shown above is the following:
89 *
90 * <pre>
91 * <code>
92 * JsonBuilderFactory factory = Json.createBuilderFactory(config);
93 * JsonObject value = factory.createObjectBuilder()
94 * .add("firstName", "John")
95 * .add("lastName", "Smith")
96 * .add("age", 25)
97 * .add("address", factory.createObjectBuilder()
98 * .add("streetAddress", "21 2nd Street")
99 * .add("city", "New York")
100 * .add("state", "NY")
101 * .add("postalCode", "10021"))
102 * .add("phoneNumber", factory.createArrayBuilder()
103 * .add(factory.createObjectBuilder()
104 * .add("type", "home")
105 * .add("number", "212 555-1234"))
106 * .add(factory.createObjectBuilder()
107 * .add("type", "fax")
108 * .add("number", "646 555-4567")))
109 * .build();
110 * </code>
111 * </pre>
112 *
113 * <p>This class does <em>not</em> allow <tt>null</tt> to be used as a name or
114 * value while building the JSON object
115 *
116 * @see JsonArrayBuilder
117 */
118public interface JsonObjectBuilder {
119
120 /**
121 * Adds a name/{@code JsonValue} pair to the JSON object associated with
122 * this object builder. If the object contains a mapping for the specified
123 * name, this method replaces the old value with the specified value.
124 *
125 * @param name name in the name/value pair
126 * @param value value in the name/value pair
127 * @return this object builder
128 * @throws NullPointerException if the specified name or value is null
129 */
130 JsonObjectBuilder add(String name, JsonValue value);
131
132 /**
133 * Adds a name/{@code JsonString} pair to the JSON object associated with
134 * this object builder. If the object contains a mapping for the specified
135 * name, this method replaces the old value with the specified value.
136 *
137 * @param name name in the name/value pair
138 * @param value value in the name/value pair
139 * @return this object builder
140 * @throws NullPointerException if the specified name or value is null
141 */
142 JsonObjectBuilder add(String name, String value);
143
144 /**
145 * Adds a name/{@code JsonNumber} pair to the JSON object associated with
146 * this object builder. If the object contains a mapping for the specified
147 * name, this method replaces the old value with the specified value.
148 *
149 * @param name name in the name/value pair
150 * @param value value in the name/value pair
151 * @return this object builder
152 * @throws NullPointerException if the specified name or value is null
153 *
154 * @see JsonNumber
155 */
156 JsonObjectBuilder add(String name, BigInteger value);
157
158 /**
159 * Adds a name/{@code JsonNumber} pair to the JSON object associated with
160 * this object builder. If the object contains a mapping for the specified
161 * name, this method replaces the old value with the specified value.
162 *
163 * @param name name in the name/value pair
164 * @param value value in the name/value pair
165 * @return this object builder
166 * @throws NullPointerException if the specified name or value is null
167 *
168 * @see JsonNumber
169 */
170 JsonObjectBuilder add(String name, BigDecimal value);
171
172 /**
173 * Adds a name/{@code JsonNumber} pair to the JSON object associated with
174 * this object builder. If the object contains a mapping for the specified
175 * name, this method replaces the old value with the specified value.
176 *
177 * @param name name in the name/value pair
178 * @param value value in the name/value pair
179 * @return this object builder
180 * @throws NullPointerException if the specified name is null
181 *
182 * @see JsonNumber
183 */
184 JsonObjectBuilder add(String name, int value);
185
186 /**
187 * Adds a name/{@code JsonNumber} pair to the JSON object associated with
188 * this object builder. If the object contains a mapping for the specified
189 * name, this method replaces the old value with the specified value.
190 *
191 * @param name name in the name/value pair
192 * @param value value in the name/value pair
193 * @return this object builder
194 * @throws NullPointerException if the specified name is null
195 *
196 * @see JsonNumber
197 */
198 JsonObjectBuilder add(String name, long value);
199
200 /**
201 * Adds a name/{@code JsonNumber} pair to the JSON object associated with
202 * this object builder. If the object contains a mapping for the specified
203 * name, this method replaces the old value with the specified value.
204 *
205 * @param name name in the name/value pair
206 * @param value value in the name/value pair
207 * @return this object builder
208 * @throws NumberFormatException if the value is Not-a-Number (NaN) or
209 * infinity
210 * @throws NullPointerException if the specified name is null
211 *
212 * @see JsonNumber
213 */
214 JsonObjectBuilder add(String name, double value);
215
216 /**
217 * Adds a name/{@code JsonValue#TRUE} or name/{@code JsonValue#FALSE} pair
218 * to the JSON object associated with this object builder. If the object
219 * contains a mapping for the specified name, this method replaces the old
220 * value with the specified value.
221 *
222 * @param name name in the name/value pair
223 * @param value value in the name/value pair
224 * @return this object builder
225 * @throws NullPointerException if the specified name is null
226 */
227 JsonObjectBuilder add(String name, boolean value);
228
229 /**
230 * Adds a name/{@code JsonValue#NULL} pair to the JSON object associated
231 * with this object builder where the value is {@code null}.
232 * If the object contains a mapping for the specified name, this method
233 * replaces the old value with {@code null}.
234 *
235 * @param name name in the name/value pair
236 * @return this object builder
237 * @throws NullPointerException if the specified name is null
238 */
239 JsonObjectBuilder addNull(String name);
240
241 /**
242 * Adds a name/{@code JsonObject} pair to the JSON object associated
243 * with this object builder. The value {@code JsonObject} is built from the
244 * specified object builder. If the object contains a mapping for the
245 * specified name, this method replaces the old value with the
246 * {@code JsonObject} from the specified object builder.
247 *
248 * @param name name in the name/value pair
249 * @param builder the value is the object associated with this builder
250 * @return this object builder
251 * @throws NullPointerException if the specified name or builder is null
252 */
253 JsonObjectBuilder add(String name, JsonObjectBuilder builder);
254
255 /**
256 * Adds a name/{@code JsonArray} pair to the JSON object associated with
257 * this object builder. The value {@code JsonArray} is built from the
258 * specified array builder. If the object contains a mapping for the
259 * specified name, this method replaces the old value with the
260 * {@code JsonArray} from the specified array builder.
261 *
262 * @param name the name in the name/value pair
263 * @param builder the value is the object array with this builder
264 * @return this object builder
265 * @throws NullPointerException if the specified name or builder is null
266 */
267 JsonObjectBuilder add(String name, JsonArrayBuilder builder);
268
269 /**
270 * Adds all name/value pairs in the JSON object associated with the specified
271 * object builder to the JSON object associated with this object builder.
272 * The newly added name/value pair will replace any existing name/value pair with
273 * the same name.
274 *
275 * @param builder the specified object builder
276 * @return this object builder
277 * @throws NullPointerException if the specified builder is null
278 * @since 1.1
279 */
280 default JsonObjectBuilder addAll(JsonObjectBuilder builder) {
281 throw new UnsupportedOperationException();
282 }
283
284 /**
285 * Remove the name/value pair from the JSON object associated with this
286 * object builder if it is present.
287 *
288 * @param name the name in the name/value pair to be removed
289 * @return this object builder
290 * @throws NullPointerException if the specified name is null
291 * @since 1.1
292 */
293 default JsonObjectBuilder remove(String name) {
294 throw new UnsupportedOperationException();
295 }
296
297 /**
298 * Returns the JSON object associated with this object builder.
299 * The iteration order for the {@code JsonObject} is based
300 * on the order in which name/value pairs are added to the object using
301 * this builder.
302 *
303 * @return JSON object that is being built
304 */
305 JsonObject build();
306
307}
Note: See TracBrowser for help on using the repository browser.