source: josm/trunk/src/javax/json/stream/JsonGenerator.java@ 14273

Last change on this file since 14273 was 14273, checked in by stoecker, 6 years ago

fix typos - patch by naoliv - fix #16781 - Thanks a lot

File size: 21.9 KB
Line 
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2011-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
41package javax.json.stream;
42
43import javax.json.JsonValue;
44import java.io.Closeable;
45import java.io.Flushable;
46import java.math.BigDecimal;
47import java.math.BigInteger;
48
49/**
50 * Writes JSON data to an output source in a streaming way. The class
51 * {@link javax.json.Json} contains methods to create generators for character
52 * or output streams ({@link java.io.Writer} and {@link java.io.OutputStream}).
53 *
54 * <p>
55 * The following example shows how to create a JSON generator:
56 * <pre>
57 * <code>
58 * JsonGenerator generator = Json.createGenerator(...);
59 * </code>
60 * </pre>
61 *
62 * <p>
63 * The class {@link JsonGeneratorFactory} also contains methods to create
64 * {@code JsonGenerator} instances. {@link JsonGeneratorFactory} should be used
65 * when creating multiple generator instances, as in the following example:
66 * <pre>
67 * <code>
68 * JsonGeneratorFactory factory = Json.createGeneratorFactory();
69 * JsonGenerator generator1 = factory.createGenerator(...);
70 * JsonGenerator generator2 = factory.createGenerator(...);
71 * </code>
72 * </pre>
73 *
74 * <p>
75 * JSON objects can be created using {@code JsonGenerator} by calling the
76 * {@link #writeStartObject()} method and then adding name/value pairs with the
77 * {@code write} method.
78 * <p>
79 * The following example shows how to generate an empty JSON object:
80 * <pre>
81 * <code>
82 * JsonGenerator generator = ...;
83 * generator.writeStartObject().writeEnd().close();
84 * </code>
85 * </pre>
86 *
87 * JSON arrays can be created using {@code JsonGenerator} by calling the
88 * {@link #writeStartArray()} method and then adding values with the
89 * {@code write} method.
90 *
91 * <p>
92 * The following example shows how to generate an empty JSON array:
93 * <pre>
94 * <code>
95 * JsonGenerator generator = ...;
96 * generator.writeStartArray().writeEnd().close();
97 * </code>
98 * </pre>
99 *
100 * <p>
101 * Other JSON values (that are not JSON objects or arrays) can be created
102 * by calling the appropriate {@code write} methods.
103 * <p>
104 * The following example shows how to generate a JSON string:
105 * <pre><code>
106 * JsonGenerator generator = ...;
107 * generator.write("message").close();
108 * </code></pre>
109 *
110 * {@code JsonGenerator} methods can be chained as in the following example:
111 * <pre>
112 * <code>
113 * generator
114 * .writeStartObject()
115 * .write("firstName", "John")
116 * .write("lastName", "Smith")
117 * .write("age", 25)
118 * .writeStartObject("address")
119 * .write("streetAddress", "21 2nd Street")
120 * .write("city", "New York")
121 * .write("state", "NY")
122 * .write("postalCode", "10021")
123 * .writeEnd()
124 * .writeStartArray("phoneNumber")
125 * .writeStartObject()
126 * .write("type", "home")
127 * .write("number", "212 555-1234")
128 * .writeEnd()
129 * .writeStartObject()
130 * .write("type", "fax")
131 * .write("number", "646 555-4567")
132 * .writeEnd()
133 * .writeEnd()
134 * .writeEnd();
135 * generator.close();
136 * </code>
137 * </pre>
138 *
139 * The example code above generates the following JSON (or equivalent):
140 * <pre>
141 * <code>
142 * {
143 * "firstName": "John", "lastName": "Smith", "age": 25,
144 * "address" : {
145 * "streetAddress": "21 2nd Street",
146 * "city": "New York",
147 * "state": "NY",
148 * "postalCode": "10021"
149 * },
150 * "phoneNumber": [
151 * {"type": "home", "number": "212 555-1234"},
152 * {"type": "fax", "number": "646 555-4567"}
153 * ]
154 * }
155 * </code>
156 * </pre>
157 *
158 * The generated JSON text must strictly conform to the grammar defined in
159 * <a href="http://www.ietf.org/rfc/rfc7159.txt">RFC 7159</a>.
160 *
161 * @see javax.json.Json
162 * @see JsonGeneratorFactory
163 */
164public interface JsonGenerator extends Flushable, /*Auto*/Closeable {
165 /**
166 * Configuration property to generate JSON prettily. All providers
167 * must support this property. The value of the property could be
168 * be anything.
169 */
170 String PRETTY_PRINTING = "javax.json.stream.JsonGenerator.prettyPrinting" ;
171
172 /**
173 * Writes the JSON start object character. It starts a new child object
174 * context within which JSON name/value pairs can be written to the object.
175 * This method is valid only in an array context, field context or in no context (when a
176 * context is not yet started). This method can only be called once in
177 * no context.
178 *
179 * @return this generator
180 * @throws javax.json.JsonException if an i/o error occurs (IOException
181 * would be cause of JsonException)
182 * @throws JsonGenerationException if this method is called within an
183 * object context or if it is called more than once in no context.
184 */
185 JsonGenerator writeStartObject();
186
187 /**
188 * Writes the JSON name/start object character pair in the current
189 * object context. It starts a new child object context within which JSON
190 * name/value pairs can be written to the object.
191 *
192 * @param name a name within the JSON name/object pair to be written
193 * @return this generator
194 * @throws javax.json.JsonException if an i/o error occurs (IOException
195 * would be cause of JsonException)
196 * @throws JsonGenerationException if this method is not called within an
197 * object context
198 */
199 JsonGenerator writeStartObject(String name);
200
201 /**
202 * Writes the JSON name with a colon. It starts a field context, in which valid
203 * options are writing a value, starting an object or an array.
204 *
205 * Writing value closes field context, if object or array is started after field name,
206 * field context will be closed after object/array close.
207 *
208 * @param name name of json field
209 * @return this generator
210 * @throws javax.json.JsonException if an i/o error occurs (IOException
211 * would be cause of JsonException)
212 * @throws JsonGenerationException if this method is not called within an
213 * object context
214 *
215 * @since 1.1
216 */
217 JsonGenerator writeKey(String name);
218
219 /**
220 * Writes the JSON start array character. It starts a new child array
221 * context within which JSON values can be written to the array. This
222 * method is valid only in an array context, field context or in no context (when a
223 * context is not yet started). This method can only be called once in
224 * no context.
225 *
226 * @return this generator
227 * @throws javax.json.JsonException if an i/o error occurs (IOException
228 * would be cause of JsonException)
229 * @throws JsonGenerationException if this method is called within an
230 * object context or if called more than once in no context
231 */
232 JsonGenerator writeStartArray();
233
234 /**
235 * Writes the JSON name/start array character pair with in the current
236 * object context. It starts a new child array context within which JSON
237 * values can be written to the array.
238 *
239 * @param name a name within the JSON name/array pair to be written
240 * @return this generator
241 * @throws javax.json.JsonException if an i/o error occurs (IOException
242 * would be cause of JsonException)
243 * @throws JsonGenerationException if this method is not called within
244 * an object context
245 */
246 JsonGenerator writeStartArray(String name);
247
248 /**
249 * Writes a JSON name/value pair in the current object context.
250 *
251 * @param name a name in the JSON name/value pair to be written in
252 * current JSON object
253 * @param value a value in the JSON name/value pair to be written in
254 * current JSON object
255 * @return this generator
256 * @throws javax.json.JsonException if an i/o error occurs (IOException
257 * would be cause of JsonException)
258 * @throws JsonGenerationException if this method is not called within an
259 * object context
260 */
261 JsonGenerator write(String name, JsonValue value);
262
263 /**
264 * Writes a JSON name/string value pair in the current object context.
265 * The specified value is written as JSON string value.
266 *
267 * @param name a name in the JSON name/string pair to be written in
268 * current JSON object
269 * @param value a value in the JSON name/string pair to be written in
270 * current JSON object
271 * @return this generator
272 * @throws javax.json.JsonException if an i/o error occurs (IOException
273 * would be cause of JsonException)
274 * @throws JsonGenerationException if this method is not called within an
275 * object context
276 */
277 JsonGenerator write(String name, String value);
278
279 /**
280 * Writes a JSON name/number value pair in the current object context.
281 * The specified value is written as a JSON number value. The string
282 * {@code new BigDecimal(value).toString()}
283 * is used as the text value for writing.
284 *
285 * @param name a name in the JSON name/number pair to be written in
286 * current JSON object
287 * @param value a value in the JSON name/number pair to be written in
288 * current JSON object
289 * @return this generator
290 * @throws javax.json.JsonException if an i/o error occurs (IOException
291 * would be cause of JsonException)
292 * @throws JsonGenerationException if this method is not called within an
293 * object context.
294 */
295 JsonGenerator write(String name, BigInteger value);
296
297 /**
298 * Writes a JSON name/number value pair in the current object context.
299 * The specified value is written as a JSON number value. The specified
300 * value's {@code toString()} is used as the text value for writing.
301 *
302 * @param name a name in the JSON name/number pair to be written in
303 * current JSON object
304 * @param value a value in the JSON name/number pair to be written in
305 * current JSON object
306 * @return this generator
307 * @throws javax.json.JsonException if an i/o error occurs (IOException
308 * would be cause of JsonException)
309 * @throws JsonGenerationException if this method is not called within an
310 * object context.
311 */
312 JsonGenerator write(String name, BigDecimal value);
313
314 /**
315 * Writes a JSON name/number value pair in the current object context.
316 * The specified value is written as a JSON number value. The string
317 * {@code new BigDecimal(value).toString()} is used as the text value
318 * for writing.
319 *
320 * @param name a name in the JSON name/number pair to be written in
321 * current JSON object
322 * @param value a value in the JSON name/number pair to be written in
323 * current JSON object
324 * @return this generator
325 * @throws javax.json.JsonException if an i/o error occurs (IOException
326 * would be cause of JsonException)
327 * @throws JsonGenerationException if this method is not called within an
328 * object context.
329 */
330 JsonGenerator write(String name, int value);
331
332 /**
333 * Writes a JSON name/number value pair in the current object context.
334 * The specified value is written as a JSON number value. The string
335 * {@code new BigDecimal(value).toString()} is used as the text
336 * value for writing.
337 *
338 * @param name a name in the JSON name/number pair to be written in
339 * current JSON object
340 * @param value a value in the JSON name/number pair to be written in
341 * current JSON object
342 * @return this generator
343 * @throws javax.json.JsonException if an i/o error occurs (IOException
344 * would be cause of JsonException)
345 * @throws JsonGenerationException if this method is not called within an
346 * object context.
347 */
348 JsonGenerator write(String name, long value);
349
350 /**
351 * Writes a JSON name/number value pair in the current object context.
352 * The specified value is written as a JSON number value. The string
353 * {@code BigDecimal.valueOf(double).toString()}
354 * is used as the text value for writing.
355 *
356 * @param name a name in the JSON name/number pair to be written in
357 * current JSON object
358 * @param value a value in the JSON name/number pair to be written in
359 * current JSON object
360 * @return this generator
361 * @throws javax.json.JsonException if an i/o error occurs (IOException
362 * would be cause of JsonException)
363 * @throws NumberFormatException if the value is Not-a-Number (NaN) or infinity.
364 * @throws JsonGenerationException if this method is not called within an
365 * object context
366 */
367 JsonGenerator write(String name, double value);
368
369 /**
370 * Writes a JSON name/boolean value pair in the current object context.
371 * If value is true, it writes the JSON {@code true} value, otherwise
372 * it writes the JSON {@code false} value.
373 *
374 * @param name a name in the JSON name/boolean pair to be written in
375 * current JSON object
376 * @param value a value in the JSON name/boolean pair to be written in
377 * current JSON object
378 * @return this generator
379 * @throws javax.json.JsonException if an i/o error occurs (IOException
380 * would be cause of JsonException)
381 * @throws JsonGenerationException if this method is not called within an
382 * object context.
383 */
384 JsonGenerator write(String name, boolean value);
385
386
387 /**
388 * Writes a JSON name/null value pair in an current object context.
389 *
390 * @param name a name in the JSON name/null pair to be written in
391 * current JSON object
392 * @return this generator
393 * @throws javax.json.JsonException if an i/o error occurs (IOException
394 * would be cause of JsonException)
395 * @throws JsonGenerationException if this method is not called within an
396 * object context
397 */
398 JsonGenerator writeNull(String name);
399
400 /**
401 * Writes the end of the current context. If the current context is
402 * an array context, this method writes the end-of-array character (']').
403 * If the current context is an object context, this method writes the
404 * end-of-object character ('}'). After writing the end of the current
405 * context, the parent context becomes the new current context.
406 * If parent context is field context, it is closed.
407 *
408 * @return this generator
409 * @throws javax.json.JsonException if an i/o error occurs (IOException
410 * would be cause of JsonException)
411 * @throws JsonGenerationException if this method is called in no context.
412 */
413 JsonGenerator writeEnd();
414
415 /**
416 * Writes the specified value as a JSON value within
417 * the current array, field or root context.
418 *
419 * @param value a value to be written in current JSON array
420 * @return this generator
421 * @throws javax.json.JsonException if an i/o error occurs (IOException
422 * would be cause of JsonException)
423 * @throws JsonGenerationException if this method is not called within an
424 * array or root context.
425 */
426 JsonGenerator write(JsonValue value);
427
428 /**
429 * Writes the specified value as a JSON string value within
430 * the current array, field or root context.
431 *
432 * @param value a value to be written in current JSON array
433 * @return this generator
434 * @throws javax.json.JsonException if an i/o error occurs (IOException
435 * would be cause of JsonException)
436 * @throws JsonGenerationException if this method is not called within an
437 * array or root context.
438 */
439 JsonGenerator write(String value);
440
441 /**
442 * Writes the specified value as a JSON number value within
443 * the current array, field or root context. The specified value's {@code toString()}
444 * is used as the the text value for writing.
445 *
446 * @param value a value to be written in current JSON array
447 * @return this generator
448 * @throws javax.json.JsonException if an i/o error occurs (IOException
449 * would be cause of JsonException)
450 * @throws JsonGenerationException if this method is not called within an
451 * array or root context.
452 *
453 * @see javax.json.JsonNumber
454 */
455 JsonGenerator write(BigDecimal value);
456
457 /**
458 * Writes the specified value as a JSON number value within
459 * the current array, field or root context. The string {@code new BigDecimal(value).toString()}
460 * is used as the text value for writing.
461 *
462 * @param value a value to be written in current JSON array
463 * @return this generator.
464 * @throws javax.json.JsonException if an i/o error occurs (IOException
465 * would be cause of JsonException)
466 * @throws JsonGenerationException if this method is not called within an
467 * array or root context.
468 *
469 * @see javax.json.JsonNumber
470 */
471 JsonGenerator write(BigInteger value);
472
473 /**
474 * Writes the specified value as a JSON number value within
475 * the current array, field or root context. The string {@code new BigDecimal(value).toString()}
476 * is used as the text value for writing.
477 *
478 * @param value a value to be written in current JSON array
479 * @return this generator
480 * @throws javax.json.JsonException if an i/o error occurs (IOException
481 * would be cause of JsonException)
482 * @throws JsonGenerationException if this method is not called within an
483 * array or root context.
484 */
485 JsonGenerator write(int value);
486
487 /**
488 * Writes the specified value as a JSON number value within
489 * the current array, field or root context. The string {@code new BigDecimal(value).toString()}
490 * is used as the text value for writing.
491 *
492 * @param value a value to be written in current JSON array
493 * @return this generator
494 * @throws javax.json.JsonException if an i/o error occurs (IOException
495 * would be cause of JsonException)
496 * @throws JsonGenerationException if this method is not called within an
497 * array or root context.
498 */
499 JsonGenerator write(long value);
500
501 /**
502 * Writes the specified value as a JSON number value within the current
503 * array, field or root context. The string {@code BigDecimal.valueOf(value).toString()}
504 * is used as the text value for writing.
505 *
506 * @param value a value to be written in current JSON array
507 * @return this generator
508 * @throws javax.json.JsonException if an i/o error occurs (IOException
509 * would be cause of JsonException)
510 * @throws JsonGenerationException if this method is not called within an
511 * array or root context.
512 * @throws NumberFormatException if the value is Not-a-Number (NaN) or infinity.
513 */
514 JsonGenerator write(double value);
515
516 /**
517 * Writes a JSON true or false value within the current array, field or root context.
518 * If value is true, this method writes the JSON {@code true} value,
519 * otherwise it writes the JSON {@code false} value.
520 *
521 * @param value a {@code boolean} value
522 * @return this generator
523 * @throws javax.json.JsonException if an i/o error occurs (IOException
524 * would be cause of JsonException)
525 * @throws JsonGenerationException if this method is not called within an
526 * array or root context.
527 */
528 JsonGenerator write(boolean value);
529
530 /**
531 * Writes a JSON null value within the current array, field or root context.
532 *
533 * @return this generator
534 * @throws javax.json.JsonException if an i/o error occurs (IOException
535 * would be cause of JsonException)
536 * @throws JsonGenerationException if this method is not called within an
537 * array or root context.
538 */
539 JsonGenerator writeNull();
540
541 /**
542 * Closes this generator and frees any resources associated with it.
543 * This method closes the underlying output source.
544 *
545 * @throws javax.json.JsonException if an i/o error occurs (IOException
546 * would be cause of JsonException)
547 * @throws JsonGenerationException if an incomplete JSON is generated
548 */
549 @Override
550 void close();
551
552 /**
553 * Flushes the underlying output source. If the generator has saved
554 * any characters in a buffer, writes them immediately to the underlying
555 * output source before flushing it.
556 *
557 * @throws javax.json.JsonException if an i/o error occurs (IOException
558 * would be cause of JsonException)
559 */
560 @Override
561 void flush();
562
563}
Note: See TracBrowser for help on using the repository browser.