Creating JSON

In this section, we will create a Java object model for JSON and output the JSON to a file. We will use the Java API for JSON processing to create the following JSON structure in createJson.jsp:

{
"journal":"Oracle Magazine",
"edition":
{"date":"March April 2013","cover":"Public Cloud. Private Cloud"},
"catalog":
[
{"title":"PaaS Fits the Enterprise","author":"David Baum"},
{"title":"On Becoming Others, Limits, and Restoration","author":"Tom Kyte"}
]
}

A JSON Java object model is represented by the JsonObject class. The JsonObjectBuilder interface can be used to initialize a JSON object model and create a JSON object. First, create a JsonObjectBuilder object using createObjectBuilder(), the Json class static method, as follows:

JsonObjectBuilder builder = Json.createObjectBuilder();

JsonObjectBuilder provides the overloaded add() method to add name/value pairs of different data types to the JSON object model. Add a name/value pair for "journal", as follows:

builder=builder.add("journal", "Oracle Magazine");

To create a hierarchy of JSON Java object model structures, invoke Json.createObjectBuilder() for each of the substructures. Add the "edition" JSON object model, as follows:

builder=builder.add("edition", Json.createObjectBuilder().add("date","March April 2013").add("cover", "Public Cloud. Private Cloud"));

A JSON array can be added using the static method createArrayBuilder(), which returns a JSON array builder JsonArrayBuilder object, from the Json class. To build the JSON array, invoke Json.createObjectBuilder() for each JSON object model substructure, as follows:

builder=builder.add("catalog", Json.createArrayBuilder().add(Json.createObjectBuilder(). add("title", "PaaS Fits the Enterprise").add("author","David Baum")).add(Json.createObjectBuilder().add("title","On Becoming Others, Limits, and Restoration").add("author","Tom Kyte")));

Create a JsonObject object from the JsonObjectBuilder object using the build() method:

JsonObject value = builder.build();

The JsonWriter class is used to output a JSON object or array. Create a JsonWriter object to output to the jsonOutput.txt file, as follows:

JsonWriter jsonWriter= Json.createWriter(new FileOutputStream(new File("C:/json/jsonOutput.txt")));

Output the JsonObject object using the writeObject method, as follows:

jsonWriter.writeObject(value);

An alternative method to create JsonObjectBuilder is to use the JsonBuilderFactory interface, which is suitable if multiple instances of the JsonObjectBuilder object are required. A JsonBuilderFactory object is created using createBuilderFactory(), the Json class static method. Subsequently, invoke the createObjectBuilder() method of the JsonBuilderFactory object to create a JsonObjectBuilder object:

JsonBuilderFactory factory = Json.createBuilderFactory(null);
JsonObjectBuilder builder = factory.createObjectBuilder();

An alternative to creating a JsonWriter object is the JsonWriterFactory interface, which is suitable if multiple JsonWriter objects are required. Create a JsonWriterFactory object using the static method createWriterFactory(), and subsequently, create a JsonWriter object using the factory method createWriter():

JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(null);
JsonWriter jsonWriter= jsonWriterFactory.createWriter(new FileOutputStream(new File("C:/json/jsonOutput.txt")));

The JsonGenerator interface is provided to generate JSON in a streaming way. A JsonGenerator object can be created using the Json class static method createGenerator() or from a JsonGeneratorFactory factory, which can be created using the Json class static method createGeneratorFactory(). The two modes of creating a JsonGenerator object are:

JsonGenerator generator = Json.createGenerator(new FileOutputStream(new File("C:/json/jsonOutput.txt")));

JsonGeneratorFactory factory = Json.createGeneratorFactory(null);
JsonGenerator generator = factory.createGenerator(new FileOutputStream(new File("C:/json/jsonOutput.txt")));

JsonGenerator provides several methods to generate the JsonObject and JsonArray name/value pairs. The writeStartObject() method starts a JsonObject object model. The writeStartArray() method starts a JsonArray object model. The corresponding method to end a JsonObject or JsonArray object model is writeEnd(). The generator methods can be invoked in a sequence to generate a complete JSON object model:

generator
.writeStartObject()
.write("journal", "Oracle Magazine")
.writeStartObject("edition")
.write("date", "March April 2013")
.write("cover", "Public Cloud. Private Cloud")
         .writeEnd()
         .writeStartArray("catalog")
             .writeStartObject()
                 .write("title", "PaaS Fits the Enterprise")
                 .write("author", "David Baum")
             .writeEnd()
             .writeStartObject()
                 .write("title", "On Becoming Others, Limits, and Restoration")
                 .write("author", "Tom Kyte")
             .writeEnd()
         .writeEnd()
     .writeEnd();

