Using streaming APIs to parse JSON data

In this section, you will learn the use of streaming APIs for converting JSON data into appropriate Java classes.

This example illustrates the use of streaming APIs for converting a JSON array of the employee objects present in the emp-array.json file into an appropriate Java class representation.

The following code snippet illustrates how you can use javax.json.stream.JsonParser, which follows the streaming parsing model, to read the content from the emp-array.json file:

  1. The first step is to get the input stream for reading the emp-array.json file. Then, you can create a JsonParser instance with the input stream, as follows:
//Other imports are removed for brevity 
import javax.json.stream.JsonParser;

//Read emp-array.json file that contains JSON array of
//employees
//This file is listed under the section:
//"A sample JSON file representing employee objects"
InputStream inputStream =
getClass().getResourceAsStream("/emp-array.json");
JsonParser jsonParser = Json.createParser(inputStream);
  1. Start parsing the content now. The following code snippet illustrates the API use for parsing the content with the pull parsing model:
// Returns true if there are more parsing states 
while (jsonParser.hasNext()) {
//Returns the event for the next parsing state
Event event = jsonParser.next();
//Start of a JSON object,
//position of the parser is after'{'
if (event.equals(JsonParser.Event.START_OBJECT)) {
employee = new Employee();
employeeList.add(employee);
} else if (event.equals(JsonParser.Event.KEY_NAME)) {
String keyName = jsonParser.getString();
switch (keyName) {
case "firstName":
jsonParser.next();
employee.setFirstName(
jsonParser.getString());
break;
case "lastName":
jsonParser.next();
employee.setLastName(
jsonParser.getString());
break;
case "email":
jsonParser.next();
employee.setEmail(jsonParser.getString());
break;
case "employeeId":
jsonParser.next();
employee.setEmployeeId(jsonParser.getInt());
break;
case "hireDate":
jsonParser.next();
//Converts date string (from JSON) into
//java.util.Date object
SimpleDateFormat dateFormat =
new SimpleDateFormat("yyyy-MM-dd");
Date hireDate=
dateFormat.Parse
(jsonObject.getString("hireDate"));
employee.setHireDate(hireDate);
break;
default:
}
}

}

Remember that JsonParser parses JSON by using the pull parsing programming model. In this case, the client application code controls the progress of parsing. The client calls next() on JsonParser to advance the parser to the next state after processing each element. In response to the next() call from the client, the parser generates the following events on the basis of the type of the next token encountered: START_OBJECT, END_OBJECT, START_ARRAY, END_ARRAY, KEY_NAME, VALUE_STRING, VALUE_NUMBER, VALUE_TRUE, VALUE_FALSE, and VALUE_NULL.

To better understand this, consider the following JSON array as the input to the parser:

[{"country": "IN"}] 

The parser generates the START_ARRAY event for the first call to the next() method and the START_OBJECT event with the second call to the next() method, and so on. The following diagram illustrates the events generated while parsing each token present in the JSON representation:

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

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