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