In this post, I will try to walk through an example in Scala which shoud cover the following concepts:
Please note that the JSON support is done by specifically using the json4s library and not through the internal support that Dispatch library has for JSON (am yet to figure that one out). In order to demonstrate something more real, I will be using the Google Places API, make a server call to it and convert the response back to strongly typed instances.
First things first, if you wish to run this, then you will have to get your own Google API key. The instructions are pretty clear at Google’s documentation site. Once you have an API, you are good to go.
To get started, create a folder for your project. Then place a “build.sbt” whose contents looks as shown below.
Some important points, that helps a newbie in Scala:
Our goal here is to make a call to the Google Places service to find some locations around a specific location & that matches a keyword. I am only going to list here the parameters that I know are needed for this call to work. For other details, refer to the Places API documentation.
So what are the steps, in general? The usual steps in any programming environment to make service calls are as follows:
For an asynchronous way to do thins, the step 4 should be read as “once the response is available, then”. So the above 7 steps are shown below. Newbie should also pay attention to some example use case on how to work with Futures (Line #21, #32) , how pattern matching in scala is so powerful (see if the if statement mixed with case at line #24)
In order to work with JSON, either you can leave JSON as is and look for specific properties that you are interested or you can “deserialize” JSON to strongly typed objects. I prefer deserializing JSON and working with real objects. This is so much easier than having to deal with RAW JSON all the time. So the steps to convert JSON to objects are:
First things first, JSON is usually mapped to “case classes” of Scala. And to further simplify the above flow, you can use an online converter that interprets the JSON you give & gives you classes. There exists one for C# and there is one for Scala as well. Once you have the boiler plate code, you can work on the generated code to fit your needs.
So get the sample output from the Google Places API and use json2caseclass to create case classes. We will be using json4s to work with JSON deserialization for Scala. And that would not just work directly with the output returned from the json2caseclass because if it does not find a value in the JSON for the case class member, it will be really unhappy. The fix for that is to make the property optional and how do you make that in Scala? The standard way of making anything option in Scala is to use Option[T]. So the corrected case classes are also listed.
When you bring it all together, you will get
And the output is
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
ENTER TO EXIT
Total Results: 11
Starbucks at 1364 Centennial Ave, Piscataway Township
Starbucks at 1 Lincoln Hwy, Edison
Starbucks at 391 George St, New Brunswick
Starbucks at 5000 Hadley Center Dr, South Plainfield
Starbucks at 125 Raritan Center Pkwy, Edison
Starbucks at 774 Route 1 N, Iselin
Starbucks at 4 Mt Bethel Rd, Warren
Starbucks at 250 Woodbridge Center Drive, Woodbridge Center, Woodbridge Township
Starbucks at 120 Cedar Grove Ln, Somerset
Starbucks at 45 Central Ave, Clark
Starbucks @ The Courtyard Bistro at 250 Davidson Ave, Somerset
Later, I will try take this and create a REST service that sends the Places results to other callers.