Module jakarta.json

Interface JsonGenerator

All Superinterfaces:
AutoCloseable, Closeable, Flushable

public interface JsonGenerator extends Flushable, Closeable
Writes JSON data to an output source in a streaming way. The class Json contains methods to create generators for character or output streams (Writer and OutputStream).

The following example shows how to create a JSON generator:

 
 JsonGenerator generator = Json.createGenerator(...);
 
 

The class JsonGeneratorFactory also contains methods to create JsonGenerator instances. JsonGeneratorFactory should be used when creating multiple generator instances, as in the following example:

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

JSON objects can be created using JsonGenerator by calling the writeStartObject() method and then adding name/value pairs with the write method.

The following example shows how to generate an empty JSON object:

 
 JsonGenerator generator = ...;
 generator.writeStartObject().writeEnd().close();
 
 
JSON arrays can be created using JsonGenerator by calling the writeStartArray() method and then adding values with the write method.

The following example shows how to generate an empty JSON array:

 
 JsonGenerator generator = ...;
 generator.writeStartArray().writeEnd().close();
 
 

Other JSON values (that are not JSON objects or arrays) can be created by calling the appropiate write methods.

The following example shows how to generate a JSON string:


 JsonGenerator generator = ...;
 generator.write("message").close();
 
JsonGenerator methods can be chained as in the following example:
 
 generator
     .writeStartObject()
         .write("firstName", "John")
         .write("lastName", "Smith")
         .write("age", 25)
         .writeStartObject("address")
             .write("streetAddress", "21 2nd Street")
             .write("city", "New York")
             .write("state", "NY")
             .write("postalCode", "10021")
         .writeEnd()
         .writeStartArray("phoneNumber")
             .writeStartObject()
                 .write("type", "home")
                 .write("number", "212 555-1234")
             .writeEnd()
             .writeStartObject()
                 .write("type", "fax")
                 .write("number", "646 555-4567")
             .writeEnd()
         .writeEnd()
     .writeEnd();
 generator.close();
 
 
The example code above generates the following JSON (or equivalent):
 
 {
   "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"}
    ]
 }
 
 
The generated JSON text must strictly conform to the grammar defined in RFC 7159.
See Also: