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

Last change on this file since 9978 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) 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.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 * <a id="JsonObjectBuilderExample1"/>
68 * The example code below shows how to build a {@code JsonObject} model that
69 * represents the following JSON object:
70 *
71 * <pre>
72 * <code>
73 * {
74 * "firstName": "John", "lastName": "Smith", "age": 25,
75 * "address" : {
76 * "streetAddress": "21 2nd Street",
77 * "city": "New York",
78 * "state": "NY",
79 * "postalCode": "10021"
80 * },
81 * "phoneNumber": [
82 * { "type": "home", "number": "212 555-1234" },
83 * { "type": "fax", "number": "646 555-4567" }
84 * ]
85 * }
86 * </code>
87 * </pre>
88 *
89 * <p>The code to create the object shown above is the following:
90 *
91 * <pre>
92 * <code>
93 * JsonBuilderFactory factory = Json.createBuilderFactory(config);
94 * JsonObject value = factory.createObjectBuilder()
95 * .add("firstName", "John")
96 * .add("lastName", "Smith")
97 * .add("age", 25)
98 * .add("address", factory.createObjectBuilder()
99 * .add("streetAddress", "21 2nd Street")
100 * .add("city", "New York")
101 * .add("state", "NY")
102 * .add("postalCode", "10021"))
103 * .add("phoneNumber", factory.createArrayBuilder()
104 * .add(factory.createObjectBuilder()
105 * .add("type", "home")
106 * .add("number", "212 555-1234"))
107 * .add(factory.createObjectBuilder()
108 * .add("type", "fax")
109 * .add("number", "646 555-4567")))
110 * .build();
111 * </code>
112 * </pre>
113 *
114 * <p>This class does <em>not</em> allow <tt>null</tt> to be used as a name or
115 * value while building the JSON object
116 *
117 * @see JsonArrayBuilder
118 */
119public interface JsonObjectBuilder {
120
121 /**
122 * Adds a name/{@code JsonValue} pair to the JSON object associated with
123 * this object builder. If the object contains a mapping for the specified
124 * name, this method replaces the old value with the specified value.
125 *
126 * @param name name in the name/value pair
127 * @param value value in the name/value pair
128 * @return this object builder
129 * @throws NullPointerException if the specified name or value is null
130 */
131 JsonObjectBuilder add(String name, JsonValue value);
132
133 /**
134 * Adds a name/{@code JsonString} pair to the JSON object associated with
135 * this object builder. If the object contains a mapping for the specified
136 * name, this method replaces the old value with the specified value.
137 *
138 * @param name name in the name/value pair
139 * @param value value in the name/value pair
140 * @return this object builder
141 * @throws NullPointerException if the specified name or value is null
142 */
143 JsonObjectBuilder add(String name, String value);
144
145 /**
146 * Adds a name/{@code JsonNumber} pair to the JSON object associated with
147 * this object builder. If the object contains a mapping for the specified
148 * name, this method replaces the old value with the specified value.
149 *
150 * @param name name in the name/value pair
151 * @param value value in the name/value pair
152 * @return this object builder
153 * @throws NullPointerException if the specified name or value is null
154 *
155 * @see JsonNumber
156 */
157 JsonObjectBuilder add(String name, BigInteger value);
158
159 /**
160 * Adds a name/{@code JsonNumber} pair to the JSON object associated with
161 * this object builder. If the object contains a mapping for the specified
162 * name, this method replaces the old value with the specified value.
163 *
164 * @param name name in the name/value pair
165 * @param value value in the name/value pair
166 * @return this object builder
167 * @throws NullPointerException if the specified name or value is null
168 *
169 * @see JsonNumber
170 */
171 JsonObjectBuilder add(String name, BigDecimal value);
172
173 /**
174 * Adds a name/{@code JsonNumber} pair to the JSON object associated with
175 * this object builder. If the object contains a mapping for the specified
176 * name, this method replaces the old value with the specified value.
177 *
178 * @param name name in the name/value pair
179 * @param value value in the name/value pair
180 * @return this object builder
181 * @throws NullPointerException if the specified name is null
182 *
183 * @see JsonNumber
184 */
185 JsonObjectBuilder add(String name, int value);
186
187 /**
188 * Adds a name/{@code JsonNumber} pair to the JSON object associated with
189 * this object builder. If the object contains a mapping for the specified
190 * name, this method replaces the old value with the specified value.
191 *
192 * @param name name in the name/value pair
193 * @param value value in the name/value pair
194 * @return this object builder
195 * @throws NullPointerException if the specified name is null
196 *
197 * @see JsonNumber
198 */
199 JsonObjectBuilder add(String name, long value);
200
201 /**
202 * Adds a name/{@code JsonNumber} pair to the JSON object associated with
203 * this object builder. If the object contains a mapping for the specified
204 * name, this method replaces the old value with the specified value.
205 *
206 * @param name name in the name/value pair
207 * @param value value in the name/value pair
208 * @return this object builder
209 * @throws NumberFormatException if the value is Not-a-Number(NaN) or
210 * infinity
211 * @throws NullPointerException if the specified name is null
212 *
213 * @see JsonNumber
214 */
215 JsonObjectBuilder add(String name, double value);
216
217 /**
218 * Adds a name/{@code JsonValue#TRUE} or name/{@code JsonValue#FALSE} pair
219 * to the JSON object associated with this object builder. If the object
220 * contains a mapping for the specified name, this method replaces the old
221 * value with the specified value.
222 *
223 * @param name name in the name/value pair
224 * @param value value in the name/value pair
225 * @return this object builder
226 * @throws NullPointerException if the specified name is null
227 */
228 JsonObjectBuilder add(String name, boolean value);
229
230 /**
231 * Adds a name/{@code JsonValue#NULL} pair to the JSON object associated
232 * with this object builder where the value is {@code null}.
233 * If the object contains a mapping for the specified name, this method
234 * replaces the old value with {@code null}.
235 *
236 * @param name name in the name/value pair
237 * @return this object builder
238 * @throws NullPointerException if the specified name is null
239 */
240 JsonObjectBuilder addNull(String name);
241
242 /**
243 * Adds a name/{@code JsonObject} pair to the JSON object associated
244 * with this object builder. The value {@code JsonObject} is built from the
245 * specified object builder. If the object contains a mapping for the
246 * specified name, this method replaces the old value with the
247 * {@code JsonObject} from the specified object builder.
248 *
249 * @param name name in the name/value pair
250 * @param builder the value is the object associated with this builder
251 * @return this object builder
252 * @throws NullPointerException if the specified name or builder is null
253 */
254 JsonObjectBuilder add(String name, JsonObjectBuilder builder);
255
256 /**
257 * Adds a name/{@code JsonArray} pair to the JSON object associated with
258 * this object builder. The value {@code JsonArray} is built from the
259 * specified array builder. If the object contains a mapping for the
260 * specified name, this method replaces the old value with the
261 * {@code JsonArray} from the specified array builder.
262 *
263 * @param name the name in the name/value pair
264 * @param builder the value is the object array with this builder
265 * @return this object builder
266 * @throws NullPointerException if the specified name or builder is null
267 */
268 JsonObjectBuilder add(String name, JsonArrayBuilder builder);
269
270 /**
271 * Returns the JSON object associated with this object builder.
272 * The iteration order for the {@code JsonObject} is based
273 * on the order in which name/value pairs are added to the object using
274 * this builder.
275 *
276 * @return JSON object that is being built
277 */
278 JsonObject build();
279
280}
Note: See TracBrowser for help on using the repository browser.