Question

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])

json.extract[Done]

}

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.

Was it helpful?

Solution

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
newDone.sample1("Fishsticks").sample2("InYour").sample3("Mouth")

// Persist it
newDone.save

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top