Take the 2-minute tour ×

I'm trying to save data parced with Lifts' Json data extraction and save it to the database(via mapper) but I cannot find where to connect the snippit to the mapper. The code looks like this.

Here is a test snippit.

package com.testjson.snippet

import dispatch._
import net.liftweb.json.JsonParser._
import java.io.{ByteArrayOutputStream}
import com.testjson.model.Done

class HelloWorld {
def howdy = <span>Welcome to hello-lift at {new _root_.java.util.Date}</span>
val http = new Http
val bos = new ByteArrayOutputStream
val myRequest = new Request("http://testing.com/folder/file.json")
val rawdata = http(myRequest >>> bos)
val bs = bos.toString

val json = parse(bs)

implicit val formats = net.liftweb.json.DefaultFormats

case class One(sample1: String, sample2: String, sample3: String)
case class Two(samplea: String, sampleb: String, samplec: String, sampled: String)
case class Three(alpha: Int, beta: Int, charlie: String, delta: String)
case class Done(notice: List[One], header: List[Two], data: List[Three])



And here a sample Model.

package com.testjson.model

import net.liftweb.http.SHtml
import net.liftweb.common._
import net.liftweb.mapper._

class Done extends LongKeyedMapper[Done] with IdPK {

def getSingleton = Done

object sample1 extends MappedPoliteString(this, 12)
object sample2 extends MappedPoliteString(this, 12)
object sample3 extends MappedPoliteString(this, 56)
object samplea extends MappedPoliteString(this, 12)
object sampleb extends MappedPoliteString(this, 12)
object samplec extends MappedPoliteString(this, 56)
object alpha extends MappedPoliteString(this, 56)
object beta extends MappedInt(this)
object charlie extends MappedInt(this)
object delta extends MappedPoliteString(this, 56)


object Done extends Done with LongKeyedMetaMapper[Done]

I've looked through my print copy of the book, the updated ebook, and the google group to no avail. I'm just looking for some help or maybe a code example to point me in the right direction.

share|improve this question
I do not fully understand, are your both code fragments a part of Lift application? If yes, database connection init is usually a part of bootstrap.liftweb.Boot class –  Alexander Azarov Jan 24 '10 at 19:17
I'm sorry if I wasn't clear. They are one is the model and the other a snippet, what I'm looking for is how to connect the two. To take the parced Json data and place it in the data made with the mapper. –  Randin Feb 8 '10 at 8:11
add comment

1 Answer

up vote 1 down vote accepted

You create instances of a modeled class using Object.create, use the apply methods of the class' objects to set the instance's values, then persist by calling save.

Say you wanted to set these values:

  sample1: "Fishsticks"
  sample2: "InYour"
  sample3: "Mouth"

You then want to save the model to the database.

Since you can chain function calls, you can do this very succinctly:

// Create the instance
var newDone = newDone.create

// Set it's instance variables

// Persist it

So, once you've parsed your JSON data, you can use use the format above to persist it into your database.

share|improve this answer
add comment

Your Answer


