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

Last change on this file since 10464 was 6756, checked in by Don-vip, 11 years ago

fix #9590 - replace org.json with GPL-compliant jsonp + remove mention of old world image removed in r1680

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