Showing posts with label scala. Show all posts
Showing posts with label scala. Show all posts

Thursday, February 28, 2013

Converting Play 2.1 JSON to Scala XML

Working with JSON and XML is pretty simple in Play-framework. For JSON, it is recommended to use Play's type-class based library.

I recently, came across a scenario where I had to convert Play 2.1 JSON to Scala XML and vice versa. I didn't find any internal classes in Play which could handle this. Lift provides a tool to handle a similar conversion.

To convert the JSON into XML I used recursion. Although, the scala.xml.Elem constructor allows only one Node to be added as child, there is another method copy, which takes in a Seq[Node] as child.  

To handle conversion from XML to JSON, I modified the toJson method in Lift to result in Play JSON. 

Link to gist



Thursday, November 8, 2012

Setting up Play-Scala Project in IntelliJ Idea


The easiest way to setup a Play-Scala project is by using the giter8 template.

Setup the project using by giving the following command in the terminal

$ g8 typesafehub/play-scala
# cd into the newly created directory and run with 'sbt run'

In the sbt console
$ idea

Once this is done, the project can be opened from Idea.

Setting up database for the project
  1. Add the database driver in project/Build.scala (throws driver not found error on being skipped)
  2. In conf/application.conf specify the database configuration(more on database in play)
  3. In conf/evolutions/default create a file 1.sql which has the database schema(more on evolutions)

Prerequisites
  1. sbt
  2. giter8
  3. Play and Scala plugin for IntelliJ Idea