I just released a Javascript bridge for Codename One that allows you to communicate easily back and forth between Java and Javascript. This library may be absorbed by the Codename One core at some point in the future, but for now it is very easy to include it with your own projects.
There are countless ways that this Javascript bridge can be used to add value to Codename One applications. In this post, I will show how it can be used as an alternate way to consume JSON web services.
Codename One already includes a JSONParser class that allows you to, quite easily, consume a web service and parse its output into a tree of Hashtables and Vectors (I will demonstrate this method below). So why use the Javascript bridge to solve an already-solved problem? The answer is: Just to prove that it can be done.
Method 1: Load data using ConnectionRequest, and parse data into Hashtables and Vectors using the JSONParser class
E.g. Loading JSON feed from Youtube for Most Popular Videos
The benefit of this method is that it is a 100% Java solution that should work the same across all platforms. It should also be very fast and efficient.
Note: Codename One offers a Result class that makes it much easier to query the result of the JSON parser. Read more about it in Eric Coolman’s blog post.
The amount of code required to consume and parse the request is not really relevant because it can all be wrapped in a single method with a callback for the result.
Method 2: Load data using WebBrowser and use Javascript’s JSON.parse() function to produce a JSObject
E.g. Loading JSON feed from Youtube for Most Popular Videos
The benefit of this method is that it is easier to obtain nested content stored in a JSObject than in a tree of Hashtables and Vectors. E.g. The same example as above (to obtain the make of a car in the result set) could be carried out on 2 lines:
JSObject response = (JSObject)context.get("JSON.parse(document.body.textContent)"); String make = response.getString("people[0].car.type.make.name");
Method 3: Pass a JSON String into JavascriptContext.get() to obtain a JSObject
E.g. Loading JSON feed from Youtube for Most Popular Videos
This is really just a hybrid of the two approaches, and it is useful if we’ve already loaded the JSON data and have it in a String. E.g. perhaps we used ConnectionRequest to load data from a Web Service, and some of the Data is JSON and some of it is another format. In any case, this method assumes that we have a String of JSON data and we want to turn it into a JSON object so that we can work with it.
Then we can pass the string directly to JavascriptContext.get(), and the WebBrowser component will handle all of the parsing.
JSObject response = (JSObject)context.get(jsonString); String make = response.getString("people[0].car.type.make.name");
Method 4: Load data with the WebBrowser and parse it using JSONParser
E.g. Loading JSON feed from Youtube for Most Popular Videos
This is really the inverse approach of method 3 above. In this case we load JSON data in the WebBrowser, retrieve it as a string using JavascriptContext.get(), and then parse it using the JSONParser class.