Generating the object model from the JSON representation

The example in this section illustrates the usage of the JSR 353 object model APIs for building an object representation of the JSON data.

Downloading the example code

All the code examples that you see in this chapter are downloadable from the Packt website link mentioned at the beginning of this book, in the Preface section. In the downloaded source, open the rest-chapter2-jsonp folder to access all the examples discussed in this chapter.

This example uses the JSON array of the employee objects stored in the emp-array.json file as an input source. The contents of this file are listed under the Sample JSON file representing employee objects section. Let's see how we can convert the JSON content present in the file into a Java object model by using the JSR 353 object model API.

As you may have guessed, the first step is to read the JSON content from the emp-array.json file and store it in an appropriate data structure. The following code snippet illustrates the APIs for reading the JSON content from the file to javax.json.JsonArray:

//Import statements for the core classes 
import java.io.InputStream;
import javax.json.JsonArray;
import javax.json.JsonReader;

//Get input stream for reading the specified resource.
InputStream inputStream =
getClass().getResourceAsStream("/emp-array.json");
// Create JsonReader to read JSON data from a stream
Reader reader = new InputStreamReader(inputStream, "UTF-8");
JsonReader jsonReader = Json.createReader(reader);
// Creates an object model in memory.
JsonArray employeeArray = jsonReader.readArray();

Here is a brief description of the preceding code snippet. javax.json.Json is the factory class that we use for creating JSON processing objects. We will use this Json class to create the javax.json.JsonReader instance. The next step is to read the JSON content into an appropriate object model. As the JSON data that we use in this example holds an array of employee objects, we call readArray() on the JsonReader instance to retrieve javax.json.JsonArray. The JsonArray instance contains an ordered sequence of zero or more objects read from the input source.

If the input is a JSON object, you can call readObject() on JsonReader to retrieve the JSON object that is presented in the input source.

Now, let's see how to convert the JsonArray elements into specific object types. In this example, we will convert each JsonObject object present in employeeArray into the Employee object. The Employee class used in this example is shown here for your reference:

//All import statements are removed for brevity 
public class Employee {
private String firstName;
private String lastName;
private String email;
private Integer employeeId;
private java.util.Date hireDate;
//Getters and Setter for the above properties are not
//shown in this code snippet to save space
}
The Java EE 7 platform lacks a binding feature, which does the automatic conversion of the JSON content into Java classes. There is a proposal, JSR 367: JavaTM API for JSON Binding (JSON-B) as part of Java EE 8, to provide a standard binding layer for converting Java objects into/from JSON messages, which will be discussed in the later sections.

In this example, we create the Employee instance for each JsonObject object present in employeeArray.

The following code snippet is a continuation of the previous example. This example converts the employeeArray array that we retrieved from the JSON input file into a list of Employee objects:

//import statements for the core APIs used in  
//the following code snippet
import javax.json.JsonObject;
import javax.json.JsonValue;
import java.text.SimpleDateFormat;
import java.util.Date;

//Iterate over employeeArray(JsonArray)
//and process each JsonObject
List<Employee> employeeList = new ArrayList<Employee>();
for(JsonValue jsonValue : employeeArray) {
//Get JsonObject and read desired attributes
//and copy them to Employee object
if(JsonValue.ValueType.OBJECT == jsonValue.getValueType()){
JsonObject jsonObject = (JsonObject) jsonValue;
Employee employee = new Employee();
employee.setFirstName(jsonObject.getString("firstName"));
employee.setLastName(jsonObject.getString("lastName"));
employee.setEmployeeId(jsonObject.getInt("employeeId"));
//Converts date string (from JSON) to java.util.Date object
SimpleDateFormat dateFormat =
new SimpleDateFormat("yyyy-MM-dd");
Date hireDate=
dateFormat.parse(jsonObject.getString("hireDate"))
employee.setHireDate(hireDate);
employeeList.add(employee);
}
}

The preceding code iterates over the JsonArray instance and builds the Employee instances. Let's take a closer look at the JsonArray object to understand how it stores JSON data.

A JsonArray instance contains one or more javax.json.JsonValue elements, each representing an item present in the input source. Let's take a closer look at the representation of different value types in JSON.

..................Content has been hidden....................

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