The JSON-P model API allows us to generate an in-memory tree structured representation of a JSON object. The JSON-P API uses the builder pattern, which allows us as application developers to easily create a JSON representation of a Java object.
When using the JSON-P object model API, we typically start by invoking the add()
method of an implementation of the JsonObjectBuilder
interface. This method returns an instance of another JsonObjectBuilder
interface implementation. We can chain invocations of JsonObject.add()
together, allowing us to easily create a JSON representation from a Java object. The following example illustrates this process:
package com.ensode.jsonpmodelapi; //imports omitted @Named @RequestScoped public class JsonPModelApiBean { @Inject private Person person; private String jsonStr; public String generateJson() { JsonObjectBuilder jsonObjectBuilder = Json...