Java Reference
In-Depth Information
To generate JSON data using the JSON-P streaming API, first we need to invoke
a call to the static Json.createGenerator() method. This method returns an
instance of a class implementing javax.json.stream.JsonGenerator . There are
two overloaded versions of the Json.createGenerator() method, one takes an
instance of java.io.OutputStream (or one of its subclasses) as a parameter and
the other one takes an instance of java.io.Writer (or one of its subclasses) as a
parameter. In our example we chose the second version, passing an instance of
java.io.StringWriter to Json.createGenerator() .
Once we obtain an instance of JsonGenerator , we need to invoke the
writeStartObject() method on it. This method writes the start object character
of JSON (the opening curly brace { ) to the OutputStream or Writer we passed to
Json.createGenerator() . The writeStartObject() method returns another
instance of JsonGenerator , allowing us to immediately invoke the write()
method on the resulting JsonGenerator .
The write() method of JsonGenerator adds a JSON property to our JSON data. Its
first parameter is a string containing the property value, and its second parameter
is the corresponding value. There are several overloaded versions of the write()
method, one for each corresponding supported JSON value type (either String or a
numeric type such as BigInteger or double ). In our example, we are only adding
properties of the type String and Integer , therefore we used the corresponding
versions of the write() methods. The following table lists all of the existing versions
of the write() method:
write() method
Description
write(String name,
BigDecimal value)
Adds a numeric property of type BigDecimal to our
JSON data
write(String name,
BigInteger value)
Adds a numeric property of type BigInteger to our
JSON data
write(String name,
JsonValue value)
Adds a property of type JsonValue or one of its
subinterfaces ( JsonArray , JsonNumber , JsonObject ,
JsonString or JsonStructure ) to our JSON data
write(String name,
String value)
Adds a property of type String to our JSON data
write(String name,
boolean value)
Adds a boolean property to our JSON data
write(String name,
double value)
Adds a numeric property of type double to our JSON data
write(String name,
int value)
Writes a numeric property of type int to our JSON data
write(String name,
long value)
Writes a numeric property of type long to our JSON data
Search WWH ::




Custom Search