public interface JsonGenerator extends Flushable, Closeable
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 writeStartObject() method and writing name/value pairs.
For example 1:
Empty JSON object can be generated as follows:
JsonGenerator generator = ...;
generator.writeStartObject().writeEnd().close();
The generator is used to generate JSON array in a streaming way by calling
its writeStartArray() method and adding values.
For example 2:
Empty JSON array can be generated as follows:
JsonGenerator generator = ...;
generator.writeStartArray().writeEnd().close();
The generator methods can be chained. Similarly, the following generator
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();
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"}
]
}
The generated JSON text must strictly conform to the grammar defined in the
RFC.Json,
JsonGeneratorFactory| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this generator and frees any resources associated with the
generator.
|
void |
flush() |
JsonGenerator |
write(BigDecimal value)
Writes the specified value as a JSON number value within
the current array context.
|
JsonGenerator |
write(BigInteger value)
Writes the specified value as a JSON number value within
the current array context.
|
JsonGenerator |
write(boolean value)
Writes a JSON true or false value within the current array context.
|
JsonGenerator |
write(double value)
Writes the specified value as a JSON number value within the current
array context.The
BigDecimal.valueOf(value).toString()
is used as the text value for writing. |
JsonGenerator |
write(int value)
Writes the specified value as a JSON number value within
the current array context.
|
JsonGenerator |
write(JsonValue value)
Writes the specified value as a JSON value within
the current array context.
|
JsonGenerator |
write(long value)
Writes the specified value as a JSON number value within
the current array context.
|
JsonGenerator |
write(String value)
Writes the specified value as a JSON string value within
the current array context.
|
JsonGenerator |
write(String name,
BigDecimal value)
Writes a JSON name/number value pair with in the current object context.
|
JsonGenerator |
write(String name,
BigInteger value)
Writes a JSON name/number value pair with in the current object context.
|
JsonGenerator |
write(String name,
boolean value)
Writes a JSON name/boolean value pair with in the current object context.
|
JsonGenerator |
write(String name,
double value)
Writes a JSON name/number value pair with in the current object context.
|
JsonGenerator |
write(String name,
int value)
Writes a JSON name/number value pair with in the current object context.
|
JsonGenerator |
write(String name,
JsonValue value)
Writes a JSON name/value pair with in the current object context.
|
JsonGenerator |
write(String name,
long value)
Writes a JSON name/number value pair with in the current object context.
|
JsonGenerator |
write(String name,
String value)
Writes a JSON name/string value pair with in the current object context.
|
JsonGenerator |
writeEnd()
Indicates the writeEnd of the current context.
|
JsonGenerator |
writeNull()
Writes a JSON null value within the current array context.
|
JsonGenerator |
writeNull(String name)
Writes a JSON name/null value pair with in an current object context.
|
JsonGenerator |
writeStartArray()
Writes the JSON start array character.
|
JsonGenerator |
writeStartArray(String name)
Writes the JSON name/start array character pair with in the current
object context.
|
JsonGenerator |
writeStartObject()
Writes the JSON start object character.
|
JsonGenerator |
writeStartObject(String name)
Writes the JSON name/start object character pair with in the current
object context.
|
JsonGenerator writeStartObject()
JsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if called within object context
or if called more than once in no contextJsonGenerator writeStartObject(String name)
name - a name within the JSON name/object pair to be writtenJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within object contextJsonGenerator writeStartArray()
JsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if called within object context
or if called more than once in no contextJsonGenerator writeStartArray(String name)
name - a name within the JSON name/array pair to be writtenJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within object contextJsonGenerator write(String name, JsonValue value)
name - a name in the JSON name/value pair to be written within
current JSON objectvalue - a value in the JSON name/value pair to be written within
current JSON objectJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within object contextJsonGenerator write(String name, String value)
name - a name in the JSON name/string pair to be written within
current JSON objectvalue - a value in the JSON name/string pair to be written within
current JSON objectJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within object contextJsonGenerator write(String name, BigInteger value)
new BigDecimal(value).toString()
is used as the text value for writing.name - a name in the JSON name/number pair to be written within
current JSON objectvalue - a value in the JSON name/number pair to be written within
current JSON objectJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within object contextJsonGenerator write(String name, BigDecimal value)
toString() is used as the text value for writing.name - a name in the JSON name/number pair to be written within
current JSON objectvalue - a value in the JSON name/number pair to be written within
current JSON objectJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within object contextJsonGenerator write(String name, int value)
new BigDecimal(value).toString() is used as the text value
for writing.name - a name in the JSON name/number pair to be written within
current JSON objectvalue - a value in the JSON name/number pair to be written within
current JSON objectJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within object contextJsonGenerator write(String name, long value)
new BigDecimal(value).toString() is used as the text
value for writing.name - a name in the JSON name/number pair to be written within
current JSON objectvalue - a value in the JSON name/number pair to be written within
current JSON objectJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within object contextJsonGenerator write(String name, double value)
BigDecimal.valueOf(double).toString()
is used as the text value for writing.name - a name in the JSON name/number pair to be written within
current JSON objectvalue - a value in the JSON name/number pair to be written within
current JSON objectJsonException - if an i/o error occurs (IOException
would be cause of JsonException)NumberFormatException - if value is Not-a-Number(NaN) or infinityJsonGenerationException - if not called within object contextJsonGenerator write(String name, boolean value)
name - a name in the JSON name/boolean pair to be written within
current JSON objectvalue - a value in the JSON name/boolean pair to be written within
current JSON objectJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within object contextJsonGenerator writeNull(String name)
name - a name in the JSON name/null pair to be written within
current JSON objectJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within object contextJsonGenerator writeEnd()
JsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if called in no contextJsonGenerator write(JsonValue value)
value - a value to be written within current JSON arrayJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within array contextJsonGenerator write(String value)
value - a value to be written within current JSON arrayJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within array contextJsonGenerator write(BigDecimal value)
toString()
is used as the the text value for writing.value - a value to be written within current JSON arrayJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within array contextJsonNumberJsonGenerator write(BigInteger value)
new BigDecimal(value).toString()
is used as the text value for writing.value - a value to be written within current JSON arrayJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within array contextJsonNumberJsonGenerator write(int value)
new BigDecimal(value).toString()
is used as the text value for writing.value - a value to be written within current JSON arrayJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within array contextJsonGenerator write(long value)
new BigDecimal(value).toString()
is used as the text value for writing.value - a value to be written within current JSON arrayJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within array contextJsonGenerator write(double value)
BigDecimal.valueOf(value).toString()
is used as the text value for writing.value - a value to be written within current JSON arrayJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within array contextNumberFormatException - if value is Not-a-Number(NaN) or infinityJsonGenerator write(boolean value)
value - a value to be written within current JSON arrayJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within array contextJsonGenerator writeNull()
JsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if not called within array contextvoid close()
close in interface AutoCloseableclose in interface CloseableJsonException - if an i/o error occurs (IOException
would be cause of JsonException)JsonGenerationException - if an incomplete JSON is generatedCopyright © 2013. All Rights Reserved.