[go: up one dir, main page]

0% found this document useful (0 votes)
12 views18 pages

CHAPTER 4B JSON Procesing - 855a2591a

Uploaded by

kefon41944
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views18 pages

CHAPTER 4B JSON Procesing - 855a2591a

Uploaded by

kefon41944
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

CHAPTER IV B: Java APIs for JSON

Processing
Outline:

 A brief overview of JSON


 Using the JSR 353—Java API for processing
JSON

chapter 4 REST - JSON 1


JSON data syntax
• It is represented by the following two data structures
• An unordered collection of name-value pairs (representing an object):
• The attributes of an object and their values are represented in the name-value
pair format; the name and the value in a pair is separated by a colon (:).
• Names in an object are strings, and values may be of any of the valid JSON
data types such as number, string, Boolean, array, object, or null.
• Each name:value pair in a JSON object is separated by a comma (,).
• The entire object is enclosed in curly braces ({ }).
• For instance, the JSON representation of a department object is as follows:

{"departmentId":10, "departmentName":"IT", "manager":"John Chen"}

chapter 4 REST - JSON 2


JSON data syntax …
• An ordered collection of values (representing an array):
• Arrays are enclosed in square brackets ([ ]), and their values are
separated by a comma (,).
• Each value in an array may be of a different type, including
another array or an object.
• Example : employees working in a department.

chapter 4 REST - JSON 3


Json processing Example :emp-
array.json

chapter 4 REST - JSON 4


Processing JSON data
• Role of the JSON marshalling and unmarshalling components in
a typical Java RESTful web service implementation:

We will
implement
spring boot
REST
chapter 4 REST - JSON 5
Processing models for JSON data
Processing : reading, writing, querying, and modifying
JSON data
A. Object model:
the entire JSON data is read into memory in a tree format.
This tree can be traversed, analyzed, or modif ie d with the
appropriate APIs.
As this approach loads the entire content into the memory
f irst and then starts parsing, it ends up consuming more
memory and CPU cycles.
H o w e v e r, t h i s m o d e l g i v e s m o r e f le x i b i l i t y w h i l e
manipulating the content.

chapter 4 REST - JSON 6


B. Streaming model:
 that data can be read or written in blocks.
 This model does not read the entire JSON content into the memory to get
started with parsing; rather, it reads one element at a time.
 For each token read, the parser generates appropriate events indicating
the type of token, such as the start or end of an array, or the start or end of
the object and attribute values.
 A client can process the contents by listening for appropriate events.
 The most important point is that instead of letting the parser push the
content to the client (push parser), the client can pull the information from
the parser as it needs (pull parser).
 In this model, the client is allowed to skip or stop reading contents in the
middle of the process if it has finished reading the desired elements.
 This model is also useful when you write contents to an output source in
blocks.

chapter 4 REST - JSON 7


B. Streaming model when?
• When the data is huge in size and it is not feasible to load the
entire content into the memory for processing the content
• When the partial processing is needed and the data model is not
fully available yet

chapter 4 REST - JSON 8


Using JSR 353 – Java API for processing
JSON
• Object model API -> Will be covered in class
• Streaming model API -> Home reading

chapter 4 REST - JSON 9


Processing JSON with JSR 353 object
model APIs : common classes

chapter 4 REST - JSON 10


Common classes in JSR 353 object
model APIs

chapter 4 REST - JSON 11


Generating the object model from the
JSON representation
• Example uses the JSON array of the employee objects stored in
the emp-array. json file as an input source.
• The contents of this f ile are listed under the A sample JSON f ile
representing employee objects section.
• Let’s convert the JSON content present in the f il e into a Java
object model

chapter 4 REST - JSON 12


Step 1: Read the JSON content from the emp-
array.json file and store it in an appropriate data
structure. 

chapter 4 REST - JSON 13


Convert JsonArray elements into specific
object types.

chapter 4 REST - JSON 14


Let’s create the Employee instance for each
JsonObject present in employeeArray.continue
code from slide 13
• 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

chapter 4 REST - JSON 15


Generating the JSON representation from the
object model
• You can use either of the following classes to generate the
object model.
• javax.json.JsonObjectBuilder: This builder class is used for
generating the JSON object model from scratch. This class
provides methods to add the name-value pairs to the object
model and to return the final object.
• javax.json.JsonArrayBuilder: This builder class is used for
generating an array of the JSON objects from scratch. This class
provides methods to add objects or values to the array model
and to return the final array.

chapter 4 REST - JSON 16


// Creates a
JsonArrayBuilder instance

Output:

chapter 4 REST - JSON 17


Home reading:
Processing JSON with JSR 353
streaming APIs

chapter 4 REST - JSON 18

You might also like