| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 30 |
|
| 31 |
|
| 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 |
|
| 52 |
|
| 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 |
|
| 71 |
|
| 72 |
generator.flushBuffer(); |
| 73 |
|
| 74 |
|
| 75 |
|
| 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 |
|
| 93 |
|
| 94 |
generator.flushBuffer(); |
| 95 |
|
| 96 |
|
| 97 |
|
| 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 |
|
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
} |