The JSON-P Model API allows us to generate an in-memory representation of a JSON object. This API is more flexible than the Streaming API discussed later in this chapter, however, it is slower and requires more memory, which can be a concern when handling large volumes of data.
At the heart of the JSON-P Model API is the JsonObjectBuilder
class. This class has several overloaded add()
methods, which can be used to add properties and their corresponding values to generated JSON data.
The following code sample illustrates how to generate JSON data using the Model API:
package net.ensode.javaee8book.jsonpobject; //other imports omitted for brevity. import javax.inject.Named; import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonReader; import javax.json.JsonWriter; @Named @SessionScoped public class JsonpBean implements Serializable{ private String jsonStr...