1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.johnzon.core; |
20 | |
|
21 | |
import java.io.OutputStream; |
22 | |
import java.io.Writer; |
23 | |
import java.math.BigDecimal; |
24 | |
import java.math.BigInteger; |
25 | |
import java.nio.charset.Charset; |
26 | |
import java.util.concurrent.ConcurrentMap; |
27 | |
|
28 | |
import javax.json.JsonValue; |
29 | |
import javax.json.stream.JsonGenerator; |
30 | |
|
31 | |
final class JsonPrettyGeneratorImpl extends JsonGeneratorImpl { |
32 | |
private static final String DEFAULT_INDENTATION = " "; |
33 | |
private final String indent; |
34 | |
|
35 | |
public JsonPrettyGeneratorImpl(final Writer writer, final BufferStrategy.BufferProvider<char[]> bufferProvider, |
36 | |
final ConcurrentMap<String, String> cache) { |
37 | 0 | super(writer, bufferProvider, cache); |
38 | 0 | indent = DEFAULT_INDENTATION; |
39 | 0 | } |
40 | |
|
41 | |
public JsonPrettyGeneratorImpl(final OutputStream out, final Charset encoding, |
42 | |
final BufferStrategy.BufferProvider<char[]> bufferProvider, final ConcurrentMap<String, String> cache) { |
43 | 0 | super(out, encoding, bufferProvider, cache); |
44 | 0 | indent = DEFAULT_INDENTATION; |
45 | 0 | } |
46 | |
|
47 | |
public JsonPrettyGeneratorImpl(final OutputStream out, final BufferStrategy.BufferProvider<char[]> bufferProvider, |
48 | |
final ConcurrentMap<String, String> cache) { |
49 | 1 | super(out, bufferProvider, cache); |
50 | 1 | indent = DEFAULT_INDENTATION; |
51 | 1 | } |
52 | |
|
53 | |
private void writeEOL() { |
54 | 20 | justWrite(EOL); |
55 | 20 | } |
56 | |
|
57 | |
private void writeIndent(final int correctionOffset) { |
58 | 55 | for (int i = 0; i < depth + correctionOffset; i++) { |
59 | 35 | justWrite(indent); |
60 | |
} |
61 | 20 | } |
62 | |
|
63 | |
@Override |
64 | |
protected void addCommaIfNeeded() { |
65 | 29 | if (needComma) { |
66 | 10 | justWrite(COMMA_CHAR); |
67 | 10 | writeEOL(); |
68 | 10 | writeIndent(0); |
69 | 10 | needComma = false; |
70 | |
} |
71 | |
|
72 | 29 | } |
73 | |
|
74 | |
@Override |
75 | |
public JsonGenerator writeStartObject() { |
76 | 3 | if (depth > 0 && !needComma) { |
77 | 1 | writeEOL(); |
78 | 1 | writeIndent(0); |
79 | |
} |
80 | 3 | return super.writeStartObject(); |
81 | |
|
82 | |
} |
83 | |
|
84 | |
@Override |
85 | |
public JsonGenerator writeStartObject(final String name) { |
86 | 1 | if (!needComma) { |
87 | 0 | writeEOL(); |
88 | 0 | writeIndent(0); |
89 | |
} |
90 | 1 | return super.writeStartObject(name); |
91 | |
|
92 | |
} |
93 | |
|
94 | |
@Override |
95 | |
public JsonGenerator writeStartArray() { |
96 | 0 | if (depth > 0 && !needComma) { |
97 | 0 | writeEOL(); |
98 | 0 | writeIndent(0); |
99 | |
} |
100 | 0 | return super.writeStartArray(); |
101 | |
|
102 | |
} |
103 | |
|
104 | |
@Override |
105 | |
public JsonGenerator writeStartArray(final String name) { |
106 | 1 | if (!needComma) { |
107 | 0 | writeEOL(); |
108 | 0 | writeIndent(0); |
109 | |
} |
110 | 1 | return super.writeStartArray(name); |
111 | |
|
112 | |
} |
113 | |
|
114 | |
|
115 | |
|
116 | |
@Override |
117 | |
public JsonGenerator writeEnd() { |
118 | 5 | writeEOL(); |
119 | 5 | writeIndent(-1); |
120 | 5 | return super.writeEnd(); |
121 | |
|
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
@Override |
127 | |
public JsonGenerator write(final String name, final JsonValue value) { |
128 | 0 | if (!needComma) { |
129 | 0 | writeEOL(); |
130 | 0 | writeIndent(0); |
131 | |
} |
132 | |
|
133 | 0 | return super.write(name, value); |
134 | |
|
135 | |
} |
136 | |
|
137 | |
@Override |
138 | |
public JsonGenerator write(final String name, final String value) { |
139 | 10 | if (!needComma) { |
140 | 4 | writeEOL(); |
141 | 4 | writeIndent(0); |
142 | |
} |
143 | 10 | return super.write(name, value); |
144 | |
} |
145 | |
|
146 | |
@Override |
147 | |
public JsonGenerator write(final String name, final BigInteger value) { |
148 | 0 | if (!needComma) { |
149 | 0 | writeEOL(); |
150 | 0 | writeIndent(0); |
151 | |
} |
152 | 0 | return super.write(name, value); |
153 | |
|
154 | |
} |
155 | |
|
156 | |
@Override |
157 | |
public JsonGenerator write(final String name, final BigDecimal value) { |
158 | 0 | if (!needComma) { |
159 | 0 | writeEOL(); |
160 | 0 | writeIndent(0); |
161 | |
} |
162 | 0 | return super.write(name, value); |
163 | |
|
164 | |
} |
165 | |
|
166 | |
@Override |
167 | |
public JsonGenerator write(final String name, final int value) { |
168 | 1 | if (!needComma) { |
169 | 0 | writeEOL(); |
170 | 0 | writeIndent(0); |
171 | |
} |
172 | 1 | return super.write(name, value); |
173 | |
|
174 | |
} |
175 | |
|
176 | |
@Override |
177 | |
public JsonGenerator write(final String name, final long value) { |
178 | 0 | if (!needComma) { |
179 | 0 | writeEOL(); |
180 | 0 | writeIndent(0); |
181 | |
} |
182 | 0 | return super.write(name, value); |
183 | |
|
184 | |
} |
185 | |
|
186 | |
@Override |
187 | |
public JsonGenerator write(final String name, final double value) { |
188 | 0 | if (!needComma) { |
189 | 0 | writeEOL(); |
190 | 0 | writeIndent(0); |
191 | |
} |
192 | 0 | return super.write(name, value); |
193 | |
|
194 | |
} |
195 | |
|
196 | |
@Override |
197 | |
public JsonGenerator write(final String name, final boolean value) { |
198 | 0 | if (!needComma) { |
199 | 0 | writeEOL(); |
200 | 0 | writeIndent(0); |
201 | |
} |
202 | 0 | return super.write(name, value); |
203 | |
|
204 | |
} |
205 | |
|
206 | |
@Override |
207 | |
public JsonGenerator writeNull(final String name) { |
208 | 0 | if (!needComma) { |
209 | 0 | writeEOL(); |
210 | 0 | writeIndent(0); |
211 | |
} |
212 | 0 | return super.writeNull(name); |
213 | |
|
214 | |
} |
215 | |
|
216 | |
@Override |
217 | |
public JsonGenerator write(final JsonValue value) { |
218 | 0 | if (!needComma) { |
219 | 0 | writeEOL(); |
220 | 0 | writeIndent(0); |
221 | |
} |
222 | 0 | return super.write(value); |
223 | |
|
224 | |
} |
225 | |
|
226 | |
@Override |
227 | |
public JsonGenerator write(final String value) { |
228 | 0 | if (!needComma) { |
229 | 0 | writeEOL(); |
230 | 0 | writeIndent(0); |
231 | |
} |
232 | 0 | return super.write(value); |
233 | |
|
234 | |
} |
235 | |
|
236 | |
@Override |
237 | |
public JsonGenerator write(final BigDecimal value) { |
238 | 0 | if (!needComma) { |
239 | 0 | writeEOL(); |
240 | 0 | writeIndent(0); |
241 | |
} |
242 | 0 | return super.write(value); |
243 | |
|
244 | |
} |
245 | |
|
246 | |
@Override |
247 | |
public JsonGenerator write(final BigInteger value) { |
248 | 0 | if (!needComma) { |
249 | 0 | writeEOL(); |
250 | 0 | writeIndent(0); |
251 | |
} |
252 | 0 | return super.write(value); |
253 | |
|
254 | |
} |
255 | |
|
256 | |
@Override |
257 | |
public JsonGenerator write(final int value) { |
258 | 0 | if (!needComma) { |
259 | 0 | writeEOL(); |
260 | 0 | writeIndent(0); |
261 | |
} |
262 | 0 | return super.write(value); |
263 | |
|
264 | |
} |
265 | |
|
266 | |
@Override |
267 | |
public JsonGenerator write(final long value) { |
268 | 0 | if (!needComma) { |
269 | 0 | writeEOL(); |
270 | 0 | writeIndent(0); |
271 | |
} |
272 | 0 | return super.write(value); |
273 | |
|
274 | |
} |
275 | |
|
276 | |
@Override |
277 | |
public JsonGenerator write(final double value) { |
278 | 0 | if (!needComma) { |
279 | 0 | writeEOL(); |
280 | 0 | writeIndent(0); |
281 | |
} |
282 | 0 | return super.write(value); |
283 | |
|
284 | |
} |
285 | |
|
286 | |
@Override |
287 | |
public JsonGenerator write(final boolean value) { |
288 | 0 | if (!needComma) { |
289 | 0 | writeEOL(); |
290 | 0 | writeIndent(0); |
291 | |
} |
292 | 0 | return super.write(value); |
293 | |
|
294 | |
} |
295 | |
|
296 | |
@Override |
297 | |
public JsonGenerator writeNull() { |
298 | 0 | if (!needComma) { |
299 | 0 | writeEOL(); |
300 | 0 | writeIndent(0); |
301 | |
} |
302 | 0 | return super.writeNull(); |
303 | |
} |
304 | |
|
305 | |
} |