Tuesday, January 29, 2013

scala + lift + json

So since I really really really dislike soap and Java, I decided to play a bit more with scala and rest based web services. Basically parse some JSON strings with scala.

Now after browsing countless articles and remembering the JSON structure, I finally found the 'lift-json' project, which simplifies my live as busy developer a bit.

How to parse JSON with scala?


package edu.ucdavis.fiehnlab.alchemy.core.communication.services.reader
import edu.ucdavis.fiehnlab.alchemy.core.process.types.LibrarySpectra
import net.liftweb.json.parse
import net.liftweb.json.DefaultFormats
/**
 * connects to the alchemy webservice and fetches all registered co
 */
class WebserviceLibraryReader {

  /**
   * internal converter class to simplify things
   */
  case class JsonCompound(name: String, inchikey: String, retentiontime: Double, theoretical: Double, massspectra: String)

  case class CompoundWapper(compound: Array[JsonCompound])

  implicit val formats = DefaultFormats

  /**
   * downloads the compounds for the given URL and returns it as a set of library spectra
   */
  def fetchCompounds(url: String): Set[LibrarySpectra] = {

    val compounds: CompoundWapper = parse("""
        
        {"compound":[{"name":"test","inchikey":"ADVPTQAUNPRNPO-UHFFFAOYSA-N","retentiontime":11111,"theoretical":11,"massspectra":"111:1 112:2 113:3"},{"name":"test","inchikey":"ADVPTQAUNPRNPO-UHFFFAOYSA-N","retentiontime":11111,"theoretical":11,"massspectra":"111:1 112:2 113:3"}]}
        
        """).extract[CompoundWapper]

    compounds.compound.foreach { x: JsonCompound =>
      println(x)
    }
    
    
    null
  }

}

object WebserviceLibraryReader {

  def main(ars: Array[String]) = {
    println(new WebserviceLibraryReader().fetchCompounds("http://localhost:8080/alchemy-admin/services/queryAllCompoundsForMethod/test"))
  }
}

The complete tutorial can be found here

No comments:

Post a Comment