| 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.stream; |
| 18 | |
| 19 | import jakarta.json.JsonValue; |
| 20 | import java.io.Closeable; |
| 21 | import java.io.Flushable; |
| 22 | import java.math.BigDecimal; |
| 23 | import java.math.BigInteger; |
| 24 | |
| 25 | /** |
| 26 | * Writes JSON data to an output source in a streaming way. The class |
| 27 | * {@link jakarta.json.Json} contains methods to create generators for character |
| 28 | * or output streams ({@link java.io.Writer} and {@link java.io.OutputStream}). |
| 29 | * |
| 30 | * <p> |
| 31 | * The following example shows how to create a JSON generator: |
| 32 | * <pre> |
| 33 | * <code> |
| 34 | * JsonGenerator generator = Json.createGenerator(...); |
| 35 | * </code> |
| 36 | * </pre> |
| 37 | * |
| 38 | * <p> |
| 39 | * The class {@link JsonGeneratorFactory} also contains methods to create |
| 40 | * {@code JsonGenerator} instances. {@link JsonGeneratorFactory} should be used |
| 41 | * when creating multiple generator instances, as in the following example: |
| 42 | * <pre> |
| 43 | * <code> |
| 44 | * JsonGeneratorFactory factory = Json.createGeneratorFactory(); |
| 45 | * JsonGenerator generator1 = factory.createGenerator(...); |
| 46 | * JsonGenerator generator2 = factory.createGenerator(...); |
| 47 | * </code> |
| 48 | * </pre> |
| 49 | * |
| 50 | * <p> |
| 51 | * JSON objects can be created using {@code JsonGenerator} by calling the |
| 52 | * {@link #writeStartObject()} method and then adding name/value pairs with the |
| 53 | * {@code write} method. |
| 54 | * <p> |
| 55 | * The following example shows how to generate an empty JSON object: |
| 56 | * <pre> |
| 57 | * <code> |
| 58 | * JsonGenerator generator = ...; |
| 59 | * generator.writeStartObject().writeEnd().close(); |
| 60 | * </code> |
| 61 | * </pre> |
| 62 | * |
| 63 | * JSON arrays can be created using {@code JsonGenerator} by calling the |
| 64 | * {@link #writeStartArray()} method and then adding values with the |
| 65 | * {@code write} method. |
| 66 | * |
| 67 | * <p> |
| 68 | * The following example shows how to generate an empty JSON array: |
| 69 | * <pre> |
| 70 | * <code> |
| 71 | * JsonGenerator generator = ...; |
| 72 | * generator.writeStartArray().writeEnd().close(); |
| 73 | * </code> |
| 74 | * </pre> |
| 75 | * |
| 76 | * <p> |
| 77 | * Other JSON values (that are not JSON objects or arrays) can be created |
| 78 | * by calling the appropiate {@code write} methods. |
| 79 | * <p> |
| 80 | * The following example shows how to generate a JSON string: |
| 81 | * <pre><code> |
| 82 | * JsonGenerator generator = ...; |
| 83 | * generator.write("message").close(); |
| 84 | * </code></pre> |
| 85 | * |
| 86 | * {@code JsonGenerator} methods can be chained as in the following example: |
| 87 | * <pre> |
| 88 | * <code> |
| 89 | * generator |
| 90 | * .writeStartObject() |
| 91 | * .write("firstName", "John") |
| 92 | * .write("lastName", "Smith") |
| 93 | * .write("age", 25) |
| 94 | * .writeStartObject("address") |
| 95 | * .write("streetAddress", "21 2nd Street") |
| 96 | * .write("city", "New York") |
| 97 | * .write("state", "NY") |
| 98 | * .write("postalCode", "10021") |
| 99 | * .writeEnd() |
| 100 | * .writeStartArray("phoneNumber") |
| 101 | * .writeStartObject() |
| 102 | * .write("type", "home") |
| 103 | * .write("number", "212 555-1234") |
| 104 | * .writeEnd() |
| 105 | * .writeStartObject() |
| 106 | * .write("type", "fax") |
| 107 | * .write("number", "646 555-4567") |
| 108 | * .writeEnd() |
| 109 | * .writeEnd() |
| 110 | * .writeEnd(); |
| 111 | * generator.close(); |
| 112 | * </code> |
| 113 | * </pre> |
| 114 | * |
| 115 | * The example code above generates the following JSON (or equivalent): |
| 116 | * <pre> |
| 117 | * <code> |
| 118 | * { |
| 119 | * "firstName": "John", "lastName": "Smith", "age": 25, |
| 120 | * "address" : { |
| 121 | * "streetAddress": "21 2nd Street", |
| 122 | * "city": "New York", |
| 123 | * "state": "NY", |
| 124 | * "postalCode": "10021" |
| 125 | * }, |
| 126 | * "phoneNumber": [ |
| 127 | * {"type": "home", "number": "212 555-1234"}, |
| 128 | * {"type": "fax", "number": "646 555-4567"} |
| 129 | * ] |
| 130 | * } |
| 131 | * </code> |
| 132 | * </pre> |
| 133 | * |
| 134 | * The generated JSON text must strictly conform to the grammar defined in |
| 135 | * <a href="http://www.ietf.org/rfc/rfc7159.txt">RFC 7159</a>. |
| 136 | * |
| 137 | * @see jakarta.json.Json |
| 138 | * @see JsonGeneratorFactory |
| 139 | */ |
| 140 | public interface JsonGenerator extends Flushable, /*Auto*/Closeable { |
| 141 | /** |
| 142 | * Configuration property to generate JSON prettily. All providers |
| 143 | * must support this property. The value of the property could be |
| 144 | * be anything. |
| 145 | */ |
| 146 | String PRETTY_PRINTING = "jakarta.json.stream.JsonGenerator.prettyPrinting" ; |
| 147 | |
| 148 | /** |
| 149 | * Writes the JSON start object character. It starts a new child object |
| 150 | * context within which JSON name/value pairs can be written to the object. |
| 151 | * This method is valid only in an array context, field context or in no context (when a |
| 152 | * context is not yet started). This method can only be called once in |
| 153 | * no context. |
| 154 | * |
| 155 | * @return this generator |
| 156 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 157 | * would be cause of JsonException) |
| 158 | * @throws JsonGenerationException if this method is called within an |
| 159 | * object context or if it is called more than once in no context. |
| 160 | */ |
| 161 | JsonGenerator writeStartObject(); |
| 162 | |
| 163 | /** |
| 164 | * Writes the JSON name/start object character pair in the current |
| 165 | * object context. It starts a new child object context within which JSON |
| 166 | * name/value pairs can be written to the object. |
| 167 | * |
| 168 | * @param name a name within the JSON name/object pair to be written |
| 169 | * @return this generator |
| 170 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 171 | * would be cause of JsonException) |
| 172 | * @throws JsonGenerationException if this method is not called within an |
| 173 | * object context |
| 174 | */ |
| 175 | JsonGenerator writeStartObject(String name); |
| 176 | |
| 177 | /** |
| 178 | * Writes the JSON name with a colon. It starts a field context, in which valid |
| 179 | * options are writing a value, starting an object or an array. |
| 180 | * |
| 181 | * Writing value closes field context, if object or array is started after field name, |
| 182 | * field context will be closed after object/array close. |
| 183 | * |
| 184 | * @param name name of json field |
| 185 | * @return this generator |
| 186 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 187 | * would be cause of JsonException) |
| 188 | * @throws JsonGenerationException if this method is not called within an |
| 189 | * object context |
| 190 | * |
| 191 | * @since 1.1 |
| 192 | */ |
| 193 | JsonGenerator writeKey(String name); |
| 194 | |
| 195 | /** |
| 196 | * Writes the JSON start array character. It starts a new child array |
| 197 | * context within which JSON values can be written to the array. This |
| 198 | * method is valid only in an array context, field context or in no context (when a |
| 199 | * context is not yet started). This method can only be called once in |
| 200 | * no context. |
| 201 | * |
| 202 | * @return this generator |
| 203 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 204 | * would be cause of JsonException) |
| 205 | * @throws JsonGenerationException if this method is called within an |
| 206 | * object context or if called more than once in no context |
| 207 | */ |
| 208 | JsonGenerator writeStartArray(); |
| 209 | |
| 210 | /** |
| 211 | * Writes the JSON name/start array character pair with in the current |
| 212 | * object context. It starts a new child array context within which JSON |
| 213 | * values can be written to the array. |
| 214 | * |
| 215 | * @param name a name within the JSON name/array pair to be written |
| 216 | * @return this generator |
| 217 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 218 | * would be cause of JsonException) |
| 219 | * @throws JsonGenerationException if this method is not called within |
| 220 | * an object context |
| 221 | */ |
| 222 | JsonGenerator writeStartArray(String name); |
| 223 | |
| 224 | /** |
| 225 | * Writes a JSON name/value pair in the current object context. |
| 226 | * |
| 227 | * @param name a name in the JSON name/value pair to be written in |
| 228 | * current JSON object |
| 229 | * @param value a value in the JSON name/value pair to be written in |
| 230 | * current JSON object |
| 231 | * @return this generator |
| 232 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 233 | * would be cause of JsonException) |
| 234 | * @throws JsonGenerationException if this method is not called within an |
| 235 | * object context |
| 236 | */ |
| 237 | JsonGenerator write(String name, JsonValue value); |
| 238 | |
| 239 | /** |
| 240 | * Writes a JSON name/string value pair in the current object context. |
| 241 | * The specified value is written as JSON string value. |
| 242 | * |
| 243 | * @param name a name in the JSON name/string pair to be written in |
| 244 | * current JSON object |
| 245 | * @param value a value in the JSON name/string pair to be written in |
| 246 | * current JSON object |
| 247 | * @return this generator |
| 248 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 249 | * would be cause of JsonException) |
| 250 | * @throws JsonGenerationException if this method is not called within an |
| 251 | * object context |
| 252 | */ |
| 253 | JsonGenerator write(String name, String value); |
| 254 | |
| 255 | /** |
| 256 | * Writes a JSON name/number value pair in the current object context. |
| 257 | * The specified value is written as a JSON number value. The string |
| 258 | * {@code new BigDecimal(value).toString()} |
| 259 | * is used as the text value for writing. |
| 260 | * |
| 261 | * @param name a name in the JSON name/number pair to be written in |
| 262 | * current JSON object |
| 263 | * @param value a value in the JSON name/number pair to be written in |
| 264 | * current JSON object |
| 265 | * @return this generator |
| 266 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 267 | * would be cause of JsonException) |
| 268 | * @throws JsonGenerationException if this method is not called within an |
| 269 | * object context. |
| 270 | */ |
| 271 | JsonGenerator write(String name, BigInteger value); |
| 272 | |
| 273 | /** |
| 274 | * Writes a JSON name/number value pair in the current object context. |
| 275 | * The specified value is written as a JSON number value. The specified |
| 276 | * value's {@code toString()} is used as the text value for writing. |
| 277 | * |
| 278 | * @param name a name in the JSON name/number pair to be written in |
| 279 | * current JSON object |
| 280 | * @param value a value in the JSON name/number pair to be written in |
| 281 | * current JSON object |
| 282 | * @return this generator |
| 283 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 284 | * would be cause of JsonException) |
| 285 | * @throws JsonGenerationException if this method is not called within an |
| 286 | * object context. |
| 287 | */ |
| 288 | JsonGenerator write(String name, BigDecimal value); |
| 289 | |
| 290 | /** |
| 291 | * Writes a JSON name/number value pair in the current object context. |
| 292 | * The specified value is written as a JSON number value. The string |
| 293 | * {@code new BigDecimal(value).toString()} is used as the text value |
| 294 | * for writing. |
| 295 | * |
| 296 | * @param name a name in the JSON name/number pair to be written in |
| 297 | * current JSON object |
| 298 | * @param value a value in the JSON name/number pair to be written in |
| 299 | * current JSON object |
| 300 | * @return this generator |
| 301 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 302 | * would be cause of JsonException) |
| 303 | * @throws JsonGenerationException if this method is not called within an |
| 304 | * object context. |
| 305 | */ |
| 306 | JsonGenerator write(String name, int value); |
| 307 | |
| 308 | /** |
| 309 | * Writes a JSON name/number value pair in the current object context. |
| 310 | * The specified value is written as a JSON number value. The string |
| 311 | * {@code new BigDecimal(value).toString()} is used as the text |
| 312 | * value for writing. |
| 313 | * |
| 314 | * @param name a name in the JSON name/number pair to be written in |
| 315 | * current JSON object |
| 316 | * @param value a value in the JSON name/number pair to be written in |
| 317 | * current JSON object |
| 318 | * @return this generator |
| 319 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 320 | * would be cause of JsonException) |
| 321 | * @throws JsonGenerationException if this method is not called within an |
| 322 | * object context. |
| 323 | */ |
| 324 | JsonGenerator write(String name, long value); |
| 325 | |
| 326 | /** |
| 327 | * Writes a JSON name/number value pair in the current object context. |
| 328 | * The specified value is written as a JSON number value. The string |
| 329 | * {@code BigDecimal.valueOf(double).toString()} |
| 330 | * is used as the text value for writing. |
| 331 | * |
| 332 | * @param name a name in the JSON name/number pair to be written in |
| 333 | * current JSON object |
| 334 | * @param value a value in the JSON name/number pair to be written in |
| 335 | * current JSON object |
| 336 | * @return this generator |
| 337 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 338 | * would be cause of JsonException) |
| 339 | * @throws NumberFormatException if the value is Not-a-Number (NaN) or infinity. |
| 340 | * @throws JsonGenerationException if this method is not called within an |
| 341 | * object context |
| 342 | */ |
| 343 | JsonGenerator write(String name, double value); |
| 344 | |
| 345 | /** |
| 346 | * Writes a JSON name/boolean value pair in the current object context. |
| 347 | * If value is true, it writes the JSON {@code true} value, otherwise |
| 348 | * it writes the JSON {@code false} value. |
| 349 | * |
| 350 | * @param name a name in the JSON name/boolean pair to be written in |
| 351 | * current JSON object |
| 352 | * @param value a value in the JSON name/boolean pair to be written in |
| 353 | * current JSON object |
| 354 | * @return this generator |
| 355 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 356 | * would be cause of JsonException) |
| 357 | * @throws JsonGenerationException if this method is not called within an |
| 358 | * object context. |
| 359 | */ |
| 360 | JsonGenerator write(String name, boolean value); |
| 361 | |
| 362 | |
| 363 | /** |
| 364 | * Writes a JSON name/null value pair in an current object context. |
| 365 | * |
| 366 | * @param name a name in the JSON name/null pair to be written in |
| 367 | * current JSON object |
| 368 | * @return this generator |
| 369 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 370 | * would be cause of JsonException) |
| 371 | * @throws JsonGenerationException if this method is not called within an |
| 372 | * object context |
| 373 | */ |
| 374 | JsonGenerator writeNull(String name); |
| 375 | |
| 376 | /** |
| 377 | * Writes the end of the current context. If the current context is |
| 378 | * an array context, this method writes the end-of-array character (']'). |
| 379 | * If the current context is an object context, this method writes the |
| 380 | * end-of-object character ('}'). After writing the end of the current |
| 381 | * context, the parent context becomes the new current context. |
| 382 | * If parent context is field context, it is closed. |
| 383 | * |
| 384 | * @return this generator |
| 385 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 386 | * would be cause of JsonException) |
| 387 | * @throws JsonGenerationException if this method is called in no context. |
| 388 | */ |
| 389 | JsonGenerator writeEnd(); |
| 390 | |
| 391 | /** |
| 392 | * Writes the specified value as a JSON value within |
| 393 | * the current array, field or root context. |
| 394 | * |
| 395 | * @param value a value to be written in current JSON array |
| 396 | * @return this generator |
| 397 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 398 | * would be cause of JsonException) |
| 399 | * @throws JsonGenerationException if this method is not called within an |
| 400 | * array, root or field context. |
| 401 | */ |
| 402 | JsonGenerator write(JsonValue value); |
| 403 | |
| 404 | /** |
| 405 | * Writes the specified value as a JSON string value within |
| 406 | * the current array, field or root context. |
| 407 | * |
| 408 | * @param value a value to be written in current JSON array |
| 409 | * @return this generator |
| 410 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 411 | * would be cause of JsonException) |
| 412 | * @throws JsonGenerationException if this method is not called within an |
| 413 | * array, root or field context. |
| 414 | */ |
| 415 | JsonGenerator write(String value); |
| 416 | |
| 417 | /** |
| 418 | * Writes the specified value as a JSON number value within |
| 419 | * the current array, field or root context. The specified value's {@code toString()} |
| 420 | * is used as the the text value for writing. |
| 421 | * |
| 422 | * @param value a value to be written in current JSON array |
| 423 | * @return this generator |
| 424 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 425 | * would be cause of JsonException) |
| 426 | * @throws JsonGenerationException if this method is not called within an |
| 427 | * array, root or field context. |
| 428 | * |
| 429 | * @see jakarta.json.JsonNumber |
| 430 | */ |
| 431 | JsonGenerator write(BigDecimal value); |
| 432 | |
| 433 | /** |
| 434 | * Writes the specified value as a JSON number value within |
| 435 | * the current array, field or root context. The string {@code new BigDecimal(value).toString()} |
| 436 | * is used as the text value for writing. |
| 437 | * |
| 438 | * @param value a value to be written in current JSON array |
| 439 | * @return this generator. |
| 440 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 441 | * would be cause of JsonException) |
| 442 | * @throws JsonGenerationException if this method is not called within an |
| 443 | * array, root or field context. |
| 444 | * |
| 445 | * @see jakarta.json.JsonNumber |
| 446 | */ |
| 447 | JsonGenerator write(BigInteger value); |
| 448 | |
| 449 | /** |
| 450 | * Writes the specified value as a JSON number value within |
| 451 | * the current array, field or root context. The string {@code new BigDecimal(value).toString()} |
| 452 | * is used as the text value for writing. |
| 453 | * |
| 454 | * @param value a value to be written in current JSON array |
| 455 | * @return this generator |
| 456 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 457 | * would be cause of JsonException) |
| 458 | * @throws JsonGenerationException if this method is not called within an |
| 459 | * array, root or field context. |
| 460 | */ |
| 461 | JsonGenerator write(int value); |
| 462 | |
| 463 | /** |
| 464 | * Writes the specified value as a JSON number value within |
| 465 | * the current array, field or root context. The string {@code new BigDecimal(value).toString()} |
| 466 | * is used as the text value for writing. |
| 467 | * |
| 468 | * @param value a value to be written in current JSON array |
| 469 | * @return this generator |
| 470 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 471 | * would be cause of JsonException) |
| 472 | * @throws JsonGenerationException if this method is not called within an |
| 473 | * array, root or field context. |
| 474 | */ |
| 475 | JsonGenerator write(long value); |
| 476 | |
| 477 | /** |
| 478 | * Writes the specified value as a JSON number value within the current |
| 479 | * array, field or root context. The string {@code BigDecimal.valueOf(value).toString()} |
| 480 | * is used as the text value for writing. |
| 481 | * |
| 482 | * @param value a value to be written in current JSON array |
| 483 | * @return this generator |
| 484 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 485 | * would be cause of JsonException) |
| 486 | * @throws JsonGenerationException if this method is not called within an |
| 487 | * array, root or field context. |
| 488 | * @throws NumberFormatException if the value is Not-a-Number (NaN) or infinity. |
| 489 | */ |
| 490 | JsonGenerator write(double value); |
| 491 | |
| 492 | /** |
| 493 | * Writes a JSON true or false value within the current array, field or root context. |
| 494 | * If value is true, this method writes the JSON {@code true} value, |
| 495 | * otherwise it writes the JSON {@code false} value. |
| 496 | * |
| 497 | * @param value a {@code boolean} value |
| 498 | * @return this generator |
| 499 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 500 | * would be cause of JsonException) |
| 501 | * @throws JsonGenerationException if this method is not called within an |
| 502 | * array, root or field context. |
| 503 | */ |
| 504 | JsonGenerator write(boolean value); |
| 505 | |
| 506 | /** |
| 507 | * Writes a JSON null value within the current array, field or root context. |
| 508 | * |
| 509 | * @return this generator |
| 510 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 511 | * would be cause of JsonException) |
| 512 | * @throws JsonGenerationException if this method is not called within an |
| 513 | * array, root or field context. |
| 514 | */ |
| 515 | JsonGenerator writeNull(); |
| 516 | |
| 517 | /** |
| 518 | * Closes this generator and frees any resources associated with it. |
| 519 | * This method closes the underlying output source. |
| 520 | * |
| 521 | * The underlying stream is closed only if complete JSON object is written. In case |
| 522 | * of incomplete object JsonGenerationException is thrown and underlying stream is |
| 523 | * not closed. |
| 524 | * |
| 525 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 526 | * would be cause of JsonException) |
| 527 | * @throws JsonGenerationException if an incomplete JSON is generated |
| 528 | */ |
| 529 | @Override |
| 530 | void close(); |
| 531 | |
| 532 | /** |
| 533 | * Flushes the underlying output source. If the generator has saved |
| 534 | * any characters in a buffer, writes them immediately to the underlying |
| 535 | * output source before flushing it. |
| 536 | * |
| 537 | * @throws jakarta.json.JsonException if an i/o error occurs (IOException |
| 538 | * would be cause of JsonException) |
| 539 | */ |
| 540 | @Override |
| 541 | void flush(); |
| 542 | |
| 543 | } |