Monday, November 1, 2010

I'm abandoning using XML parsing for this project. While I can get the results back from GoGrid now, I can't seem to figure out a simple, clean way to get the result string transformed into the objects for manipulation in our code. GoGrid has the XML set up as basically key value pairs, where for each object 'type' is specified as an attribute to the element, which makes squishing the result string through something to pop out an object at the end kind of an ugly hassle.

GoGrid does provide the default response format to be Json, so I'm now chasing down this avenue.

First up is to find an appropriate library to handle the heavy lifting, and for this I'm using Lift-Web's Json library. Lift-web is a large project made up of several projects, for my purposes, I only need the Json module. It took a little bit of scrounging around the net, but I found that what I need is the lift-json module. The getting started documentation that I found on lift-web's site tells you how to set up a project from scratch, which is not what I need. I just needed access to this particular lib. After browsing their source tree, I decided to just put the information into my project's pom that I found here combined with the announcement that Lift-Web 2.1 has been released and inserted the following into my pom:

//DOES NOT WORK
net.liftweblift-json2.1
//DOES NOT WORK

This (as the comments note) does not work. I ended up with the following error when I tried to save the pom:

Missing artifact net.liftweb:lift-json:jar:2.1:compile

Okay, so that didn't work. So then I tried using version 2.0 instead to see if that will work:

//SORT OF WORKS
net.liftweblift-json2.0
//SORT OF WORKS

Well this seemed promising. I was able to save the pom with no errors. So the next step was to give the JsonParse -ing a whirl in the command line Scala interpreter (Eclipse v3.5.2 with Scala IDE for Eclipse v1.0.0.201009232358 plugin). I found an example on Stackoverflow and tried it out for myself:

scala> import net.liftweb.util.JSONParser
:8: error: value util is not a member of package net.liftweb
import net.liftweb.util.JSONParser

Um, okay. So that didn't work. Looking through the library that I added, I decided to try this next:

Heh?! What the heck is going on? After some more fruitless searching on the web, I looked back at this site and saw that there was also a lift-json_2.8.0 package. I am using Scala 2.8.0 ... so I changed my pom to:

So the next thing on the list is to take a Json string, parse it and shove it into an object. An example of this can be found on lift-web's source code site and scroll down to extraction. I decided to go for a very simple test, using just a list of numbers (I'm still on the console in Eclipse with Scala interpreter):

So close...but not quite there. Back to the googling board. I eventually found this in lift-web's markmail and they say that:

"The extraction feature uses paranamer to reflectively read the
constructor parameter names. Unfortunately paranamer lib does not work
with Scala console. Your example should work if you put your case
class definitions into .scala file and then import those after
compilation."

Okay, so now I created a new scala file in my project that contains the following case class:

About Me

I know a little bit about a lot of different programming languages and stuff, but sometimes, I don't know enough and find myself searching the interweb in circles for what should be a simple answer. This is a compilation of the fruits of these searches in hopes that will help some one out there that has similar questions/issues/problems and maybe save them a little bit of time. :)