The JSON-P Streaming API allows sequential reading of a JSON object from a stream (a subclass of java.io.OutputStream
or a subclass of java.io.Writer
). It is faster and more memory efficient than the Model API, however, the trade-off is that it is more limited, since the JSON data needs to be read sequentially and we cannot access specific JSON properties directly the way the Model API allows.
The JSON Streaming API has a JsonGenerator
class that we can use to generate JSON data and write it to a stream. This class has several overloaded write()
methods, which can be used to add properties and their corresponding values to the generated JSON data.
The following code sample illustrates how to generate JSON data using the Streaming API:
package net.ensode.javaee8book.jsonpstreaming; //other imports omitted import javax.json.Json; import javax.json.stream.JsonGenerator; import javax.json.stream.JsonParser...