javax.json.stream
Interface JsonGenerator

All Superinterfaces:
Closeable

public interface JsonGenerator
extends Closeable

A JSON generator that writes JSON in a streaming way. The generator can be created from many output sources like Writer and OutputStream.

For example, a generator can be created as follows:

 JsonGenerator generator = Json.createGenerator(...);
 
A generator can also be created using JsonGeneratorFactory. If multiple generator instances are created, then creating them using a generator factory is preferred.

 JsonGeneratorFactory factory = Json.createGeneratorFactory();
 JsonGenerator generator1 = factory.createGenerator(...);
 JsonGenerator generator2 = factory.createGenerator(...);
 

The generator is used to generate JSON object in a streaming way by calling its beginObject() method and adding name/value pairs.

For example 1:

Empty JSON object can be generated as follows:

 JsonGenerator generator = ...;
 generator.beginObject().endObject().close();
 
The generator is used to generate JSON array in a streaming way by calling its beginArray() method and adding values.

For example 2:

Empty JSON array can be generated as follows:

 JsonGenerator generator = ...;
 generator.beginArray().endArray().close();
 
Similarly, the following generator

 generator
     .beginObject()
         .add("firstName", "John")
         .add("lastName", "Smith")
         .add("age", 25)
         .beginObject("address")
             .add("streetAddress", "21 2nd Street")
             .add("city", "New York")
             .add("state", "NY")
             .add("postalCode", "10021")
         .endObject()
         .beginArray("phoneNumber")
             .beginObject()
                 .add("type", "home")
                 .add("number", "212 555-1234")
             .endObject()
             .beginObject()
                 .add("type", "fax")
                 .add("number", "646 555-4567")
             .endObject()
         .endArray()
     .endObject();
 generator.close();
 
would generate a JSON equivalent to the following:

 {
   "firstName": "John", "lastName": "Smith", "age": 25,
   "address" : {
       "streetAddress", "21 2nd Street",
       "city", "New York",
       "state", "NY",
       "postalCode", "10021"
   },
   "phoneNumber": [
       {"type": "home", "number": "212 555-1234"},
       {"type": "fax", "number": "646 555-4567"}
    ]
 }
 

Author:
Jitendra Kotamraju
See Also:
Json, JsonGeneratorFactory

Method Summary
 JsonArrayBuilder<Closeable> beginArray()
          Starts writing of a JSON array in a streaming fashion.
 JsonObjectBuilder<Closeable> beginObject()
          Starts writing of a JSON object in a streaming fashion.
 void close()
          Closes this generator and frees any resources associated with the generator.
 

Method Detail

beginObject

JsonObjectBuilder<Closeable> beginObject()
Starts writing of a JSON object in a streaming fashion.

Returns:
an object builder

beginArray

JsonArrayBuilder<Closeable> beginArray()
Starts writing of a JSON array in a streaming fashion.

Returns:
an array builder

close

void close()
Closes this generator and frees any resources associated with the generator. This doesn't close the underlying output source.

Specified by:
close in interface Closeable



Copyright © 2012 Oracle and/or its affiliates. All rights reserved.