Debian Codegraph @

src/org/eclipse/parsson/JsonWriterImpl.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2013, 2023 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 org.eclipse.parsson;
18
19 import jakarta.json.*;
20 import java.io.FilterOutputStream;
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.io.Writer;
24 import java.nio.charset.Charset;
25 import java.nio.charset.StandardCharsets;
26 import java.util.Map;
27
28 /**
29 * JsonWriter impl using generator.
30 *
31 * @author Jitendra Kotamraju
32 */
33 class JsonWriterImpl implements JsonWriter {
34
35 private final JsonGeneratorImpl generator;
36 private boolean writeDone;
37 private final NoFlushOutputStream os;
38
39 JsonWriterImpl(Writer writer, JsonContext jsonContext) {
40 this.generator = jsonContext.prettyPrinting()
41 ? new JsonPrettyGeneratorImpl(writer, jsonContext)
42 : new JsonGeneratorImpl(writer, jsonContext);
43 this.os = null;
44 }
45
46 JsonWriterImpl(OutputStream out, JsonContext jsonContext) {
47 this(out, StandardCharsets.UTF_8, jsonContext);
48 }
49
50 JsonWriterImpl(OutputStream out, Charset charset, JsonContext jsonContext) {
51 // Decorating the given stream, so that buffered contents can be
52 // written without actually flushing the stream.
53 this.os = new NoFlushOutputStream(out);
54 this.generator = jsonContext.prettyPrinting()
55 ? new JsonPrettyGeneratorImpl(os, charset, jsonContext)
56 : new JsonGeneratorImpl(os, charset, jsonContext);
57 }
58
59 @Override
60 public void writeArray(JsonArray array) {
61 if (writeDone) {
62 throw new IllegalStateException(JsonMessages.WRITER_WRITE_ALREADY_CALLED());
63 }
64 writeDone = true;
65 generator.writeStartArray();
66 for(JsonValue value : array) {
67 generator.write(value);
68 }
69 generator.writeEnd();
70 // Flush the generator's buffered contents. This won't work for byte
71 // streams as intermediary OutputStreamWriter buffers.
72 generator.flushBuffer();
73 // Flush buffered contents but not the byte stream. generator.flush()
74 // does OutputStreamWriter#flushBuffer (package private) and underlying
75 // byte stream#flush(). Here underlying stream's flush() is no-op.
76 if (os != null) {
77 generator.flush();
78 }
79 }
80
81 @Override
82 public void writeObject(JsonObject object) {
83 if (writeDone) {
84 throw new IllegalStateException(JsonMessages.WRITER_WRITE_ALREADY_CALLED());
85 }
86 writeDone = true;
87 generator.writeStartObject();
88 for(Map.Entry<String, JsonValue> e : object.entrySet()) {
89 generator.write(e.getKey(), e.getValue());
90 }
91 generator.writeEnd();
92 // Flush the generator's buffered contents. This won't work for byte
93 // streams as intermediary OutputStreamWriter buffers.
94 generator.flushBuffer();
95 // Flush buffered contents but not the byte stream. generator.flush()
96 // does OutputStreamWriter#flushBuffer (package private) and underlying
97 // byte stream#flush(). Here underlying stream's flush() is no-op.
98 if (os != null) {
99 generator.flush();
100 }
101 }
102
103 @Override
104 public void write(JsonStructure value) {
105 if (value instanceof JsonArray) {
106 writeArray((JsonArray)value);
107 } else {
108 writeObject((JsonObject)value);
109 }
110 }
111
112 @Override
113 public void write(JsonValue value) {
114 switch (value.getValueType()) {
115 case OBJECT:
116 writeObject((JsonObject) value);
117 return;
118 case ARRAY:
119 writeArray((JsonArray) value);
120 return;
121 default:
122 if (writeDone) {
123 throw new IllegalStateException(JsonMessages.WRITER_WRITE_ALREADY_CALLED());
124 }
125 writeDone = true;
126 generator.write(value);
127 generator.flushBuffer();
128 if (os != null) {
129 generator.flush();
130 }
131 }
132 }
133
134 @Override
135 public void close() {
136 writeDone = true;
137 generator.close();
138 }
139
140 private static final class NoFlushOutputStream extends FilterOutputStream {
141 public NoFlushOutputStream(OutputStream out) {
142 super(out);
143 }
144
145 @Override
146 public void write(byte b[], int off, int len) throws IOException {
147 out.write(b, off ,len);
148 }
149
150 @Override
151 public void flush() {
152 // no-op
153 }
154 }
155
156 }