Created
December 18, 2012 21:14
-
-
Save st3v/4332069 to your computer and use it in GitHub Desktop.
lift - parse, extract, and find
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.scalatest.FunSuite | |
import net.liftweb.json._ | |
class Dummy extends FunSuite { | |
private implicit val formats = net.liftweb.json.DefaultFormats | |
private def data = | |
"""[ | |
{ | |
"id": "1234", | |
"name": "Tim the Baker", | |
"description": "Needs a Kiva Zip loan to bake goodies", | |
"quantity": 200 | |
}, | |
{ | |
"id": "4567", | |
"name": "Jeremy the social worker", | |
"description": "Needs a Kiva Zip loan to help his community", | |
"quantity": 800 | |
} | |
]""" | |
test("parse and find") { | |
val subjects = parse(data).extract[List[TestSubject]] | |
val result = subjects.find(_.id == "4567").toList | |
assert(result.length === 1) | |
assert(result(0).name === "Jeremy the social worker") | |
} | |
} | |
case class TestSubject(id: String, name: String, description: String, quantity: Int) { | |
override def equals(obj:Any) : Boolean = { | |
obj.isInstanceOf[TestSubject] && obj.asInstanceOf[TestSubject].id == id | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment