| 1 | /*
|
|---|
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * The contents of this file are subject to the terms of either the GNU
|
|---|
| 7 | * General Public License Version 2 only ("GPL") or the Common Development
|
|---|
| 8 | * and Distribution License("CDDL") (collectively, the "License"). You
|
|---|
| 9 | * may not use this file except in compliance with the License. You can
|
|---|
| 10 | * obtain a copy of the License at
|
|---|
| 11 | * https://oss.oracle.com/licenses/CDDL+GPL-1.1
|
|---|
| 12 | * or LICENSE.txt. See the License for the specific
|
|---|
| 13 | * language governing permissions and limitations under the License.
|
|---|
| 14 | *
|
|---|
| 15 | * When distributing the software, include this License Header Notice in each
|
|---|
| 16 | * file and include the License file at LICENSE.txt.
|
|---|
| 17 | *
|
|---|
| 18 | * GPL Classpath Exception:
|
|---|
| 19 | * Oracle designates this particular file as subject to the "Classpath"
|
|---|
| 20 | * exception as provided by Oracle in the GPL Version 2 section of the License
|
|---|
| 21 | * file that accompanied this code.
|
|---|
| 22 | *
|
|---|
| 23 | * Modifications:
|
|---|
| 24 | * If applicable, add the following below the License Header, with the fields
|
|---|
| 25 | * enclosed by brackets [] replaced by your own identifying information:
|
|---|
| 26 | * "Portions Copyright [year] [name of copyright owner]"
|
|---|
| 27 | *
|
|---|
| 28 | * Contributor(s):
|
|---|
| 29 | * If you wish your version of this file to be governed by only the CDDL or
|
|---|
| 30 | * only the GPL Version 2, indicate your decision by adding "[Contributor]
|
|---|
| 31 | * elects to include this software in this distribution under the [CDDL or GPL
|
|---|
| 32 | * Version 2] license." If you don't indicate a single choice of license, a
|
|---|
| 33 | * recipient has the option to distribute your version of this file under
|
|---|
| 34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to
|
|---|
| 35 | * its licensees as provided above. However, if you add GPL Version 2 code
|
|---|
| 36 | * and therefore, elected the GPL Version 2 license, then the option applies
|
|---|
| 37 | * only if the new code is made subject to such option by the copyright
|
|---|
| 38 | * holder.
|
|---|
| 39 | */
|
|---|
| 40 |
|
|---|
| 41 | package org.glassfish.json;
|
|---|
| 42 |
|
|---|
| 43 | import org.glassfish.json.api.BufferPool;
|
|---|
| 44 |
|
|---|
| 45 | import javax.json.*;
|
|---|
| 46 | import java.io.FilterOutputStream;
|
|---|
| 47 | import java.io.IOException;
|
|---|
| 48 | import java.io.OutputStream;
|
|---|
| 49 | import java.io.Writer;
|
|---|
| 50 | import java.nio.charset.Charset;
|
|---|
| 51 | import java.nio.charset.StandardCharsets;
|
|---|
| 52 | import java.util.Map;
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * JsonWriter impl using generator.
|
|---|
| 56 | *
|
|---|
| 57 | * @author Jitendra Kotamraju
|
|---|
| 58 | */
|
|---|
| 59 | class JsonWriterImpl implements JsonWriter {
|
|---|
| 60 |
|
|---|
| 61 | private final JsonGeneratorImpl generator;
|
|---|
| 62 | private boolean writeDone;
|
|---|
| 63 | private final NoFlushOutputStream os;
|
|---|
| 64 |
|
|---|
| 65 | JsonWriterImpl(Writer writer, BufferPool bufferPool) {
|
|---|
| 66 | this(writer, false, bufferPool);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | JsonWriterImpl(Writer writer, boolean prettyPrinting, BufferPool bufferPool) {
|
|---|
| 70 | generator = prettyPrinting
|
|---|
| 71 | ? new JsonPrettyGeneratorImpl(writer, bufferPool)
|
|---|
| 72 | : new JsonGeneratorImpl(writer, bufferPool);
|
|---|
| 73 | os = null;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | JsonWriterImpl(OutputStream out, BufferPool bufferPool) {
|
|---|
| 77 | this(out, StandardCharsets.UTF_8, false, bufferPool);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | JsonWriterImpl(OutputStream out, boolean prettyPrinting, BufferPool bufferPool) {
|
|---|
| 81 | this(out, StandardCharsets.UTF_8, prettyPrinting, bufferPool);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | JsonWriterImpl(OutputStream out, Charset charset,
|
|---|
| 85 | boolean prettyPrinting, BufferPool bufferPool) {
|
|---|
| 86 | // Decorating the given stream, so that buffered contents can be
|
|---|
| 87 | // written without actually flushing the stream.
|
|---|
| 88 | this.os = new NoFlushOutputStream(out);
|
|---|
| 89 | generator = prettyPrinting
|
|---|
| 90 | ? new JsonPrettyGeneratorImpl(os, charset, bufferPool)
|
|---|
| 91 | : new JsonGeneratorImpl(os, charset, bufferPool);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | @Override
|
|---|
| 95 | public void writeArray(JsonArray array) {
|
|---|
| 96 | if (writeDone) {
|
|---|
| 97 | throw new IllegalStateException(JsonMessages.WRITER_WRITE_ALREADY_CALLED());
|
|---|
| 98 | }
|
|---|
| 99 | writeDone = true;
|
|---|
| 100 | generator.writeStartArray();
|
|---|
| 101 | for(JsonValue value : array) {
|
|---|
| 102 | generator.write(value);
|
|---|
| 103 | }
|
|---|
| 104 | generator.writeEnd();
|
|---|
| 105 | // Flush the generator's buffered contents. This won't work for byte
|
|---|
| 106 | // streams as intermediary OutputStreamWriter buffers.
|
|---|
| 107 | generator.flushBuffer();
|
|---|
| 108 | // Flush buffered contents but not the byte stream. generator.flush()
|
|---|
| 109 | // does OutputStreamWriter#flushBuffer (package private) and underlying
|
|---|
| 110 | // byte stream#flush(). Here underlying stream's flush() is no-op.
|
|---|
| 111 | if (os != null) {
|
|---|
| 112 | generator.flush();
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | @Override
|
|---|
| 117 | public void writeObject(JsonObject object) {
|
|---|
| 118 | if (writeDone) {
|
|---|
| 119 | throw new IllegalStateException(JsonMessages.WRITER_WRITE_ALREADY_CALLED());
|
|---|
| 120 | }
|
|---|
| 121 | writeDone = true;
|
|---|
| 122 | generator.writeStartObject();
|
|---|
| 123 | for(Map.Entry<String, JsonValue> e : object.entrySet()) {
|
|---|
| 124 | generator.write(e.getKey(), e.getValue());
|
|---|
| 125 | }
|
|---|
| 126 | generator.writeEnd();
|
|---|
| 127 | // Flush the generator's buffered contents. This won't work for byte
|
|---|
| 128 | // streams as intermediary OutputStreamWriter buffers.
|
|---|
| 129 | generator.flushBuffer();
|
|---|
| 130 | // Flush buffered contents but not the byte stream. generator.flush()
|
|---|
| 131 | // does OutputStreamWriter#flushBuffer (package private) and underlying
|
|---|
| 132 | // byte stream#flush(). Here underlying stream's flush() is no-op.
|
|---|
| 133 | if (os != null) {
|
|---|
| 134 | generator.flush();
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | @Override
|
|---|
| 139 | public void write(JsonStructure value) {
|
|---|
| 140 | if (value instanceof JsonArray) {
|
|---|
| 141 | writeArray((JsonArray)value);
|
|---|
| 142 | } else {
|
|---|
| 143 | writeObject((JsonObject)value);
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | @Override
|
|---|
| 148 | public void write(JsonValue value) {
|
|---|
| 149 | switch (value.getValueType()) {
|
|---|
| 150 | case OBJECT:
|
|---|
| 151 | writeObject((JsonObject) value);
|
|---|
| 152 | return;
|
|---|
| 153 | case ARRAY:
|
|---|
| 154 | writeArray((JsonArray) value);
|
|---|
| 155 | return;
|
|---|
| 156 | default:
|
|---|
| 157 | if (writeDone) {
|
|---|
| 158 | throw new IllegalStateException(JsonMessages.WRITER_WRITE_ALREADY_CALLED());
|
|---|
| 159 | }
|
|---|
| 160 | writeDone = true;
|
|---|
| 161 | generator.write(value);
|
|---|
| 162 | generator.flushBuffer();
|
|---|
| 163 | if (os != null) {
|
|---|
| 164 | generator.flush();
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | @Override
|
|---|
| 170 | public void close() {
|
|---|
| 171 | writeDone = true;
|
|---|
| 172 | generator.close();
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | private static final class NoFlushOutputStream extends FilterOutputStream {
|
|---|
| 176 | public NoFlushOutputStream(OutputStream out) {
|
|---|
| 177 | super(out);
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | @Override
|
|---|
| 181 | public void write(byte b[], int off, int len) throws IOException {
|
|---|
| 182 | out.write(b, off ,len);
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | @Override
|
|---|
| 186 | public void flush() {
|
|---|
| 187 | // no-op
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | }
|
|---|