Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
43 views

Java API For JSON Processing (JSON-P)

The document summarizes a Java programming course on JSON processing. It introduces JSON and its structure, and describes the Java API for JSON processing, which includes both an object model API and a streaming API. The object model API creates a tree structure to represent JSON data in memory, while the streaming API parses and generates JSON in a streaming fashion using events.

Uploaded by

Sơn Phạm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Java API For JSON Processing (JSON-P)

The document summarizes a Java programming course on JSON processing. It introduces JSON and its structure, and describes the Java API for JSON processing, which includes both an object model API and a streaming API. The object model API creates a tree structure to represent JSON data in memory, while the streaming API parses and generates JSON in a streaming fashion using events.

Uploaded by

Sơn Phạm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Java Programming Course

JSON Processing with Java

Faculty of Information Technologies


Industrial University of Ho Chi Minh City
Session objectives

JSON Introduction
JSON structure
Java API for JSON Processing

2
JSON Introduction
http://www.json.org/

• JSON (JavaScript Object Notation) is a lightweight data-interchange


format.
o It is easy for humans to read and write.
o It is easy for machines to parse and generate.
o It is based on a subset of the JavaScript Programming Language, Standard
ECMA-262 3rd Edition - December 1999.
o JSON is a text format that is completely language independent but uses
conventions that are familiar to programmers of the C-family of languages,
including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These
properties make JSON an ideal data-interchange language.
• JSON is often used in Ajax applications, configurations, databases,
and RESTful web services. All popular websites offer JSON as the
data exchange format with their RESTful web services.

3
JSON structure (1)

• JSON is built on two structures:


o A collection of name/value pairs. In various languages, this is realized as
an object, record, struct, dictionary, hash table, keyed list, or
associative array.
o An ordered list of values. In most languages, this is realized as an array,
vector, list, or sequence.

4
JSON structure (2)

• In JSON, they take on these forms:


o An object is an unordered set of name/value pairs. An object begins
with { (left brace) and ends with } (right brace). Each name is followed
by : (colon) and the name/value pairs are separated by , (comma).

o An array is an ordered collection of values. An array begins with [ (left


bracket) and ends with ] (right bracket). Values are separated by
, (comma).

5
JSON structure (3)

• A value can be a string in double quotes, or a number, or true or


false or null, or an object or an array. These structures can be
nested.

6
JSON structure (4)

• A string is a sequence of zero or more Unicode characters,


wrapped in double quotes, using backslash escapes. A character is
represented as a single character string. A string is very much like
a C or Java string.

7
JSON structure (5)

• A number is very much like a C or Java number, except that the


octal and hexadecimal formats are not used.

8
Sample json document & rule

9
Java API for JSON Processing

• The Java API for JSON Processing (JSR 353) provides portable
APIs to parse, generate, transform, and query JSON using object
model and streaming APIs.
• It produces and consumes JSON text in a streaming fashion
(similar to StAX API for XML) and allows to build a Java object
model for JSON text using API classes (similar to DOM API for
XML).

10
JSON Processing - The Object Model API (1)

• The Object Model API


o The object model API creates a random-access, tree-like structure that
represents the JSON data in memory. The tree can then be navigated and
queried.
o This programming model is the most flexible and enables processing that
requires random access to the complete contents of the tree. However, it is
often not as efficient as the streaming model and requires more memory.
o The object model API is similar to the Document Object Model (DOM) API
for XML.
o It is a high-level API that provides immutable object models for JSON
object and array structures. These JSON structures are represented as
object models using the Java types JsonObject and JsonArray.

11
JSON Processing - The Object Model API (2)

• The main classes and interfaces in the object model API


Class or Interface Description
Contains static methods to create JSON readers, writers,
Json
builders, and their factory objects.
JsonGenerator Writes JSON data to a stream one value at a time.
Reads JSON data from a stream and creates an object
JsonReader
model in memory.
JsonObjectBuilder Create an object model or an array model in memory by
JsonArrayBuilder adding values from application code.
JsonWriter Writes an object model from memory to a stream.
JsonValue
JsonObject
JsonArray Represent data types for values in JSON data.
JsonString
JsonNumber

12
JSON Processing - The Object Model API (3)

• JsonObject, JsonArray, JsonString, and JsonNumber are subtypes of


JsonValue. These are constants defined in the API for null, true, and false
JSON values.
• The object model API uses builder patterns to create these object models
from scratch. Application code can use the interface JsonObjectBuilder to
create models that represent JSON objects. The resulting model is of type
JsonObject. Application code can use the interface JsonArrayBuilder to
create models that represent JSON arrays. The resulting model is of type
JsonArray.
• These object models can also be created from an input source (such as
InputStream or Reader) using the interface JsonReader. Similarly, these
object models can be written to an output source (such as OutputStream or
Writer) using the class JsonWriter.

13
Mapping between JSON and Java entities

On decoding:
The default concrete class of java.util.List is org.json.simple.JSONArray
The default concrete class of java.util.Map is org.json.simple.JSONObject.

14
Encoding JSON in Java

15
Decoding JSON in Java

16
JSON Processing - The Streaming API (1)

• The Streaming API


o The streaming API provides a way to parse and generate JSON in a
streaming fashion.
o It hands over parsing and generation control to the programmer.
o The streaming API provides an event-based parser and allows an
application developer to ask for the next event rather than handling the
event in a callback. This gives a developer more procedural control over
the JSON processing. Application code can process or discard the
parser event and ask for the next event (pull the event).

17
JSON Processing - The Streaming API (2)

• The streaming API is similar to the Streaming API for XML


(StAX) and consists of the interfaces JsonParser and
JsonGenerator.
• JsonParser contains methods to parse JSON data using the
streaming model.
• JsonGenerator contains methods to write JSON data to an output
source. Table 2 lists the main classes and interfaces in the
streaming API.

18
JSON Processing - The Streaming API (3)

• The main classes and interfaces in the streaming API


Class or Interface Description
Contains static methods to create JSON parsers,
Json
generators, and their factory objects.

Represents an event-based parser that can read JSON data


JsonParser
from a stream.
JsonGenerator Writes JSON data to a stream one value at a time

19
JSON Processing - The Streaming API (3)

• JsonParser provides forward, read-only access to JSON data using


the pull parsing programming model. In this model, the application code
controls the thread and calls methods in the parser interface to move
the parser forward or to obtain JSON data from the current state of
the parser.
• JsonGenerator provides methods to write JSON data to a stream. The
generator can be used to write name/value pairs in JSON objects and
values in JSON arrays.
• The streaming API is a low-level API designed to process large
amounts of JSON data efficiently. Other JSON frameworks (such as
JSON binding) can be implemented using this API.

20
Decoding JSON in Java – Stream API

21
Conclusion

• The Java API for JSON Processing provides the following


capabilities:
o Parsing input streams into immutable objects or event streams
o Writing event streams or immutable objects to output streams
o Programmatically navigating immutable objects
o Programmatically building immutable objects with builders

22
FAQ

23
That’s all for this session!

Thank you all for your attention and patient !

24
24/27

You might also like