The createJson.jsp fileis listed as follows:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
<%@ page import="javax.json.*, java.io.*,javax.json.stream.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>JSON Array</title>
    </head>
    <body>
        <%
                JsonObjectBuilder builder = Json.createObjectBuilder();
                builder=builder.add("journal", "Oracle Magazine");

                builder=builder.add("edition", Json.createObjectBuilder().add("date","March April 2013").add("cover", "Public Cloud. Private Cloud"));
                builder=builder.add("catalog", Json.createArrayBuilder() .add(Json.createObjectBuilder() .add("title","PaaS Fits the Enterprise") .add("author","David Baum")) .add(Json.createObjectBuilder() .add("title","On Becoming Others, Limits, and Restoration").add("author","Tom Kyte")));
                
                JsonObject value = builder.build();
                JsonWriter jsonWriter= Json.createWriter(new FileOutputStream(new File("C:/json/jsonOutput.txt")));
                jsonWriter.writeObject(value); 
                jsonWriter.close();
                out.println("JSON Array output to jsonOutput.txt");
        
     /**    JsonBuilderFactory factory = Json.createBuilderFactory(null);
         JsonObjectBuilder builder = factory.createObjectBuilder();
                builder=builder.add("journal", "Oracle Magazine");

                builder=builder.add("edition", factory.createObjectBuilder().add("date","March April 2013").add("cover", "Public Cloud. Private Cloud"));
                builder=builder.add("catalog", factory.createArrayBuilder() .add(factory.createObjectBuilder() .add("title","PaaS Fits the Enterprise").add("author","David Baum")) .add(factory.createObjectBuilder() .add("title","On Becoming Others, Limits, and Restoration").add("author","Tom Kyte")));
                
                JsonObject value = builder.build();
                JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(null);
                JsonWriter jsonWriter= jsonWriterFactory.createWriter(new FileOutputStream(new File("C:/json/jsonOutput.txt")));
                jsonWriter.writeObject(value); 
                jsonWriter.close();
                out.println("JSON Array output to jsonOutput.txt");
*/
        
    /**    JsonGeneratorFactory factory = Json.createGeneratorFactory(null);
        JsonGenerator generator = factory.createGenerator(new FileOutputStream(new File("C:/json/jsonOutput.txt")));
  
       //  JsonGenerator generator = Json.createGenerator(new FileOutputStream(new File("C:/json/jsonOutput.txt")));
         generator
     .writeStartObject()
         .write("journal", "Oracle Magazine")
         .writeStartObject("edition")
             .write("date", "March April 2013")
             .write("cover", "Public Cloud. Private Cloud")
         .writeEnd()
         .writeStartArray("catalog")
             .writeStartObject()
                 .write("title", "PaaS Fits the Enterprise")
                 .write("author", "David Baum")
             .writeEnd()
             .writeStartObject()
                 .write("title", "On Becoming Others, Limits, and Restoration")
                 .write("author", "Tom Kyte")
             .writeEnd()
         .writeEnd()
     .writeEnd();
 generator.close();
  out.println("JSON Array output to jsonOutput.txt");*/
        %>
    </body>
</html>

To run createJson.jsp, first build and install the Maven project. Right-click on the project node in Package Explorer and select Run As | Maven install, as shown in the following screenshot. All the JSPs in the chapter can be built together; for retesting, the Maven project would need to be rebuilt.

Creating JSON

The JSON web application gets deployed to WildFly 8.1. The Maven build outputs the message BUILD SUCCESS, as shown here:

Creating JSON

Start WildFly Administration Console with the URL http://localhost:8080. Now, click on Administration Console. Specify the login Name and Password to log in to Administration Console. Click on Manage Deployments. The json.war file is listed and deployed, as shown here:

Creating JSON

Run createJson.jsp on the WildFly server with the URL http://localhost:8080/json-1.0/createJson.jsp , as shown in the following screenshot. The createJson.jsp yes runs on the server to output the JSON Java object model to C:/json/jsonOutput.txt file.

Creating JSON

The JSON output is shown here:

{"journal":"Oracle Magazine","edition":{"date":"March April 2013","cover":"Public Cloud. Private Cloud"},"catalog":[{"title":"PaaS Fits the Enterprise","author":"David Baum"},{"title":"On Becoming Others, Limits, and Restoration","author":"Tom Kyte"}]} 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset