| 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.io.OutputStream; |
| 20 | import java.io.Writer; |
| 21 | import java.nio.charset.Charset; |
| 22 | import java.util.Map; |
| 23 | |
| 24 | /** |
| 25 | * Factory to create {@link jakarta.json.JsonWriter} instances. If a factory |
| 26 | * instance is configured with some configuration, that would be |
| 27 | * used to configure the created writer instances. |
| 28 | * |
| 29 | * <p> |
| 30 | * {@link jakarta.json.JsonWriter} can also be created using {@link Json}'s |
| 31 | * {@code createWriter} methods. If multiple writer instances are created, |
| 32 | * then creating them using a writer factory is preferred. |
| 33 | * |
| 34 | * <p> |
| 35 | * <b>For example:</b> |
| 36 | * <pre> |
| 37 | * <code> |
| 38 | * JsonWriterFactory factory = Json.createWriterFactory(...); |
| 39 | * JsonWriter writer1 = factory.createWriter(...); |
| 40 | * JsonWriter writer2 = factory.createWriter(...); |
| 41 | * </code> |
| 42 | * </pre> |
| 43 | * |
| 44 | * <p> All the methods in this class are safe for use by multiple concurrent |
| 45 | * threads. |
| 46 | */ |
| 47 | public interface JsonWriterFactory { |
| 48 | |
| 49 | /** |
| 50 | * Creates a JSON writer to write a JSON {@link JsonObject object} or |
| 51 | * {@link JsonArray array} structure to the specified character stream. |
| 52 | * The writer is configured with the factory configuration. |
| 53 | * |
| 54 | * @param writer to which JSON object or array is written |
| 55 | * @return a JSON writer |
| 56 | */ |
| 57 | JsonWriter createWriter(Writer writer); |
| 58 | |
| 59 | /** |
| 60 | * Creates a JSON writer to write a JSON {@link JsonObject object} or |
| 61 | * {@link JsonArray array} structure to the specified byte stream. |
| 62 | * Characters written to the stream are encoded into bytes using UTF-8 |
| 63 | * encoding. The writer is configured with the factory configuration. |
| 64 | * |
| 65 | * @param out to which JSON object or array is written |
| 66 | * @return a JSON writer |
| 67 | */ |
| 68 | JsonWriter createWriter(OutputStream out); |
| 69 | |
| 70 | /** |
| 71 | * Creates a JSON writer to write a JSON {@link JsonObject object} or |
| 72 | * {@link JsonArray array} structure to the specified byte stream. |
| 73 | * Characters written to the stream are encoded into bytes using the |
| 74 | * specified charset. The writer is configured with the factory |
| 75 | * configuration. |
| 76 | * |
| 77 | * @param out to which JSON object or array is written |
| 78 | * @param charset a charset |
| 79 | * @return a JSON writer |
| 80 | */ |
| 81 | JsonWriter createWriter(OutputStream out, Charset charset); |
| 82 | |
| 83 | /** |
| 84 | * Returns read-only map of supported provider specific configuration |
| 85 | * properties that are used to configure the created JSON writer objects. |
| 86 | * If there are any specified configuration properties that are not |
| 87 | * supported by the provider, they won't be part of the returned map. |
| 88 | * |
| 89 | * @return a map of supported provider specific properties that are used |
| 90 | * to configure the created writers. The map may be empty but not null. |
| 91 | */ |
| 92 | Map<String, ?> getConfigInUse(); |
| 93 | |
| 94 | } |