Java Reference
In-Depth Information
public void destroy() {
throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
As mentioned previously, the encoder and decoder classes can be registered with a WebSocket endpoint by
specifying the encoders and decoders attributes, respectively. The following example demonstrates how to do so for
registering the example classes to the AcmeChatEndpoint WebSocket endpoint:
@ServerEndpoint(value="/acmechat", encoders=AcmeChatTextEncoder.class,
JavaScript Object Notation—JSON
The universally supported JSON (JavaScript Object Notation) object has become a widely adopted solution for
sending data between web communication endpoints. HTML5-based web applications can utilize JSON to transport
data, using WebSockets, Ajax, or other transport technologies. The Java EE 7 platform provides the JSON-P API, which
introduces utilities that make it easier to build and work with JSON objects within the Java language. JSON-P, also
referred to as “JSON with padding,” has become the standard way to build JSON objects using Java.
This section will delve into the JSON-P API, which is part of the Java EE 7 platform. You will learn how to build
a JSON file using the API, read JSON, and parse JSON. These are barebones steps to producing data sets with JSON,
and they should get you on your way to building applications with JSON and HTML5! To see the complete Javadoc
for JSON, please refer to the online documentation at:
package-frame.html .
Building JSON
The JSON-P API includes a helper class that can be used to create JSON objects using the builder pattern. Using the
JsonBuilder class, JSON objects can be built using a series of method calls, each building upon each other, hence,
the builder pattern. Once the JSON object has been built, the JsonBuilder build method can be called to return
a JsonObject .
In the example in this section, we construct a JSON object that provides details regarding a book. The
JsonBuilder.add() method can be called to return a JsonObjectBuilder , which is a building block of a JSON object.
The add method is used to build components to the object, including name/value properties, much like that of a Map.
Therefore, the following line adds a property named title with a value of Intro to Java EE 7 :
.add("title", "Intro to Java EE 7")
Objects can be embedded inside of one another, creating a hierarchy of different sections within one JsonObject .
In the example that follows, the first call to add initiates the build of an embedded JsonObjectBuilder , 'jsonJob' ,
within the JsonObjectBuilder in the example, providing an object name for the embedded structure, jsonJob .
Subsequent calls to add build sub-objects inside of the initial JsonObject by calling, passing the name/value of the
embedded object. Embedded objects can also contain properties, so to add properties to the embedded object, call
the add() method within the embedded object, nesting to as many levels as needed. JsonObject s can embody as
Search WWH ::

Custom Search