| 1 | /* |
| 2 | * Copyright (c) 2012, 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.io.Closeable; |
| 20 | |
| 21 | /** |
| 22 | * Writes a JSON {@link JsonObject object} or {@link JsonArray array} structure |
| 23 | * to an output source. |
| 24 | * |
| 25 | * <p>The class {@link jakarta.json.Json} contains methods to create writers from |
| 26 | * output sources ({@link java.io.OutputStream} and {@link java.io.Writer}). |
| 27 | * |
| 28 | * <p> |
| 29 | * The following example demonstrates how write an empty JSON object: |
| 30 | * <pre> |
| 31 | * <code> |
| 32 | * JsonWriter jsonWriter = Json.createWriter(...); |
| 33 | * jsonWriter.writeObject(Json.createObjectBuilder().build()); |
| 34 | * jsonWriter.close(); |
| 35 | * </code> |
| 36 | * </pre> |
| 37 | * |
| 38 | * <p> |
| 39 | * The class {@link JsonWriterFactory} also contains methods to create |
| 40 | * {@code JsonWriter} instances. A factory instance can be used to create |
| 41 | * multiple writer instances with the same configuration. This the preferred |
| 42 | * way to create multiple instances. A sample usage is shown in the following |
| 43 | * example: |
| 44 | * <pre> |
| 45 | * <code> |
| 46 | * JsonWriterFactory factory = Json.createWriterFactory(config); |
| 47 | * JsonWriter writer1 = factory.createWriter(...); |
| 48 | * JsonWriter writer2 = factory.createWriter(...); |
| 49 | * </code> |
| 50 | * </pre> |
| 51 | */ |
| 52 | public interface JsonWriter extends /*Auto*/Closeable { |
| 53 | |
| 54 | /** |
| 55 | * Writes the specified JSON {@link JsonArray array} to the output |
| 56 | * source. This method needs to be called only once for a writer instance. |
| 57 | * |
| 58 | * @param array JSON array that is to be written to the output source |
| 59 | * @throws JsonException if the specified JSON object cannot be |
| 60 | * written due to i/o error (IOException would be cause of |
| 61 | * JsonException) |
| 62 | * @throws IllegalStateException if writeArray, writeObject, write or close |
| 63 | * method is already called |
| 64 | */ |
| 65 | void writeArray(JsonArray array); |
| 66 | |
| 67 | /** |
| 68 | * Writes the specified JSON {@link JsonObject object} to the output |
| 69 | * source. This method needs to be called only once for a writer instance. |
| 70 | * |
| 71 | * @param object JSON object that is to be written to the output source |
| 72 | * @throws JsonException if the specified JSON object cannot be |
| 73 | * written due to i/o error (IOException would be cause of JsonException) |
| 74 | * @throws IllegalStateException if writeArray, writeObject, write or close |
| 75 | * method is already called |
| 76 | */ |
| 77 | void writeObject(JsonObject object); |
| 78 | |
| 79 | /** |
| 80 | * Writes the specified JSON {@link JsonObject object} or |
| 81 | * {@link JsonArray array} to the output source. This method needs |
| 82 | * to be called only once for a writer instance. |
| 83 | * |
| 84 | * @param value JSON array or object that is to be written to the output |
| 85 | * source |
| 86 | * @throws JsonException if the specified JSON object cannot be |
| 87 | * written due to i/o error (IOException would be cause of |
| 88 | * JsonException) |
| 89 | * @throws IllegalStateException if writeArray, writeObject, write |
| 90 | * or close method is already called |
| 91 | */ |
| 92 | void write(JsonStructure value); |
| 93 | |
| 94 | /** |
| 95 | * Closes this JSON writer and frees any resources associated with the |
| 96 | * writer. This method closes the underlying output source. |
| 97 | * |
| 98 | * @throws JsonException if an i/o error occurs (IOException would be |
| 99 | * cause of JsonException) |
| 100 | */ |
| 101 | |
| 102 | /** |
| 103 | * Writes the specified {@link JsonValue} to the output source. |
| 104 | * method needs to be called only once for a write instance. |
| 105 | * |
| 106 | * @param value a {@code JsonValue} to be written to the output |
| 107 | * source |
| 108 | * @throws JsonException if the specified JSON object cannot be |
| 109 | * written due to i/o error (IOException would be cause of |
| 110 | * JsonException) |
| 111 | * @throws IllegalStateException if writeArray, writeObject, write |
| 112 | * or close method is already called |
| 113 | * |
| 114 | * @since 1.1 |
| 115 | */ |
| 116 | default void write(JsonValue value) { |
| 117 | throw new UnsupportedOperationException(); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | void close(); |
| 122 | |
| 123 | } |