001/* 002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 003 * 004 * Copyright (c) 2013-2018 Oracle and/or its affiliates. All rights reserved. 005 * 006 * The contents of this file are subject to the terms of either the GNU 007 * General Public License Version 2 only ("GPL") or the Common Development 008 * and Distribution License("CDDL") (collectively, the "License"). You 009 * may not use this file except in compliance with the License. You can 010 * obtain a copy of the License at 011 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 012 * or LICENSE.txt. See the License for the specific 013 * language governing permissions and limitations under the License. 014 * 015 * When distributing the software, include this License Header Notice in each 016 * file and include the License file at LICENSE.txt. 017 * 018 * GPL Classpath Exception: 019 * Oracle designates this particular file as subject to the "Classpath" 020 * exception as provided by Oracle in the GPL Version 2 section of the License 021 * file that accompanied this code. 022 * 023 * Modifications: 024 * If applicable, add the following below the License Header, with the fields 025 * enclosed by brackets [] replaced by your own identifying information: 026 * "Portions Copyright [year] [name of copyright owner]" 027 * 028 * Contributor(s): 029 * If you wish your version of this file to be governed by only the CDDL or 030 * only the GPL Version 2, indicate your decision by adding "[Contributor] 031 * elects to include this software in this distribution under the [CDDL or GPL 032 * Version 2] license." If you don't indicate a single choice of license, a 033 * recipient has the option to distribute your version of this file under 034 * either the CDDL, the GPL Version 2 or to extend the choice of license to 035 * its licensees as provided above. However, if you add GPL Version 2 code 036 * and therefore, elected the GPL Version 2 license, then the option applies 037 * only if the new code is made subject to such option by the copyright 038 * holder. 039 */ 040 041package javax.json; 042 043import java.math.BigDecimal; 044import java.math.BigInteger; 045 046/** 047 * A builder for creating {@link JsonObject} models from scratch. This 048 * interface initializes an empty JSON object model and provides methods to add 049 * name/value pairs to the object model and to return the resulting object. 050 * The methods in this class can be chained to add multiple name/value pairs 051 * to the object. 052 * 053 * <EMBED CLASS='external-html' DATA-FILE-ID=LICENSE DATA-CIETName=JsonObjectBuilder> 054 * 055 * <BR /><BR />The class {@link javax.json.Json} contains methods to create the builder 056 * object. The example code below shows how to build an empty {@code JsonObject} 057 * instance. 058 * 059 * <BR /><DIV CLASS=SNIP>{@code 060 * JsonObject object = Json.createObjectBuilder().build(); 061 * }</DIV> 062 * 063 * <BR /><BR />The class {@link JsonBuilderFactory} also contains methods to create 064 * {@code JsonObjectBuilder} instances. A factory instance can be used to create 065 * multiple builder instances with the same configuration. This the preferred 066 * way to create multiple instances. 067 * 068 * <BR /><BR />The example code below shows how to build a {@code JsonObject} model that 069 * represents the following JSON object: 070 * 071 * <DIV CLASS=JSON>{@code 072 * { 073 * "firstName": "John", "lastName": "Smith", "age": 25, 074 * "address" : { 075 * "streetAddress": "21 2nd Street", 076 * "city": "New York", 077 * "state": "NY", 078 * "postalCode": "10021" 079 * }, 080 * "phoneNumber": [ 081 * { "type": "home", "number": "212 555-1234" }, 082 * { "type": "fax", "number": "646 555-4567" } 083 * ] 084 * } 085 * }</DIV> 086 * 087 * <BR />The code to create the object shown above is the following: 088 * 089 * <DIV CLASS=EXAMPLE>{@code 090 * JsonBuilderFactory factory = Json.createBuilderFactory(config); 091 * 092 * JsonObject value = factory 093 * .createObjectBuilder() 094 * .add("firstName", "John") 095 * .add("lastName", "Smith") 096 * .add("age", 25) 097 * .add("address", factory.createObjectBuilder() 098 * .add("streetAddress", "21 2nd Street") 099 * .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 * }</DIV> 111 * 112 * <BR /><BR />This class does <em>not</em> allow <code>null</code> to be used as a name or 113 * value while building the JSON object 114 * 115 * @see JsonArrayBuilder 116 */ 117public interface JsonObjectBuilder { 118 119 /** 120 * Adds a name/{@code JsonValue} pair to the JSON object associated with 121 * this object builder. If the object contains a mapping for the specified 122 * name, this method replaces the old value with the specified value. 123 * 124 * @param name name in the name/value pair 125 * @param value value in the name/value pair 126 * @return this object builder 127 * @throws NullPointerException if the specified name or value is null 128 */ 129 JsonObjectBuilder add(String name, JsonValue value); 130 131 /** 132 * Adds a name/{@code JsonString} pair to the JSON object associated with 133 * this object builder. If the object contains a mapping for the specified 134 * name, this method replaces the old value with the specified value. 135 * 136 * @param name name in the name/value pair 137 * @param value value in the name/value pair 138 * @return this object builder 139 * @throws NullPointerException if the specified name or value is null 140 */ 141 JsonObjectBuilder add(String name, String value); 142 143 /** 144 * Adds a name/{@code JsonNumber} pair to the JSON object associated with 145 * this object builder. If the object contains a mapping for the specified 146 * name, this method replaces the old value with the specified value. 147 * 148 * @param name name in the name/value pair 149 * @param value value in the name/value pair 150 * @return this object builder 151 * @throws NullPointerException if the specified name or value is null 152 * 153 * @see JsonNumber 154 */ 155 JsonObjectBuilder add(String name, BigInteger value); 156 157 /** 158 * Adds a name/{@code JsonNumber} pair to the JSON object associated with 159 * this object builder. If the object contains a mapping for the specified 160 * name, this method replaces the old value with the specified value. 161 * 162 * @param name name in the name/value pair 163 * @param value value in the name/value pair 164 * @return this object builder 165 * @throws NullPointerException if the specified name or value is null 166 * 167 * @see JsonNumber 168 */ 169 JsonObjectBuilder add(String name, BigDecimal value); 170 171 /** 172 * Adds a name/{@code JsonNumber} pair to the JSON object associated with 173 * this object builder. If the object contains a mapping for the specified 174 * name, this method replaces the old value with the specified value. 175 * 176 * @param name name in the name/value pair 177 * @param value value in the name/value pair 178 * @return this object builder 179 * @throws NullPointerException if the specified name is null 180 * 181 * @see JsonNumber 182 */ 183 JsonObjectBuilder add(String name, int value); 184 185 /** 186 * Adds a name/{@code JsonNumber} pair to the JSON object associated with 187 * this object builder. If the object contains a mapping for the specified 188 * name, this method replaces the old value with the specified value. 189 * 190 * @param name name in the name/value pair 191 * @param value value in the name/value pair 192 * @return this object builder 193 * @throws NullPointerException if the specified name is null 194 * 195 * @see JsonNumber 196 */ 197 JsonObjectBuilder add(String name, long value); 198 199 /** 200 * Adds a name/{@code JsonNumber} pair to the JSON object associated with 201 * this object builder. If the object contains a mapping for the specified 202 * name, this method replaces the old value with the specified value. 203 * 204 * @param name name in the name/value pair 205 * @param value value in the name/value pair 206 * @return this object builder 207 * @throws NumberFormatException if the value is Not-a-Number (NaN) or 208 * infinity 209 * @throws NullPointerException if the specified name is null 210 * 211 * @see JsonNumber 212 */ 213 JsonObjectBuilder add(String name, double value); 214 215 /** 216 * Adds a name/{@code JsonValue#TRUE} or name/{@code JsonValue#FALSE} pair 217 * to the JSON object associated with this object builder. If the object 218 * contains a mapping for the specified name, this method replaces the old 219 * value with the specified value. 220 * 221 * @param name name in the name/value pair 222 * @param value value in the name/value pair 223 * @return this object builder 224 * @throws NullPointerException if the specified name is null 225 */ 226 JsonObjectBuilder add(String name, boolean value); 227 228 /** 229 * Adds a name/{@code JsonValue#NULL} pair to the JSON object associated 230 * with this object builder where the value is {@code null}. 231 * If the object contains a mapping for the specified name, this method 232 * replaces the old value with {@code null}. 233 * 234 * @param name name in the name/value pair 235 * @return this object builder 236 * @throws NullPointerException if the specified name is null 237 */ 238 JsonObjectBuilder addNull(String name); 239 240 /** 241 * Adds a name/{@code JsonObject} pair to the JSON object associated 242 * with this object builder. The value {@code JsonObject} is built from the 243 * specified object builder. If the object contains a mapping for the 244 * specified name, this method replaces the old value with the 245 * {@code JsonObject} from the specified object builder. 246 * 247 * @param name name in the name/value pair 248 * @param builder the value is the object associated with this builder 249 * @return this object builder 250 * @throws NullPointerException if the specified name or builder is null 251 */ 252 JsonObjectBuilder add(String name, JsonObjectBuilder builder); 253 254 /** 255 * Adds a name/{@code JsonArray} pair to the JSON object associated with 256 * this object builder. The value {@code JsonArray} is built from the 257 * specified array builder. If the object contains a mapping for the 258 * specified name, this method replaces the old value with the 259 * {@code JsonArray} from the specified array builder. 260 * 261 * @param name the name in the name/value pair 262 * @param builder the value is the object array with this builder 263 * @return this object builder 264 * @throws NullPointerException if the specified name or builder is null 265 */ 266 JsonObjectBuilder add(String name, JsonArrayBuilder builder); 267 268 /** 269 * Adds all name/value pairs in the JSON object associated with the specified 270 * object builder to the JSON object associated with this object builder. 271 * The newly added name/value pair will replace any existing name/value pair with 272 * the same name. 273 * 274 * @param builder the specified object builder 275 * @return this object builder 276 * @throws NullPointerException if the specified builder is null 277 * @since 1.1 278 */ 279 default JsonObjectBuilder addAll(JsonObjectBuilder builder) { 280 throw new UnsupportedOperationException(); 281 } 282 283 /** 284 * Remove the name/value pair from the JSON object associated with this 285 * object builder if it is present. 286 * 287 * @param name the name in the name/value pair to be removed 288 * @return this object builder 289 * @throws NullPointerException if the specified name is null 290 * @since 1.1 291 */ 292 default JsonObjectBuilder remove(String name) { 293 throw new UnsupportedOperationException(); 294 } 295 296 /** 297 * Returns the JSON object associated with this object builder. 298 * The iteration order for the {@code JsonObject} is based 299 * on the order in which name/value pairs are added to the object using 300 * this builder. 301 * 302 * @return JSON object that is being built 303 */ 304 JsonObject build(); 305 306}