Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tools of the trade J SON, M AVEN, A PACHE COMMON Photo from

Similar presentations


Presentation on theme: "Tools of the trade J SON, M AVEN, A PACHE COMMON Photo from"— Presentation transcript:

1 Tools of the trade J SON, M AVEN, A PACHE COMMON Photo from http://www.flickr.com/photos/mtsofan//

2 JSON  JSON is an alternative to XML for transmitting data  JSON is an increasingly popular because it is more concise and therefore faster to process and deliver.  JSON is tightly integrated with JavaScript. JSON stands for JavaScript Object Notation.  Uses a compact object notation that is similar to but not identical to JavaScript object literals.  The syntax is extremely simple and flexible. 2

3 JSON  There are only several 'kinds' of things in JSON  Numbers 1.2, 3, -12  Booleans true, false  Strings "this is a string"  Objects { … something goes here… }  Lists [ items are listed here separated by commas] 3

4 JSON 4 [ { "first_name" : "Aaron", "last_name" : "Rogers", "pro_bowler" : "true", "rating" : "112.3" }, { "first_name" : "Adam", "last_name" : "Sandler", "pro_bowler" : "false", "rating" : "3" } ] [ { "first_name" : "Aaron", "last_name" : "Rogers", "pro_bowler" : "true", "rating" : "112.3" }, { "first_name" : "Adam", "last_name" : "Sandler", "pro_bowler" : "false", "rating" : "3" } ] { "name" : "Packers", "players" : [ { "first_name" : "Aaron", "last_name" : "Rogers", "pro_bowler" : "true", "rating" : "112.3" }, { "first_name" : "Adam", "last_name" : "Sandler", "pro_bowler" : "false", "rating" : "3" } ], "wins" : "5", "losses" : "2" } { "name" : "Packers", "players" : [ { "first_name" : "Aaron", "last_name" : "Rogers", "pro_bowler" : "true", "rating" : "112.3" }, { "first_name" : "Adam", "last_name" : "Sandler", "pro_bowler" : "false", "rating" : "3" } ], "wins" : "5", "losses" : "2" }

5 W HY JSON?  JSON is useful for AJAX calls.  HTML documents often contain JavaScript functions that get data from servers. A user presses a button (get information about something) A JavaScript function requests data from a server The data is returned as JSON The JavaScript function uses the data to generate an HTML fragment that is inserted into the document The HTML document is not reloaded; but rather a small portion is modified. 5

6 H ANDLING JSON  JavaScript just "works" with JSON.  var player = {"name" : "Rogers", "rating", "112.3" };  var n = player.name;  var rating = player.rating;  Java does not just "work".  String json = "{\"name\":\"Rogers\",\"rating\",\"112.3\"}";  Player p = ?  There are really just two main challenges to processing JSON with Java  Serializing : converting a Java object to JSON  Deserializing : converting JSON to a Java object. 6

7 F LEX JSON  A JSON processing library that provides two main services: serializing and deserializing  Consider an example where we are writing Java to process the following classes. 7 Parts of this tutorial from http://flexjson.sourceforge.net/

8 F LEX JSON E XAMPLE  The code below shows how to serialize an object and the result. 8 public String lookupPerson(Long id) { Person p = entityManager.findPerson(id); return new JSONSerializer.serialize(p); } public String lookupPerson(Long id) { Person p = entityManager.findPerson(id); return new JSONSerializer.serialize(p); }

9 I NCLUDES  Note that the result did not have entries for phoneNumbers or addresses. This is because the serializer only serializes the non-collection fields.  You can force serialization of collections by using the include method. 9

10 10

11 F LEX JSON E XAMPLE  Include is a variable-argument method. You can include as many elements as you would like in one call.  Note that "addresses.zipcode" means the zipcode of all address objects in the list. 11

12 F LEX JSON E XAMPLE  Excludes work like includes. You can specifically prevent a field from appearing in the generated JSON.  Include can take wildcards. Also, the order of includes is semantically meaningful.  "*" means "everything"  "*.class" means "any class field".  For each field, the includes are evaluated to answer the question: should it be serialized. That question is answered by the first applicable include or exclude. 12

13 D EEP F LEX  The serializer class has a deepSerialize method that sends the entire object graph to JSON. This is essentially 'include everything'. 13

14 D ESERIALIZE  This is more complicated since JSON has no typing information.  Serializing takes java objects (for which the class is known) and serializes the object.  Deserializing requires that flex knows what classes to generate from the un-typed JSON. 14

15 D ESERIALIZE  If the JSON comes with "class" fields then things are easy.  Person p = new JSONDeserializer ().deserialize(json);  For this code to work, the json string must have class fields within it.  If the JSON doesn't come with class fields  Person p = new JSONDeserializer ().deserialize(json, Person.class);  If you have a collection (interface) of items.  List pList = new JSONDeserialize >().use("values", Person.class).deserialize(json, ArrayList.class) 15

16 E XAMPLE  Write a service that pulls information from other services.  Domain.nr is a web service. You enter a domain name and it will generate permutations. Some of these may be available and others not.  https://domai.nr/api/docs/json  To process with a Java Servlet, you should  Write class(es) that correspond to the JSON api.  Write other class(es) that correspond to the JSON that your service will produce.  Write a mapper that converts between the two types of classes. 16

17 M AVEN  Maven is a build-management tool.  It is often used for dependency management (among many other things).  If you use a 3 rd party library you must either  Download jar files and make sure they are copied into the build path  Declare a dependency in a "pom.xml" file. This file configures Maven for your project. Maven will download any declared dependencies and ensure that they are available in the build path. 17

18 H TTP C LIENT  The Apache Software Foundation produces high-quality software. We will use numerous libraries from Apache throughout this course.  The HttpClient package is a Java library for pulling data from servers.  Just a set of Java classes  Fully supports all of HTTP (GET, POST, PUT, DELETE,…)  Supports encryption  Good utilities for caching and io management. 18

19 H TTP C LIENT  The most basic function of HttpClient is to execute HTTP methods.  Execution involves one or several HTTP request / HTTP response exchanges (these are transparent).  Clients provide a request object to execute. The HttpClient then does the following: Transmits the request to the target server Returns a corresponding response object May alternately throw an exception if execution 19

20 H TTP C LIENT 20

21  HTTP messages may carry a content entity.  Requests that use entities are referred to as entity enclosing requests. The HTTP specification defines two entity enclosing request methods: POST and PUT.  Responses are usually expected to enclose a content entity.  The entity is the http body. 21

22 H TTP C LIENT  The recommended way to consume an entity is by using  HttpEntity#getContent()  HttpEntity#writeTo(OutputStream) 22


Download ppt "Tools of the trade J SON, M AVEN, A PACHE COMMON Photo from"

Similar presentations


Ads by Google